Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

make a setter/getter function:
public string TheText
{
set
{
NewText = value;
}
get
{
return TheText;
}
}
Hope this helps,Chi
They mostly come at night...mostly

"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

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!! :DEl-Trucha
http://www.truchasoft.tk
ftp://tsfc.homeftp.net
hotline://tsfc.ath.cx

![]() |
![]() |
![]() |

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