By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

C# class property of another class type

P: 3
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
Share this Question
Share on Google+
5 Replies


Sidewinder2
P: 7
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

P: 3
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

P: 1
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

P: 3
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
Expert 100+
P: 229
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

Post your reply

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