如何在类中访问变量的值 [英] How to access variable's value in class
本文介绍了如何在类中访问变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发Windows窗体应用程序:
i已在我的表单中声明一个公共变量...
但是我想在课堂上访问该变量的价值..
提前完成。
i am developing Windows Form application :
i have declaire one public variable in my Form...
but i want to access that variable's value in class..
Thanx in advance.
推荐答案
如果您可以访问表单的实例,例如Form1,则可以访问公共变量,例如
,这样:myvar
A public variable, saymyvar
, is accessible if you can access an instance of the form, sayForm1
, this way:
Form1.myvar
那就是说,它是以某种方式暴露成员变量并不是一个好主意。正如其他人所建议的那样,你应该学习一本关于面向对象编程的好书(可能还有 C#
)
using System;
using System.Windows.Forms;
class form1:Form{
public string variablename=""; // here you are declaring a variable
//
here your code
//
}
First Create a object of that form class and using that object you can call a variable
Form1 form=new Form1();
string str=form.variablename; // here we can call that variable using object of that class and store it into another variable
这篇关于如何在类中访问变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文