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