473,503 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# class property of another class type

3 New Member
Hi Friends,
I'm a new comer to the C# and oop world.
I have a little problem about the class properties please kindly help me.

I have a class named "User" in namespace_A . (User is not a static class)

Now I'm going to write a class to the namespace_B and I need to assign some value to a User object. so I do the following

Expand|Select|Wrap|Line Numbers
  1. using namespace_A;
  2. namespace namespace_B
  3. {
  4.    public class MyNewClass
  5.       {
  6.       //class field
  7.       private User _user;
  8.  
  9.       //property
  10.       public User UserProperty
  11.       {
  12.        get{ return _user; }
  13.        set{ _user = value;}
  14.       }
  15.  
  16.       //A function
  17.       public void MyFunction()
  18.       {
  19.          //Here I need to set a value to the User class UserID Property
  20.          //I could do it using 
  21.         UserProperty.UserID = 1;
  22.         // *** but why the compiler didn't tell me to create an object first before
  23.         // accessing the properties of the User class.
  24.       }
  25.  
  26.       }
  27. }
Sep 12 '08 #1
5 11777
Sidewinder2
7 New Member
hi,

could you post ur USER class

i am sure that we have to create an instance of that USER class if it is not a static class and after that only we can able to access the property.

u can see an example in the link below!,

http://msdn.microsoft.com/en-us/library/k69wcs43.aspx
Sep 13 '08 #2
Alavi
3 New Member
Thank you for the reply friend
This is my code

I have "User" class in the Electronics.Common Namespace

This is the User Class :

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Electronics.Common
  6. {
  7.     public class User
  8.     {
  9.         private int _userid;
  10.         private string _username;
  11.  
  12.         public int UserID
  13.         {
  14.             get { return _userid; }
  15.             set { _userid = value; }
  16.         }
  17.  
  18.         public string UserName
  19.         {
  20.             get { return _username; }
  21.             set { _username = value; }
  22.         }
  23.  
  24.         public User()
  25.         {
  26.  
  27.         }
  28.     }
  29. }
I have "UserInsertData" class in the Electronics.DataAccess Namespace. I have added a assembly reference to the common namespace

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Electronics.Common;
  5. namespace Electronics.DataAccess
  6. {
  7.     public class UserInsertData
  8.     {
  9.         private User _user;
  10.  
  11.         public User User
  12.         {
  13.             get { return _user; }
  14.             set { _user = value; }
  15.         }
  16.  
  17.         public UserInsertData()
  18.         {
  19.         }
  20.  
  21.         public void Add()
  22.         {
  23.             User.UserID = 1;
  24.         }
  25.  
  26.     }
  27. }
I could build above solution without errors. Please help me, I'm confused about this.
As I know so far,If we want to access a property or function of a class. First We need to create a object of that class.
But in the UserInsertData class I haven't create a object of "User" type using the new operator.
I have just used a property of that type.
But the intellisense shows me the properties and functions of the User class.
so here I have set a value to the UserID withour errors.
What is the logic behind this?
Sep 13 '08 #3
dumiduw
1 New Member
In n tier architechture please don't come to a conclution by looking at one class.
your class should be related with some outside class. As I see the User object, you are talking about is created inside some other class and set the property of the class you are showing. That's why this class could access the propeties of the User class without creating a object. Please check and see.
Sep 13 '08 #4
Alavi
3 New Member
Thanks dumiduw for the kind advice. A class in the presentation layer create the User object. Thank you so much.
Sep 13 '08 #5
vekipeki
229 Recognized Expert New Member
You should note that the purpose of using properties instead of fields is to hide your implementation from your interface. This is why compiler will not complain about accessing your properties, as long as you are using the correct syntax.

In other words, you are telling to the user of your library that your User property will return an instance of User class, but you are giving yourself an opportunity to change the inner implementation at any time.

You can instantiate your User property in a different class calling your class, or you can instantiate the _user field in your constructor, or you can do something like this:

Expand|Select|Wrap|Line Numbers
  1.       public User User
  2.       {
  3.        get
  4.        {
  5.             // this approach is called "lazy-init", 
  6.             // because User object will not be instantaiated
  7.             // before someone tries to access your UserProperty
  8.             if (_user == null)
  9.                 _user = new User(); 
  10.  
  11.             return _user;
  12.        }
  13.       }
Note that the Setter is now omitted - you are guaranteeing that your UserProperty will be instantiated by your class, and external methods can not change it. So, you can still write:

Expand|Select|Wrap|Line Numbers
  1.       User.UserID = 1; // you are Getting your user object and changing its UserID property
but you can not write:

Expand|Select|Wrap|Line Numbers
  1.       User = new User(); // this will not compile, because you are trying to Set your property
By leaving only the Getter for your property, you can be sure that nobody will ever forget to instantiate your User property before using it - you are guaranteeing that you will do all the necessary initialization.
Sep 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2687
by: George Sakkis | last post by:
I'm trying to write a decorator similar to property, with the difference that it applies to the defining class (and its subclasses) instead of its instances. This would provide, among others, a way...
4
2118
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
5
4921
by: M O J O | last post by:
Hi, I want to expose a enum from another class, is that possible and how? Here's an example Public Class ClassMaster Public Enum Colors
9
2207
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
6
1433
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
5
1945
by: Rob | last post by:
In many articles related to VB.net the word "class" is used... How many meanings are there to this word ? "possible to derived a class from another" "forms are full-fledged classes" "base...
3
2031
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
26
5333
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
20
4002
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
7
1726
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
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
7278
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,...
1
6988
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
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5011
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.