how declare public variables in form?
such as?Code:int empID
Printable View
how declare public variables in form?
such as?Code:int empID
Declare it in the Public Partial Class like so.
Code:namespace WindowsApplication1
{
public partial class Form1 : Form
{
int empID = 1; //Public var to the form
Member variables in classes are private by default, so if you don't specifically declare the variable as public it will be private.Also note that it is considered preferable to make variables private and expose them via public properties..Code:namespace WindowsApplication1
{
public partial class Form1 : Form
{
public int empID = 1; //Public var to the form
That may seem like a lot of trouble but it does have many advantages, plus the IDE will write the code for you if you use the supplied code snippet. Just type prop and hit the Tab key twice.Code:namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int _empID;
public int EmpID
{
get
{
return _empID;
}
set
{
_empID = value;
}
}
Bah! I knew I forgot something :(
Yes, using a property is usually preferred over a public variable in a form.