Change an instance from another

E

EsCHEr

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]
 
B

Boris

EsCHEr said:
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
 
J

John Wood

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 said:
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]
 
E

EsCHEr

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

BMermuys

Hi,
inline

EsCHEr said:
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]
 
E

EsCHEr

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
 
B

BMermuys

EsCHEr said:
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
 

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