VB.NET - Passing address of an instance of a class

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

Last time I tried to explain this on another forum it didn't go too
well, so I'll try my best and if you know what I'm talking about then
please tell me how to do this.

I have a class, inside I have some public functions and private
variables. Inside the class I also have a declaration of a new form
object. One of the functions of the class takes that form object, shows
it with showdialog and the basically passes the control to the form and
the user. Now then, while the form is opened it needs to be able to call
a function from the class that created the form. Not just a function
from the same object type, but it has to be a function from the same
instance.

So basically I need to somehow have an object inside the form that is of
the same type as the initial class and points to the same instance. In
my C++ days this would've been a breeze, but with VB I can’t figure out
how to do this. I tried creating a public function that took one
argument, something like "ByRef master as <objecttype>". Then inside the
form class I'd have a private variable also of that type and would say
mymaster = master in that function hoping that no mymaster would be set
to the instance of master. But when I tried to use mymaster I got an
error that this class does not have an instance. Any ideas (or anyone
even understand what I'm saying lol)?
 
There is a couple of ways you could do this, but I would recommend this one:

1) Define an event in your form class
2) In the object that created the form, register a method to be the event
handler for the event of the form you just created:

So defining the event in your form:

Public Event MyCustomEvent(ByVal e as Object, args as EventArgs)

To raise the event in your form:

RaiseEvent MyCustomEvent(Me, new EventArgs)

Create the form and registering for the event:

....
Dim myForm as MyFormDialog = new MyFormDialog
AddHandler myForm.MyCustomEvent, AddressOf CustomEventHandler

Where CustomEventHandler is a sub with an 'Object' and 'EventArgs' for
arguments.

You of course do not have to use EventArgs - you can define your own class
that inherits from EventArgs, add other properties, so that you can pass
information from the form raising the event to the event handler.

To
 
Max said:
Last time I tried to explain this on another forum it didn't go too
well, so I'll try my best and if you know what I'm talking about
then
please tell me how to do this.

I have a class, inside I have some public functions and private
variables. Inside the class I also have a declaration of a new form
object. One of the functions of the class takes that form object,
shows
it with showdialog and the basically passes the control to the form
and the user. Now then, while the form is opened it needs to be able
to call a function from the class that created the form. Not just a
function from the same object type, but it has to be a function from
the same instance.

So basically I need to somehow have an object inside the form that is
of the same type as the initial class and points to the same
instance. In my C++ days this would've been a breeze, but with VB I
can’t figure out how to do this. I tried creating a public function
that took one argument, something like "ByRef master as
<objecttype>". Then inside the form class I'd have a private
variable also of that type and would say mymaster = master in that
function hoping that no mymaster would be set to the instance of
master. But when I tried to use mymaster I got an error that this
class does not have an instance. Any ideas (or anyone even
understand what I'm saying lol)?


What you did I would have done, too.

Form:
private m_YourClass as YourClass

public shadows function Showdialog(byval o as YourClass) as dialogresult
m_yourClass = o
return mybase.Showdialog
end function


Class YourClass
sub anysub
dim f as new form1
dim result as dialogresult
result = f.showdialog(me)
end sub
end class

Within the Form you can access m_YourClass.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Thanks, that works perfectly. Just two more questions... One, do the
arguments have the be (ByVal e as Object, args as EventArgs) or can I
just set them to anything else assuming that it's the same for the Event
as it is for the function that handles it? I just tried it with just
passing an integer and it seems to work just fine, but maybe I
overlooked something (have only started with VB.NET as you can tell so
not yet up on all of the things). Second, just out of interest what are
some of the other ways this could be done?
 
Armin said:
What you did I would have done, too.

Form:
private m_YourClass as YourClass

public shadows function Showdialog(byval o as YourClass) as dialogresult
m_yourClass = o
return mybase.Showdialog
end function


Class YourClass
sub anysub
dim f as new form1
dim result as dialogresult
result = f.showdialog(me)
end sub
end class

Within the Form you can access m_YourClass.

Don’t think that would work, you're passing the class ByVal which would
create a new instance. So while it would all be fine while the form is
shown none of the data would be saved to the original class because that
was a different one. The reasons I'm doing all this is because the form
might be shown multiple times (and closed multiple times), but the
changes that the user made have to be there every time and are stored in
the original class.
 
The arguments can be anything you want. MS set a standard in having 2 args -
first being the object that raised the event, and the second being EventArgs
(or one of its descendents) that contains all the information. But you can
have no arguments at all - or any number of arguments that you wish of any
type. As long as the method handling the event has the same signature as the
event definition.

Another way is you could have a public variable or property on the form that
is a delegate (function pointer). The class creating the form would set this
property to be a delegate to the method that should be called, and the form
would invoke this delegate at the right time.

In reality, this is very similar to the event handling - after all, raising
an event just invokes one or more delegates registered for that event. But
using Events seems to be cleaner and really this is what they were designed
for.
 
Back
Top