PDA

Click to See Complete Forum and Search --> : [2.0] declaring public variable?


arshesander
Jul 24th, 2007, 01:53 AM
how declare public variables in form?

such as int empID?

RobDog888
Jul 24th, 2007, 02:00 AM
Declare it in the Public Partial Class like so.

namespace WindowsApplication1
{
public partial class Form1 : Form
{
int empID = 1; //Public var to the form

jmcilhinney
Jul 24th, 2007, 02:06 AM
Member variables in classes are private by default, so if you don't specifically declare the variable as public it will be private.namespace WindowsApplication1
{
public partial class Form1 : Form
{
public int empID = 1; //Public var to the form Also note that it is considered preferable to make variables private and expose them via public properties..namespace WindowsApplication1
{
public partial class Form1 : Form
{
private int _empID;

public int EmpID
{
get
{
return _empID;
}
set
{
_empID = value;
}
}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.

RobDog888
Jul 24th, 2007, 02:14 AM
Bah! I knew I forgot something :(

Yes, using a property is usually preferred over a public variable in a form.