C# class reference to another class?

J

James Booker

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
 
J

Jon Skeet [C# MVP]

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
 
J

James Booker

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
 
P

Peter Duniho

James said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top