473,507 Members | 12,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instance Variables

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

}
}
Dec 19 '07 #1
2 1771
On Tue, 18 Dec 2007 16:51:17 -0800, <in*****@hotmail.comwrote:
[...]
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
Dec 19 '07 #2
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
}
}

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 18 Dec 2007 16:51:17 -0800, <in*****@hotmail.comwrote:
[...]
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
Dec 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2504
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
6
22489
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
1766
by: Earl Teigrob | last post by:
I am programming ASP.NET using C#. I have been accessing static variables accross my entire application but now need to change some of the static variables that are session specific to instance...
20
27729
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
12
3089
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
7
1495
by: JonathanB | last post by:
Ok, I know there has to be a way to do this, but my google-fu fails me (once more). I have a class with instance variables (or should they be called attributes, I'm newish to programming and get...
2
1925
by: hvj | last post by:
Does IIS create a separate instance of a webservice for each call to it or if more calls are done at the same time, is it possible that the same instance will be used for more calls? So do I...
2
1769
by: Nikolaus Rath | last post by:
Hello, I am really surprised that I am asking this question on the mailing list, but I really couldn't find it on python.org/doc. Why is there no proper way to protect an instance variable...
45
2960
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
2056
by: Joe Strout | last post by:
On Nov 10, 2008, at 2:44 PM, Zane Selvans wrote: How are you creating your list? You need to make sure that each instance creates its very own list object, something like: self.foo = ...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.