将变量值设置为对象属性

将变量值设置为对象属性

本文介绍了将变量值设置为对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有2个班级

class1,class2


$ 1 $ b在班级1

我已经分配了一个变量值类似UserName =Jone



来自class2

我想将变量值UserName称为属性

对象class1的名称是cls1

字符串名称= cls1.UserName

当我打印名称然后它将显示Jone

我该怎么办?这个

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

推荐答案

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);


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;


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);
   }


这篇关于将变量值设置为对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:47