473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change an instance from another

I created a class

public class testClass
{
public testClass()
{
}
public void changeForm(){
Form1.textBox1.Text = "hello";
}
}

And on Form1 I declared it and I called changeForm
[code:1:c279a06ed5]testClass myTest;
this.myTest = new testClass();
this.myTest.changeForm();[/code:1:c279a06ed5]
And I get this error

error CS0120: An object reference is required for the nonstatic field,
method, or property 'testCallFromClass.Form1.textBox1'
Both classes are in the same namespace

Any Ideas?[code:1:c279a06ed5][/code:1:c279a06ed5]
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
7 1338
EsCHEr wrote:
I created a class

public class testClass
{
public testClass()
{
}
public void changeForm(){
Form1.textBox1.Text = "hello";
}
}

And on Form1 I declared it and I called changeForm
[code:1:c279a06ed5]testClass myTest;
this.myTest = new testClass();
this.myTest.changeForm();[/code:1:c279a06ed5]
And I get this error

error CS0120: An object reference is required for the nonstatic field,
method, or property 'testCallFromClass.Form1.textBox1'
Both classes are in the same namespace


Is Form1 a class or an instance? If textBox1 is nonstatic you can't access
it through class Form1 but need an instance of Form1 (in changeForm).

Boris
Nov 16 '05 #2
> testClass myTest;
this.myTest = new testClass();
this.myTest.changeForm();
You should not prefix local variables with this. Only member variables
should be prefixed with this (if they conflict with parameters or local
variables, otherwise this can be ommitted entirely)/

--
John Wood
EMail: first name, dot, second name at priorganize.com
"EsCHEr" <pa***@killthepixel-dot-com.no-spam.invalid> wrote in message
news:40**********@Usenet.com... I created a class

public class testClass
{
public testClass()
{
}
public void changeForm(){
Form1.textBox1.Text = "hello";
}
}

And on Form1 I declared it and I called changeForm
[code:1:c279a06ed5]testClass myTest;
this.myTest = new testClass();
this.myTest.changeForm();[/code:1:c279a06ed5]
And I get this error

error CS0120: An object reference is required for the nonstatic field,
method, or property 'testCallFromClass.Form1.textBox1'
Both classes are in the same namespace

Any Ideas?[code:1:c279a06ed5][/code:1:c279a06ed5]
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #3
sadly am too much of a newbee to answer correctly, the class is called
Form1 and inside of it has the main function which calls for new
Form1(), but it's not an instance.
I just created a project on vs net 2003 added a text field to the main
form and then created a class with the code I posted earlier.
the text field is defined like this
public System.Windows.Forms.TextBox
textBox1;
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #4
Hi,
inline

"EsCHEr" <pa***@killthepixel-dot-com.no-spam.invalid> wrote in message
news:40**********@Usenet.com...
I created a class

public class testClass
{
public testClass()
{
}
public void changeForm(){
Form1.textBox1.Text = "hello";
}
}

And on Form1 I declared it and I called changeForm
[code:1:c279a06ed5]testClass myTest;
this.myTest = new testClass();
this.myTest.changeForm();[/code:1:c279a06ed5]
And I get this error
In VB6.0 forms are global objects, this is not the case in c#. Form1 is a
class and Main() probely creates an object of it. The reference to that
object is "this" when you're "inside" it.

So before calling the method, you must pass a Form1 object to a TestClass
object, eg. by using the constructor to pass it :

public class TestClass
{
protected Form1 form = null;

public TestClass( Form1 aForm1 )
{
form = aForm1;
}

public void ChangeForm()
{
form.textBox1.Text = "hello";
}
}

public class Form1: form
{
...
protected TestClass tc = null;
...
public Form1()
{ // Constructor
.......
// Creates an instance(object) of TestClass
// and pass our Form1 instance(object).
tc = new TestClass( this );
.......
}
...
void SomeEvent(object sender, Eve... )
{
tc.ChangeForm();
}
}
If the TestClass contains any other logic then UI then you should seperate
them. Add events to TestClass and then let your form decide how it will
change it's UI depending on the event fired.

Or if you want to create multiple forms with the same UI behaviour, lookup
Form Inheritance.
HTH,
greetings

error CS0120: An object reference is required for the nonstatic field,
method, or property 'testCallFromClass.Form1.textBox1'
Both classes are in the same namespace

Any Ideas?[code:1:c279a06ed5][/code:1:c279a06ed5]
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #5
Thanks that helped a lot, am still trying to figure out the events
part, so if am getting this right I can't call a function in a class
from an instance that's created inside of it?

so if I have a function called theFunction inside Form1 and I have an
instance of theClass called myClass I can't invoke theFunction from
myClass?

But I can create an event inside theClass and then on Form1 when I get
that event I can call theFunction?

Thanks in advance
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #6

"EsCHEr" <pa***@killthepixel-dot-com.no-spam.invalid> wrote in message
news:40********@Usenet.com...
Thanks that helped a lot, am still trying to figure out the events
part, so if am getting this right I can't call a function in a class
from an instance that's created inside of it?

so if I have a function called theFunction inside Form1 and I have an
instance of theClass called myClass I can't invoke theFunction from
myClass?
Well, you can if you have an instance(object) of Form1. You should
understand that Form1 is a *class* not an object. There is one
instance(object) of Form1 created (without a variable name) inside Main().

And if you want theClass to call theFunction you must first pass a Form1
instance(object) to theClass.

When Form1 constructor is called or any event, you're inside that one Form1
instance(object) that Main() created so you can pass "this" to the theClass
when you create it.

But I can create an event inside theClass and then on Form1 when I get
that event I can call theFunction?
This is another solution which works too and *ussualy* results in better
design.

HTH,
greetings


Thanks in advance
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #7
Thanks a lot, I'll look into events better learn the good way while am
starting
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #8

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

Similar topics

5
2060
by: Stewart Midwinter | last post by:
I've got an app that creates an object in its main class (it also creates a GUI). My problem is that I need to pass this object, a list, to a dialog that is implemented as a second class. I want...
21
3429
by: Lasher | last post by:
Hi, I have clients using an application that allows users to change their passwords. The application uses the 'ALTER USER xxx IDENTIFIED BY.....' command. What I need to do is use Oracle to...
6
4636
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
0
1477
by: Joe Harrison | last post by:
Hello. I have an application which I check to see if there is another running instance at startup. If there is another running instance, I set focus to the existing instance. I am using code...
3
4309
by: wg | last post by:
I have written a class to contain tags that are loaded from a spreadsheet. I can load the value ok. But from another class I would like to change a value inside the arraylist. Here is my code (see...
8
1872
by: patrizio.trinchini | last post by:
Hi All, I'would like to write an XSL transformation that changes the value of the atribute of a given element according to the value of another atttribute of the same element. For instance,...
1
2646
by: BF | last post by:
Hi, I want the change the SQL listen port of a named 2005 sql instance. Is there a way I can run an sql query against the named instance which gives me back which registry setting I need to...
4
1928
by: Ed | last post by:
Hi, guys, I am wondering if I could change the static variable value in one static function? If I have a class: class Apple { public: static Apple* Instance()
18
7727
by: wizdom | last post by:
Help - change text on click - text has another onclick inside with php variables ---------- I think what I'm trying to do is simple. I have a 2 buttons on a page. 1 button allows a thread of...
0
7072
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
7319
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...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
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,...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
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
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.