Argument passing in C# and VB.NET

1.4k views Asked by At

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.)

3

There are 3 answers

1
paulsm4 On

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

1
jrbalsano On

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.

0
competent_tech On

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());