Instance Variables

I

inadsad

Good Day Group,
Recently I started a small test project in C# VS 05 for learning
purposes. I have a class and a form. I defined a property in a class
with get/set and create an instance of class1 on form.

The part I'm confused at is when trying to access Name property
outside the click event it wouldn't work. Why is that? Should I
create an instance after the InitializeComponent? and move all
constants & variables to class1. I'd appreciate some feedback.

Thanks
Ian

namespace WindowsApplication1
{
public partial class Form1 : Form
{
Class1 Testc = new Class1(); // create a new instance
string sCompany = "";
const string sDATABASE;
const string sUSERID;


Testc.???? //>> cann't access property Name here
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Testc.Name = "abc"; //>> can access property Name here
}
}

class Class1
{
private String name;
public String Name
{
get
{
return _name;
}

set
{
name = value;
}
}

}
}
 
P

Peter Duniho

[...]
public partial class Form1 : Form
{
Class1 Testc = new Class1(); // create a new instance
string sCompany = "";
const string sDATABASE;
const string sUSERID;


Testc.???? //>> cann't access property Name here
...
}

And what exactly did you expect to be able to do there?

Any code statements in your class has to go inside an actual code block.
That would be inside method declarations, whether for the class itself,
getters and setters and indexers, anonymous methods assigned to class
members, etc. You can't just stick some code anywhere you like. Outside
of code blocks, all you can have are declarations and optionally some
initializer for the thing being declared (if applicable.

You can still initialize the instance variable at the declaration. But if
you want to do something with the property, you have to do that somewhere
that you're allowed to write non-declarative code (the constructor would
be a natural place to do that).

Pete
 
B

bryan

In other words :

namespace WindowsApplication1
{
public partial class Form1 : Form
{
Class1 Testc = new Class1(); // create a new instance
string sCompany = "";
const string sDATABASE;
const string sUSERID;


//Testc.???? //>> cann't access property Name here
public Form1()
{
Testc.Name = "unknown";// you CAN set it here...
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Testc.Name = "abc"; //>> can access property Name here
}
}

[...]
public partial class Form1 : Form
{
Class1 Testc = new Class1(); // create a new instance
string sCompany = "";
const string sDATABASE;
const string sUSERID;


Testc.???? //>> cann't access property Name here
...
}

And what exactly did you expect to be able to do there?

Any code statements in your class has to go inside an actual code block.
That would be inside method declarations, whether for the class itself,
getters and setters and indexers, anonymous methods assigned to class
members, etc. You can't just stick some code anywhere you like. Outside
of code blocks, all you can have are declarations and optionally some
initializer for the thing being declared (if applicable.

You can still initialize the instance variable at the declaration. But if
you want to do something with the property, you have to do that somewhere
that you're allowed to write non-declarative code (the constructor would
be a natural place to do that).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top