2

In VB.NET, can we pass an original object as an argument without instantiating it? For example, I have two forms in my project formA and formB. Now I have this code.

Public Sub AddForm(Form Outer, Form Inner)
    Inner.FormBorderStyle = FormBorderStyle.None
    Inner.TopLevel = False;
    Inner.Dock = DockStyle.Fill
    Inner.WindowState = FormWindowState.Normal
    Outer.Controls.Add(Inner)
    Inner.BringToFront()
    Inner.Show()
End Sub

Now I can use this in any event like:

AddForm(formA, formB)

but ...

Taking the same code in C#

public static void AddForm(Form Outer, Form Inner)
{
    Inner.FormBorderStyle = FormBorderStyle.None;
    Inner.TopLevel = false;
    Inner.Dock = DockStyle.Fill;
    Inner.WindowState = FormWindowState.Normal;
    Outer.Controls.Add(Inner);
    Inner.BringToFront();
    Inner.Show();
} 

I cannot call it like:

AddForm(formA, formB);

It gives the error

formB is type but is used like a variable.

Instead, I have to call it like:

AddForm(A, new B());

Apart from this, in VB.NET in any class like formA, if I type formA, I can see all the objects and controls present there but not in C#. Again, I have to make a new instance to see all the controls. This becomes a problem if I want to manipulate two running and working instances with each other. So what basic thing am I missing here?

(I am an amateur programmer migrating from VB.NET to C#. Things are going nice and clean expect for the ones I've recently figured out.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • When you are calling this, are you in FormA? If you are, you need an instance (or a reference to the instance) of FormB to be able to call this method. While you are in FormA, you have a way of getting to the instance of FormA, but you then need a way of getting to FormB. You can either instantiate a new FormB (hence new B() works), or you can use another class that manages references to all your forms or similar. – dash Jan 14 '12 at 17:15
  • 2
    This is specific to the Form class in VB.NET, a VB6 compat hack. Unlearning this is always difficult. You'll just need to remind yourself that you cannot pass the name of a *type* when an *object* is needed. – Hans Passant Jan 14 '12 at 17:16
  • @HansPassant Thanks for the knowledge, I didn't know that about Form working that way in VB.Net. I assumed that would no longer work, and instead use Me ;-) – dash Jan 14 '12 at 17:18
  • Your C# is also a static method (Shared in VB.NET), which the original VB.NET isn't. – Oded Jan 14 '12 at 17:21

3 Answers3

2

FormA and FormB are classes in VB that can be instantiated just like they are in C#.

However, Microsoft added support for automatic, default instances of forms to VB.Net in VS2005 in order to support developers migrating from VB classic that were getting confused by the need to create and manage instances of forms.

However, this is NOT considered a best programming practice since it can lead to various issues, such as the inability to access the form from a background thread and the inability to create multiple instances of the form.

Once you understand OOP and class instantiation, there is absolutely no reason to use the default instances.

In fact, you can disable the use of default form instances by either creating a Sub New in the form that is declared Friend (so that it can be created elsewhere in the project):

Friend Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

or by modifying the Public Sub New to include parameters:

Public Sub New(SomeValue As String)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

In either case, any references to the default instance of the form will result in the following two compile-time errors whereever the default instance is referenced:

'Form1' is a type in 'WindowsApplication1' and cannot be used as an expression.

Reference to a non-shared member requires an object reference.

Having said all of that about how your current app works in VB.Net, the specific answer to your question about C# is that you always need to create new instances of the forms in C#.

For example:

AddForm(new formA(), new formB());
Community
  • 1
  • 1
competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

Q: I just want to know that in vb.net we can pass original object as an argument without instantiating it

A: What you're looking for is called "pass by reference".

Here are some good examples:

http://www.java2s.com/Code/VB/Language-Basics/ObjectparameterpassedbyValueandbyReference.htm

Here's the Microsoft documentation:

http://msdn.microsoft.com/en-us/library/ddck1z30.aspx

paulsm4
  • 114,292
  • 17
  • 138
  • 190
-1

I'm not certain, but I'm pretty sure VB does this kind of "pseudo-object-oriented programming" where formA and formB are objects, not classes. You do not create objects in VB in your situation, you are simply manipulating the objects formA and formB.

When you migrate to C# you are dealing with objects and classes. Your class is "formA" but it is simply a framework for objects of the type "formA" that you will instantiate as objects. This article should explain this a little more.

jrbalsano
  • 834
  • 7
  • 18
  • 3
    This *only* applies to the `Form` class for backwards-compatibility with VB 6 code. And you certainly *can* use the `Form` class as normal, so the best idea is just not do it this way at all, even in VB.NET. – Cody Gray - on strike Jan 14 '12 at 17:23