473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
6 3919
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
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
3
by: John C | last post by:
Hi, I am a little uncertain about the concept of passing a reference to a class to another instance of a class. for instance I thought that the following was ok: Network network = Network();...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
2
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
3
by: Sam Learner | last post by:
Hello everyone, I am developping an application, I create a thread for the application because it is about to download a large file, and wanted it to do it inside of a thread... Now, the function...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
7
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter framework. Basically, the site will examine the URI and...
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.