Vb Newbie question

A

Altman

I just started programming in VB.net, I have done alot of work in VB and
FoxPro. I have a form with MS Comm. activex control and would like to pass
this object to another form by reference. Is there a way to do this upon
loading the second form?

--
Altman


Forward this to everyone on your email list and Bill Gates will Magically
Pop Out of your computer and hand you a check for $500 per person you sent
it to, and he will also mail a check to a starving Ethiopian in China with
Brain Cancer.
 
N

nate axtell

To pass the main form object to a sub form i create a new constructor in the
subform. Something like:

Private aForm as cMainForm
Public Sub New(ByRef mForm as cMainForm)
aForm = mForm
End Sub

Then you can just use the aForm object to access the main form instance.
Would something like that work for passing in your object to a new form?
You would change "cMainForm" to whatever the class (or whatever type) your
object is.
-Nate
 
A

Altman

yeah that looks like it will work, I did not know you could do constructors
in vb, was this available in VB6? Also just an after thought but when I put
the Activex Comm control on the form it says it is type
AxMSCommLib.AxMScomm, why is it not MSCommLib.MScomm? MSCommLib.MScomm is
what I am used to seeing in other languages.
 
N

nate axtell

I don't know if it was available in 6, i never used 6. My only guess to
"AxMScomm" is that it uses a different class in .NET then it did in 6. Both
classes are included in the control and you see the different class when
using .NET; like there were enough differences between the two to justify
making a whole other class for the .NET version. That is a pure guess
though.
 
N

nate axtell

Why?
If you use 'ByVal' will you be able to modify the object, like in my example
would I be able to manipulate my existing main form? I was just reading
that if the argument is a reference type then you can modify its members.
As in an array, you can't set it equal to another array but you can modify
its members. Does and object like a form work the same way?
(info on passing args:
http://msdn.microsoft.com/library/d...ry/en-us/vbcn7/html/vaconArgPassMechanism.asp)
-Nate
 
H

Herfried K. Wagner [MVP]

* "nate axtell said:
If you use 'ByVal' will you be able to modify the object, like in my example
would I be able to manipulate my existing main form?

You will be able to manipulate the object (which is an instance of a
reference type) independent from passing it 'ByRef' vs. 'ByVal'. By
passing it 'ByVal', you can change the object /reference/ passed into
the method, which only makes sense in few cases.
 
N

nate axtell

Ok, so if I want my subform to manipulate my main form when the subform
closes I should pass in a ByVal argument of the mainform to the subform's
constructor when initializing creating the subform? Is there a performance
gain by doing this? I figured that by using 'ByRef' I was keeping the
number of objects to a minimum, so I wasn't creating more objects in memory
than I needed. I am also used to C++ where a ByVal reference will not let
you manipulate the original object, so this seems strange that 'ByVal' lets
you manipulate the original.
-nate
 
I

Imran Koradia

Yes - you should still be able to manipulate your main form. What ByVal
means for Reference Types is that you're simply creating a new reference
variable - not a new object. So, if you have something like:

Private aForm as cMainForm
Public Sub New(ByVal mForm as cMainForm)
aForm = mForm
End Sub

The variable mForm and aForm both point to the same cMainForm object - they
both store the same address. This should allow you to still manipulate your
original main form as with ByRef. The only difference being that if the
variable is passed ByVal and if you change the object pointed to by the
argument variable, the original reference stays intact while for ByRef, both
the variables will point to the new object.

Imran.

nate axtell said:
Why?
If you use 'ByVal' will you be able to modify the object, like in my example
would I be able to manipulate my existing main form? I was just reading
that if the argument is a reference type then you can modify its members.
As in an array, you can't set it equal to another array but you can modify
its members. Does and object like a form work the same way?
(info on passing args:
http://msdn.microsoft.com/library/d...ry/en-us/vbcn7/html/vaconArgPassMechanism.asp)
-Nate
 
A

Altman

boy that gets confusing because I was always used to C++ also where I was
told if you want to modify an object you pass the pointer to that object and
not the object. Otherwise it will create a whole new object. But in my
case here I don't see any case for going one way over the other. I am just
creating a simple communications exe with 2 forms, one will have the comm
settings the other will do the communication.
 
J

Jay B. Harlow [MVP - Outlook]

Nate,
The following site does a good job of discussing ByRef & ByVal parameters as
opposed to Reference & Value types.

http://www.yoda.arachsys.com/csharp/parameters.html

Its in C# however with your C++ background it should be understandable.

Hope this helps
Jay


nate axtell said:
Ok, so if I want my subform to manipulate my main form when the subform
closes I should pass in a ByVal argument of the mainform to the subform's
constructor when initializing creating the subform? Is there a
performance gain by doing this? I figured that by using 'ByRef' I was
keeping the number of objects to a minimum, so I wasn't creating more
objects in memory than I needed. I am also used to C++ where a ByVal
reference will not let you manipulate the original object, so this seems
strange that 'ByVal' lets you manipulate the original.
-nate
 
C

Cor Ligthert

Altman,

When you by instance want to create the object in your method you have to
use ByRef, because with ByVal it would pass a Null reference.

I hope this gives an idea?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top