473,491 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access to instance objects in different classes

Hi all,
I have two different classes and one instance of the Form.
I would like to access the members of the Form instance in both classes
without having to create a new instance in the other class, because that
would give me access to different members than in the first instance and
would not behave as one global object. How can I do that, any guess?
Thanks.

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #1
4 4797
Mike,

By passing the referenes for reference types and the value for value types
(which is the standard passing way)

I hope this helps,

Cor
Nov 17 '05 #2
"MikeAth via DotNetMonster.com" <fo***@DotNetMonster.com> a écrit dans le
message de news: 1e******************************@DotNetMonster.com...
I have two different classes and one instance of the Form.
I would like to access the members of the Form instance in both classes
without having to create a new instance in the other class, because that
would give me access to different members than in the first instance and
would not behave as one global object. How can I do that, any guess?


You need the Singleton design pattern.

public class MyClass
{
private static MyClass instance = null;

private MyClass() {}

public static MyClass Instance()
{
if (instance == null)
instance = new MyClass();
return instance;
}
}

Called from either form like this :

{
MyClass formUser = MyClass.Instance();
...
}

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3
Thanks, I think that singleton design pattern is exactly what I need. I
just expected such access to be more common and easily usable in C#, but I
hope it will work fine this way.

--
Message posted via http://www.dotnetmonster.com
Nov 17 '05 #4
Joanna Carter (TeamB) <jo*****@nospamforme.com> wrote:
I have two different classes and one instance of the Form.
I would like to access the members of the Form instance in both classes
without having to create a new instance in the other class, because that
would give me access to different members than in the first instance and
would not behave as one global object. How can I do that, any guess?
You need the Singleton design pattern.


That sounds like a bad idea to me in this case. As Cor said, all the OP
needs is for the reference to the form to be given to both of the other
classes in some way or other, whether it's by calling a method, setting
a property, or passing a reference in the constructor. There's no need
for a singleton.
public class MyClass
{
private static MyClass instance = null;

private MyClass() {}

public static MyClass Instance()
{
if (instance == null)
instance = new MyClass();
return instance;
}
}


That's not a thread-safe way of implementing a singleton. See
http://www.pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5359
by: sedefo | last post by:
I ran into this Microsoft Patterns & Practices Enterprise Library while i was researching how i can write a database independent data access layer. In my company we already use Data Access...
5
1462
by: TheFerryman | last post by:
How bad is it to include a non const accessor to a container of objects? For instance template <class WheelType> class CarBase { typedef std::list<WheelType*> WheelList; private:
166
8474
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
9
1967
by: MLH | last post by:
I need a fundamental explanation of Class Modules - something suitable for newbies. Access 2.0 didn't seem to focus on them very much. Now that I'm using Access 97, it seems they're everywhere. thx...
6
2065
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
3
1923
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the...
5
2179
by: ZikO | last post by:
Hi there. I have a problem. I have created nested classes but don't know how to access to inner classes. I know I can create objects: Hen Object; Hen::Nest ObjectNest; Hen::Nest::Egg...
5
3153
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
4
1304
by: Gre7g Luterman | last post by:
I suppose I was lulled into complacency by how Python makes so many things look like classes, but I'm starting to realize that they're not, are they? I'm writing a C program which handles Python...
0
6978
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...
0
7154
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,...
0
7190
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...
0
7360
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...
0
5451
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,...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.