473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3952
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(B yVal e as Object, args as EventArgs)

To raise the event in your form:

RaiseEvent MyCustomEvent(M e, new EventArgs)

Create the form and registering for the event:

....
Dim myForm as MyFormDialog = new MyFormDialog
AddHandler myForm.MyCustom Event, AddressOf CustomEventHand ler

Where CustomEventHand ler 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******** ************@co mcast.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(byva l o as YourClass) as dialogresult
m_yourClass = o
return mybase.Showdial og
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(B yVal e as Object, args as EventArgs)

To raise the event in your form:

RaiseEvent MyCustomEvent(M e, new EventArgs)

Create the form and registering for the event:

...
Dim myForm as MyFormDialog = new MyFormDialog
AddHandler myForm.MyCustom Event, AddressOf CustomEventHand ler

Where CustomEventHand ler 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******* *************@c omcast.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(byva l o as YourClass) as dialogresult
m_yourClass = o
return mybase.Showdial og
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******** ************@co mcast.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(B yVal e as Object, args as EventArgs)

To raise the event in your form:

RaiseEvent MyCustomEvent(M e, new EventArgs)

Create the form and registering for the event:

...
Dim myForm as MyFormDialog = new MyFormDialog
AddHandler myForm.MyCustom Event, AddressOf CustomEventHand ler

Where CustomEventHand ler 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******* *************@c omcast.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(byva l o as YourClass) as
dialogresult
m_yourClass = o
return mybase.Showdial og
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
3913
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
3282
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(); Population pool = Population(& network); .... but this doesnt seem to work.. however the following works... Network * network = new Network();
17
9391
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 commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
2
4356
by: ToChina | last post by:
Hi, I have the following code: class A { } class B : A { }
3
2031
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 I need the thread to call has a parameter... How do I call a function with the addressof(..) with a parameter value? for example: public function processData(byval data as arraylist) as boolean.... ....
12
2684
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
1912
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 main class whenever it is needed: class shop_main { var $prices = null; function &get_prices() {
7
3364
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 based on the segments of the URI it will fire up some controller class, for instance, say I have an inbox in which end-users can view messages they got from other users, they'ld start at:
2
4247
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 question is how to elegantly pass a command line parameter from Instance_B to Instance_A where Instance_A was running prior to Instance_B. For example, the user can launch the program by passing a file name as a command line argument. The program...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.