Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2008

Tip: Looking for answers? Try searching our database.

Passing a variable to a Form from a Class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 18 Mar 2008 17:02 GMT
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
Jon Skeet [C# MVP] - 18 Mar 2008 17:15 GMT
> I have a class that has a calls a method in  a form to set some text
> on that form:
[quoted text clipped - 3 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 18 Mar 2008 17:34 GMT
> > I have a class that has a calls a method in  a form to set some text
> > on that form:
[quoted text clipped - 11 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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.
Jon Skeet [C# MVP] - 18 Mar 2008 17:56 GMT
> This is what confuses me.The only time a see a new creation of a form
> is this in my main():
[quoted text clipped - 5 lines]
> 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.
[quoted text clipped - 5 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 18 Mar 2008 20:15 GMT
> > This is what confuses me.The only time a see a new creation of a form
> > is this in my main():
[quoted text clipped - 37 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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
Jon Skeet [C# MVP] - 18 Mar 2008 21:00 GMT
<snip>

> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 19 Mar 2008 13:00 GMT
> <snip>
>
[quoted text clipped - 10 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

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
Jon Skeet [C# MVP] - 19 Mar 2008 13:26 GMT
> > 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.

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

AMP - 19 Mar 2008 15:41 GMT
> > > Make your methods short, and give your classes clear separation of
> > > concerns - that way it should be obvious what's available.
[quoted text clipped - 20 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

"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:
mpeloso@princeton.edu
Thanks
Mike
Jon Skeet [C# MVP] - 19 Mar 2008 15:51 GMT
<snip>

> "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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.