Resolved Call tool from another Class

Rabi

Member
Joined
Mar 22, 2022
Messages
17
Programming Experience
Beginner
I creat Form1 and put Tools on it like TextBox. I want to call this TextBox1 from another class.
In the VB i used this way :
class Form1.TextBox1
 
You would do the same thing in C#. Just keep in mind that C# doesn't create a default instance for you automatically.

In general, though, it is a poor object oriented programming practice (regardless of what language you are using) for one object to know about the implementation of another object. In your case above, you shouldn't be exposing the TextBox1 of Form1. Instead you should be exposing the value stored in the TextBox. For example FirstName as a string. This gives you the freedom to replace the TextBox with a RichTextBox or a ComboBox and the users of your class wouldn't have to change their code, or even know that you changed controls.
 
Last edited:
I tried this way to call from class to another class.
I creat class1 and do this in thus class
C#:
using system.windows.Forms;
 public void mesb()
{
MessageBox.Show("ok");
}
Then write this in Form1 load

C#:
Class1 clas = new Class1
clas.mesb();
But it showed me this error under clas :
A new expression requiers (), [], or {} after type
 
Last edited by a moderator:
The error is correct. You need the parenthesis. This is C#, not C++ nor VB.
C#:
Class1 clas = new Class1();
 

Latest posts

Back
Top Bottom