• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Easy C# class question

AgentZap

Senior member
How does one access objects using functions in a class you define in a .cs file that is seperate from the form whose objects you want to modify?

Example:
I have MyForm.cs with private myTextBox in it.
I also have MyClass.cs which has the definition of MyClass.

They both exist in the same namespace.

If I want to write a function in MyClass that changes the text in myTextBox how would I do that?

Of course, right now it is inaccessible due to its protection level. I did this once a long time ago, but can't remember. I think it either involved sending like a pointer to a form or a friend declaration or something.

Please help. Thanks.
 
First off, this sounds like a pretty bad idea 😛 Without knowing exactly what you're doing, my quickest, non-hideous suggestion would be to add a method to the form called DisplayMyText(string) or something like that, call it from the other class and do the text setting in the method on the form class.

If you do really want to access the control directly and you're using visual studio, controls have properties called "GenerateMember" and "Modifiers" in the "Design" category. Make sure GenerateMember is true and Modifiers is Public or Internal or something like that. Then you can just do MyFormInstance.myTextBox.Text = "Ha Ha!" Take a peak in MyForm.Designer.cs to see exactly what's going on.
 
If something is declared 'private' then it cannot be accessed directly from outside of that class. (C# has no friends.)

Option 1a)
You could either make the textbox public (or internal), in which case you could access it directly from the other class.

Option 1b)
Or you could add a public property to MyForm, which could then be accessed directly from the other class. See attached code.

Option 2)
myForm passes it's myTextBox object to myClass. myClass now has direct access and can set the text directly. See attached code.
 
Originally posted by: Mark R
Option 2)
myForm passes it's myTextBox object to myClass. myClass now has direct access and can set the text directly. See attached code.
No offense, but that's a way worse idea even than accessing the text box as a public member of the form. Doesn't anybody understand the concept of encapsulation?
 
So that's how its normally handled? I mean how to developers keep classes organized into seperate files? It seemed kind of easier to invoke classes stored in headers and whatnot in C++.
 
I mean how to developers keep classes organized into seperate files? It seemed kind of easier to invoke classes stored in headers and whatnot in C++.

In C# you just put each class in a seperate file. No need (nor is it always desirable) to put multiple classes in one file.

Any code can invoke any other code within a namespace, subject to the protection level. There's no need for header files, because any source file can automatically 'see' all the other source files at compile time.

A similar thing happens with class libraries. The compiler doesn't need header files - it can get all the information it needs from the library binary.

Although my code example was written as one single contiguous piece of code - there's no reason why it couldn't have been split into 1 (or more) source file per class.
 
Back
Top