![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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 |
|
|
|
#2 |
|
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 |
|
|
|
#3 |
|
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 |
|
|
|
#4 |
|
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 |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |