By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,798 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,798 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)?
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <ma*****@yahoo.com> wrote in message
news:g6********************@comcast.com...
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)?

Nov 20 '05 #2

P: n/a
"Max" <ma*****@yahoo.com> schrieb
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

Nov 20 '05 #3

P: n/a
Max
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?
Marina wrote:
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" <ma*****@yahoo.com> wrote in message
news:g6********************@comcast.com...

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


Nov 20 '05 #4

P: n/a
Max
Armin Zingler wrote:
"Max" <ma*****@yahoo.com> schrieb

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.


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.
Nov 20 '05 #5

P: n/a
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.

"Max" <ma*****@yahoo.com> wrote in message
news:K8********************@comcast.com...
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?
Marina wrote:
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 classthat inherits from EventArgs, add other properties, so that you can pass
information from the form raising the event to the event handler.

To
"Max" <ma*****@yahoo.com> wrote in message
news:g6********************@comcast.com...

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


Nov 20 '05 #6

P: n/a
"Max" <ma*****@yahoo.com> schrieb
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.


Wrong. ByVal passes a copy of the passed value. As classes are reference
types, it is a copy of the reference, not a copy of the object:

http://groups.google.com/groups?selm...ews.freenet.de

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.