Passing a variable to a Form from a Class

A

AMP

Hello,
I have a class that has a calls a method in a form to set some text
on that form:
This is in a Class SerialPortConnection.cs.
I have an object called NewConnection.

I want to pass some text to a form with this:
Form1.UpdateTextBox1(" Port Open\r\n");

Here is my Form Method:
internal static void UpdateTextBox1(string p)
{
textBox1.AppendText(p);
}

How do I do this?.

I am getting this error:
An object reference is required for the nonstatic field, method, or
property 'Bubbler.Form1.textBox1'

I tried this.textBox1.AppendText(p);
I tried Form1.textBox1.AppendText(p);

Nothig is working.
Any Help
Thanks
 
J

Jon Skeet [C# MVP]

AMP said:
I have a class that has a calls a method in a form to set some text
on that form:
This is in a Class SerialPortConnection.cs.
I have an object called NewConnection.

I want to pass some text to a form with this:
Form1.UpdateTextBox1(" Port Open\r\n");

The question is *which* form you want to pass it to. Form1 will be the
name of the *class*. You need to think about which *instance* of Form1
should be updated.
 
A

AMP

The question is *which* form you want to pass it to. Form1 will be the
name of the *class*. You need to think about which *instance* of Form1
should be updated.

Jon,
This is what confuses me.The only time a see a new creation of a form
is this in my main():
Application.Run(new Form1());

I thought the inial form was the name of the class. Form1.Form1. So Im
having a hard time referencing it. I seem to have most of my problems
with forms and "seeing" the controls on them.
So I'm not really sure.After it is created its called "this". which I
understand

I program in several languages, mostly php so I end up with different
crap going through my head at the same time.
I'll do some C# for a few months and then get back to it in a year,but
thats the nature of my job.
Any help is appreciated,
thanks,
Mike
So I'm not really sure.After it is created its called "this". which I
understand.
 
J

Jon Skeet [C# MVP]

AMP said:
This is what confuses me.The only time a see a new creation of a form
is this in my main():
Application.Run(new Form1());

I thought the inial form was the name of the class. Form1.Form1. So Im
having a hard time referencing it. I seem to have most of my problems
with forms and "seeing" the controls on them.
So I'm not really sure.After it is created its called "this". which I
understand

So your other class needs a reference to the new form. Two options:

1) If you have a reference already when you create your other class
(e.g. it's done within Form1) then you can give it "this".

2) Just because Application.Run is often written in that style doesn't
mean it *has* to be:

Form1 form = new Form1();
// Do some other work to initialize other stuff, using "form".
// ...
Application.Run(form);
I program in several languages, mostly php so I end up with different
crap going through my head at the same time.
I'll do some C# for a few months and then get back to it in a year,but
thats the nature of my job.
Any help is appreciated,
thanks,
Mike
So I'm not really sure.After it is created its called "this". which I
understand.

It's only "this" within Form1. "this" is always a reference to the
"current object" for instance methods. It's not like it's the name of
the object - just a way of getting at the instance you're using when
you're executing an instance method.
 
A

AMP

So your other class needs a reference to the new form. Two options:

1) If you have a reference already when you create your other class
(e.g. it's done within Form1) then you can give it "this".

2) Just because Application.Run is often written in that style doesn't
mean it *has* to be:

Form1 form = new Form1();
// Do some other work to initialize other stuff, using "form".
// ...
Application.Run(form);


It's only "this" within Form1. "this" is always a reference to the
"current object" for instance methods. It's not like it's the name of
the object - just a way of getting at the instance you're using when
you're executing an instance method.

Jon,
I am going to look into this.
I just have 1 more question for today.
The only time you can see the names of the Instances in the local
window is right before they get created. How can you keep track of
them in the IDE all the time?(a watch?)
Thanks
 
J

Jon Skeet [C# MVP]

I am going to look into this.
I just have 1 more question for today.
The only time you can see the names of the Instances in the local
window is right before they get created. How can you keep track of
them in the IDE all the time?(a watch?)

Make your methods short, and give your classes clear separation of
concerns - that way it should be obvious what's available.
 
A

AMP

Make your methods short, and give your classes clear separation of
concerns - that way it should be obvious what's available.

I *think* I get this:
1. I order for one object to see another, you MUST pass a reference of
Object1 to Object 2 during Instance creation?And at no other time.
Exactly like a function needs the parameters passed to it?Is this
correct?
2. So now Object2 can see Object1, but we still have the fact that
Object1 needs to see Object2. How?
I think if I can get this, my biggest hurdles will be overcome.
Thanks
Mike
 
J

Jon Skeet [C# MVP]

AMP said:
I *think* I get this:
1. I order for one object to see another, you MUST pass a reference of
Object1 to Object 2 during Instance creation?And at no other time.

No, you could set properties later if you wanted.
Exactly like a function needs the parameters passed to it?Is this
correct?

Well, you need to have some way of getting to the data, yes.
2. So now Object2 can see Object1, but we still have the fact that
Object1 needs to see Object2. How?
I think if I can get this, my biggest hurdles will be overcome.

Setting properties could be the easiest way here. Or you could try to
avoid needing the cyclic reference in the first place.
 
A

AMP

No, you could set properties later if you wanted.


Well, you need to have some way of getting to the data, yes.


Setting properties could be the easiest way here. Or you could try to
avoid needing the cyclic reference in the first place.

"No, you could set properties later if you wanted. "
How, not how to write a property, but how to write a property for an
object it doesnt know about?
If you could point me to a simple example online,that has 2 Objects
created and aware at each other using properties, that I could run in
my IDE,I would truly appreciate it.Here is my E-mail:
(e-mail address removed)
Thanks
Mike
 
J

Jon Skeet [C# MVP]

"No, you could set properties later if you wanted. "
How, not how to write a property, but how to write a property for an
object it doesnt know about?

What do you mean? Do you not know what type the reference will be? If
so, declare the property to be of type Object. However, that's not
going to be terribly useful to you later on. I'd expect you to know at
least *something* about the reference that will be stored in the
property - such as that it's a Control, or a Form.
If you could point me to a simple example online,that has 2 Objects
created and aware at each other using properties, that I could run in
my IDE,I would truly appreciate it.Here is my E-mail:

Rather than use online examples and the like, I really think it would
be better to hit the books. Read a good C# book from the start - it's
likely to help a lot more than fixing one isolated problem of
understanding.
 

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