Click here to Skip to main content
12,512,130 members (58,714 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Say I have 2 class
class1, class2

in class1
I have assigned a variable value like UserName="Jone"

from class2
I want to call the variable value UserName as property
object Name of class1 is cls1
string name=cls1.UserName
when i print name then it will show Jone
How can i do this
Posted 25-Jan-13 23:41pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

In Class1:
public class class1
   {
   ...
   public string UserName { get; set; }
   ...
   }
in your form (or whatever) class:
class1 oc1 = new class1();
oc1.UserName = "Jone";
...
Console.WriteLine(oc1.UserName);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

public class Global
{
      public static int global_var1 = 3;
      public static int global_var2 = 5;
}
 
//Now in order to access global_var from anywhere in your program, use the following syntax:
int local_var1 = 0;
 
local_var1 = Global.global_var1;
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

public class Class1
   {
      
    public string _CustomerName = "Jone";
    public string CustomerName
    {
        get { return _CustomerName; }
        set { _CustomerName = value; }
    }
   }


Now from another class

public class Class2
   {
      
      Class1 cls1=new Class1();
      string CustomerName= cls1.CustomerName;
      Document.Write(CustomerName);
   }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100