TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

C# class reference to another class?

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 19-10-2007, 04:31 PM   #1
James Booker
Guest
 
Posts: n/a
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

  Reply With Quote
Old 19-10-2007, 04:31 PM   #2
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: C# class reference to another class?

On Oct 19, 11:51 am, James Booker <james.boo...@gmail.com> wrote:

<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

  Reply With Quote
Old 19-10-2007, 10:32 PM   #3
James Booker
Guest
 
Posts: n/a
Re: C# class reference to another class?

> 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

  Reply With Quote
Old 19-10-2007, 10:32 PM   #4
Peter Duniho
Guest
 
Posts: n/a
Re: C# class reference to another class?

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
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 03:19 AM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional