Home All Groups Group Topic Archive Search About

Problems with ByRef parameters

Author
6 Feb 2006 9:44 PM
doriengard
Dear VB gurus,


A long question, thanks for your patience in advance :-)


Part 1)  I have a VB windows application that contains a start-up form:
frmMain


Inside frmMain, there is a Private variable that is specified as the
top level object of a third party program


Private ThirdPartyProgram as Happ.HappLS ' object reference for third
party program
________


Part 2) I have a Class named aplBlock that needs to contain a reference
to ThirdPartyProgram contained in frmMain.  What I have done is created
a constructor that forces a Happ.HappLS value to be passed ByRef to the
class when it is created


Class aplBlock

Private 3rd as Happ.HappLS

Sub New (ByRef ao_Object as Happ.HappLS)
  3rd = ao_Object
End Sub
________


Finally to my question

What I want to do is create an instance of aplBlock anywhere in my
application and be able to use properties and methods related to the
variable 3rd.

For example, in a form that is not the frmMain, I want to have the
following code under a command click event

Class frmOther

dim lo_Block as aplBlock


_ClickEvent
lo_Block = new aplBlock(reference to frmMain ThirdPartyProgram)
BogusVariable = lo_Block.Property

The error message I get when the BogusVariable line is executed is that

the Property does not exist.  This implies to me that the ByRef passing

into the instance of the aplBlock Class is not functioning as I expect.


I understand that I could set up properties that get me directly back
to the frmMain.  However, the Class aplBlock (and other similar stuff)
is being written so it is transportable to different applications.  It
is necessary to be able to pass the ThirdPartyProgram reference into
the class so the class is not dependent upon the structure of the
calling application.

I hope this makes sense.

Any advice would be appreciated.

I tried this in VB.NET 2003
I will be upgrading to VB.NET 2005 soon.  Advice in either version (if
there is a difference) would be welcome.

Michael Reed
michael.r***@netl.doe.gov

Author
7 Feb 2006 12:59 AM
AMercer
> Class aplBlock
> Private 3rd as Happ.HappLS
> Sub New (ByRef ao_Object as Happ.HappLS)
>   3rd = ao_Object
> End Sub

Since you don't modify ao_Object in New(), you dont need byref - byval is
enough.

> What I want to do is create an instance of aplBlock anywhere in my
> application and be able to use properties and methods related to the
> variable 3rd.

So, make a module and put

  Public ThirdPartyProgram as Happ.HappLS

in it.  That way, in all places where you want to instantiate aplBlock, you
will have addressability to ThirdPartyProgram.

Alternatively, if you really insist on leaving it Private in your form, and
you have only one instance of Happ.HappLS, then you could use a shared
variable in aplBlock.

Class aplBlock
  Private Shared 3rd As Happ.HappLS = Nothing
  Sub New (ByVal ao_Object as Happ.HappLS)
     if not ao_object is nothing then 3rd = ao_object
     debug.assert (not 3rd is nothing)
  End Sub
End Class

With this design, you may have many instances of aplBlock, but they will all
use the same 3rd variable.  In your main form, instantiate ThirdPartyProgram,
and then instantiate an aplBlock passing ThirdPartyProgram to New. 
Subsequent aplBlock instantiations will work fine passing Nothing to New.  By
the way, I assume you know that 3rd is not a valid vb variable name.
Author
7 Feb 2006 3:25 PM
Chris Dunaway
doriengard wrote:

> Class aplBlock
>
> Private 3rd as Happ.HappLS
>
> Sub New (ByRef ao_Object as Happ.HappLS)
>   3rd = ao_Object
> End Sub
> ________

You have created a private variable to hold the instance of
Happ.HappLS.  Is the a public property declared to expose it outside
the class?


>
> Class frmOther
>
> dim lo_Block as aplBlock
>
>
> _ClickEvent
> lo_Block = new aplBlock(reference to frmMain ThirdPartyProgram)

Here, you create a new instance of aplBlock and pass in the
ThirdPartyProgram reference.

> BogusVariable = lo_Block.Property

Here you say you get an error when accessing Property.  What is
Property?  Is it public in aplBlock class?  Is 'Property' supposed to
be a property of the ThirdPartyProgram?

I think we need a little more detail about the aplBlock class.  It
sounds like the Happ.HappLS instance is not exposed outside the
aplBlock class
Author
8 Feb 2006 2:26 PM
doriengard
I found my problems and both answers above helped.

I used the shared variable technique and found a bug in my aplClass
that was preventing proper execution of the .Property
item.

Also, I needed to change the initial instance of the Private
bogusvariable Happ.HappLS to be a Public variable

Problems solved
Thanks