469,328 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

C# class reference to another class?

Hi all - this should be a very easy question to you guys.

Say I have two classes, class1 and class2. In my application, class2
can't operate without a class1, as class1 performs communication over
the network, as a sort of abstraction layer.

Now I could create a class1, and a class2, and make every method of
class2 take a reference to my class1, but I'd like to be a bit
cleaner than this. What i'd like is for class2 to store a pointer to
class1 when it is initialised, but I'm not 100% sure about how to go
about it. Given this:

namespace myNameSpace
{
public class class1
{
//methods
}

public class class2
{
private class1 myclass1;
//methods
}

Won't that instantiate a new myclass1 when the myclass2 is created?
I've got a huge thick book in front of me, but the problem is I'm not
sure of the terminology i need to look up.

I don't want a class1 created when class2 gets created, is all I'm
worried about, since my application will have one 'master' class1,
because there might be class3, class4, which all use this abstraction
layer.

Any thoughs appreciated.
Thanks
James

Oct 19 '07 #1
3 22987
On Oct 19, 11:51 am, James Booker <james.boo...@gmail.comwrote:

<snip>
public class class2
{
private class1 myclass1;
//methods

}

Won't that instantiate a new myclass1 when the myclass2 is created?
No. It will create a variable which hold a *reference* to an instance
of class1.

Now, you probably want to create a constructor in class2 which takes a
reference as a parameter:

public class2 (class1 foo) // Rename to something meaningful
{
myclass1 = foo;
}

That forces anyone creating an instance of class2 to pass you a class1
reference which you'll then keep and can use later on.

Jon

Oct 19 '07 #2
No. It will create a variable which hold a *reference* to an instance
of class1.
Ah. Thats exactly what I was trying to do.
Now, you probably want to create a constructor in class2 which takes a
reference as a parameter:

public class2 (class1 foo) // Rename to something meaningful
{
myclass1 = foo;

}

That forces anyone creating an instance of class2 to pass you a class1
reference which you'll then keep and can use later on.

Jon
Thanks, Jon, this is the perfect answer

Oct 19 '07 #3
James Booker wrote:
[...]
I don't want a class1 created when class2 gets created, is all I'm
worried about, since my application will have one 'master' class1,
because there might be class3, class4, which all use this abstraction
layer.
In addition to what Jon wrote, based on this last paragraph you wrote I
think you may prefer to implement this as a static class or "singleton".

If as a static class, then you wouldn't use a specific reference to use
the class. You'd just always refer to it by the class name and all the
members would be static as well.

If for some reason the static class doesn't make sense and you really
feel having a class you can instantiate is better, but you still know
you'll always only have one, the singleton pattern might be preferable.
The basic idea is that you have a single instance, accessible via a
static property on the class. A couple of examples:

class class1
{
private static class1 _instance = new class1();

public static class1 Instance
{
get { return _instance; }
}
}

or (initializing the instance more "lazily")

class class1
{
private static class1 _instance;

public static class1 Instance
{
get
{
if (_instance == null)
{
_instance = new class1();
}
return _instance;
}
}
}

Then, anywhere you need this instance, you just access it via
class1.Instance, rather than passing it around or storing it as a
reference inside other instances.

Pete
Oct 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alper Barkmaz | last post: by
3 posts views Thread by Wilfried Mestdagh | last post: by
1 post views Thread by Nick Zdunic | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.