473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 25800
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alper Barkmaz | last post by:
Hello; While examining Microsoft's Framework Class Tree, i saw many classes under another class; for example treeviewnode class appears to be under treeview class. I wonder why it is used, it...
11
by: Bob Rock | last post by:
Hello, I'd like to be able to allow instanciation of a class (class Class_A) only from another class method (class Class_B). And I'd like to write the necessary code to enforce this behavior...
3
by: Wilfried Mestdagh | last post by:
Hi, When do you put a class in another class and when not ? It seems in most case both work fine, but in some case not. I'm not yet smart enough in dotnet to know when and wy. Can someone...
1
by: Nick Zdunic | last post by:
In 1.1 it was possible to refer to a web page class from another class I've just started in v2.0 and found that this code sn't working The web page class: Partial Class _Default Inherits...
4
by: psbasha | last post by:
Hi, How to declare a Class inside another class as avariable? For Example" Class X{ MethodX1();
1
by: madshov | last post by:
Hi, I have made a matrix class, that I wish to make an instiation of in another class. Matrix class: #include <vector> #include <iostream> using namespace std; // So the program can see cout...
4
by: lathamoulali | last post by:
In C# Windows Application, How to access the objects of one class (FirstClass.cs file) in another class (SecondClass.cs) file.. I have application object in a class FirstClass.cs file , i want to...
8
by: hoopy | last post by:
Hi, Im just learning OO in PHP and not sure the best method to use the functions for example in a database wrapper class within another class for example which handles all user authentication. I...
20
EARNEST
by: EARNEST | last post by:
Hello guys, My program would allow a user to tweak its calculations engine via certain controls: radio buttons, check boxes etc. The problem is, based on such settings, appropriate events would...
0
emaghero
by: emaghero | last post by:
I am trying to use the attached header file, which provides me with a generic way of declarating arrays and matrices of arbitrary size. I would like to be able to use the classes Array and Matrix...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.