Computing.Net > Forums > Programming > Assigning a value to a control (C#)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Assigning a value to a control (C#)

Reply to Message Icon

Name: El-Trucha
Date: April 16, 2005 at 11:35:25 Pacific
OS: Windows XP SP2
CPU/Ram: 2.8 GHz/448 MB
Comment:

Hello every1!! :D
OK, I need to assign a value to a control (created with VS.NET) from an external class in another file...
I've tried making the controls static, but with that I get a huge load of compiler errors, mainly all complaining about not being able to make instrances of them...
I made a button_Set(string newText) method inside the control's class so then I can access it from outside to change the control's value, but I need an object reference from it, and if I do it static, I can't access the controls because they're nonstatic!! :@
What can I do here??
Why does C# have to be so complicated with all this object referencing stuff?? ARGH!! :@
But, thanx!! ;)

El-Trucha
http://www.truchasoft.tk
ftp://tsfc.homeftp.net
hotline://tsfc.ath.cx



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: April 18, 2005 at 06:56:56 Pacific
Reply:

make a setter/getter function:

public string TheText
{
    set
    {
        NewText = value;
    }
    get
    {
        return TheText;
    }
}
     


Hope this helps,

Chi


They mostly come at night...mostly


0

Response Number 2
Name: SN
Date: April 18, 2005 at 07:30:52 Pacific
Reply:

"I've tried making the controls static"
Controls are almost never static... Controls are objects that belong to a form, and typically they will need access to other form elements (like a close button can't be static because it needs access to the form object to close it!). A static method or object can only access items that are internal to it (like local variables) or other static methods or objects.

"Why does C# have to be so complicated with all this object referencing stuff??"
I hate to be the bearer of bad news, but nearly every language is like this...Thank goodness, because if they allowed what you're trying to do, it would make things 10 times more complicated.

Now on to your problem:
"OK, I need to assign a value to a control (created with VS.NET) from an external class in another file..."
First off, this sounds fishy...Forms should be as self-contained as you can possibly make them, and usually you would communicate between forms using events or messages rather than setting text directly. But there's no rule against what you're trying to do, so we'll keep going.

"so then I can access it from outside to change the control's value, but I need an object reference from it"
Of course you need an object reference to it...What if you had three of these controls in your application and this class just did something like ControlTypeName.Text = "howdy"...How would C# know which of the three it should change? You need to provide access to the value you're trying to change via a method or a get/set property like Chi's example.

So what you probably need to do is have a member variable of your class be a reference to the control.

-SN


0

Response Number 3
Name: El-Trucha
Date: April 18, 2005 at 16:04:35 Pacific
Reply:

OK, I did something different...I changed the Main() method to another class, and then did:
Form1 myForm = new Form1();
(I actually waited for the IntelliSense to come up... XD )
And that worked...but I think the set/get thing is easier...so, where did the value variable come from?? shouldn't it be in the method header?? I don't get it, sorry...can u explain?? ;)
Thanx both of u!! :D

El-Trucha
http://www.truchasoft.tk
ftp://tsfc.homeftp.net
hotline://tsfc.ath.cx


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Assigning a value to a control (C#)

Assigning values to radio buttons? www.computing.net/answers/programming/assigning-values-to-radio-buttons/14344.html

Assign Reg key to a batch variable www.computing.net/answers/programming/assign-reg-key-to-a-batch-variable/15391.html

DOS - File Read for environment var www.computing.net/answers/programming/dos-file-read-for-environment-var/17776.html