Click here to Skip to main content
15,885,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

In Class1:
C#
public class class1
   {
   ...
   public string UserName { get; set; }
   ...
   }

in your form (or whatever) class:
C#
class1 oc1 = new class1();
oc1.UserName = "Jone";
...
Console.WriteLine(oc1.UserName);
 
Share this answer
 
C#
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;
 
Share this answer
 
v4
C#
public class Class1
   {
      
    public string _CustomerName = "Jone";
    public string CustomerName
    {
        get { return _CustomerName; }
        set { _CustomerName = value; }
    }
   }




Now from another class

C#
public class Class2
   {
      
      Class1 cls1=new Class1();
      string CustomerName= cls1.CustomerName;
      Document.Write(CustomerName);
   }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900