I'm relatively new to C#, having migrated over from C++, so I'm a little confused about the use of variables.

I have a class called User located in the program's main namespace

// In User.cs

namespace TestProgram 
{
     class User
     {
          public string name;
          public int id;
          
          public User()
          {
              name = "John Doe";
              id = 0;
          }
          public void setName(string nName)   // I know these two can be 
          {                                   // turned into C# style
              name = nName;                   // with get and set
          }                                   // but one step at a time...
          public string getName()
          {
              return name;
          }
      }
}

In the main file Program.cs, under the Main() function in the same namespace, I've got

User.mainUser = new User();
// This works fine and produces no errors.
// I can mess with the mainUser object just fine as long as I do so within Main()

Now using VS 2005's form generator I've put in a label, a textBox, and a button. I'd like it to, when the button is clicked, take textBox1.Text's value, change mainUser's name to that value, and then change the label to the name as well. All that is in Form1.cs as generated by VS.

So my problem/question is basically that the button1_Click(...) function, which is located in the partial class Form1 in the same namespace TestProgram, cannot find that declared mainUser and therefore cannot get/set any of its values.

// In Form1.cs
// namespace TestProgram -> public partial class Form1 : Form -> private void button1_Click
private void button1_Click(object sender, EventArgs e)
{
    if(textBox1.Text != "")
    {
       mainUser.setName(textBox1.Text);   // doesn't work
       label1.Text = mainUser.getName();  // also doesn't work
    }
}
// One error for each mainUser entry:
// "The name 'mainUser' does not exist in the current context."

So how do I pass that mainUser object to Form1's button1_Click(...) or any Form components?

Is that User.mainuser or User mainuser?

Create the user as a private variable inside of the form.

Oh, that's User mainUser. Sorry about that.

Long as you declare the main user as a private varaible (or public if you need to for later) within form so it goes

partiial class Form1 
{
  private User mainuser;
 // more stuff
}

You can access it form any of the form1's procedures, just like you did with c++

In your User class however, you have something which can bite you later, which is you declared your variables publically as well as then (as you said)_half set properties for them, whats the point if the variable can be used as you showed in your other code as its public, you should make the name private, as per your naming convention suggests

hi...
Create the object of your user class in button click and manipulate the data of the object as you wish...

However to create global variables in .net platform you can use web.config file which will be available in ASP.NET type of applications.. You can refer the global variables any where in your application..

hi...
Create the object of your user class in button click and manipulate the data of the object as you wish...

That would work fine if the object was solely localised to the button click area. I'd like to have background logic and object manipulation and use the user interface solely as a frontend.

Long as you declare the main user as a private varaible (or public if you need to for later) within form so it goes

partiial class Form1 
{
  private User mainuser;
 // more stuff
}

You can access it form any of the form1's procedures, just like you did with c++

The problem lies in that mainUser is declared in the main function, not in the Form function. I'd like manipulate that instance of mainUser. So I guess the question should be is there a way to have a specific instance of an object be available throughout the whole namespace or at least be passed from between "static void Main()" in Program.cs to "public partial class Form1 : Form" in Form1.cs?

A C++ example would be having two source files, main.cpp and users.cpp. In int main() I declare User mainUser = new User(). In users.cpp, since I would like to use mainUser, I'd just use "extern mainUser" to bring it in to the entire source file. Not exactly the best programming habit but the easiest way to describe what I'd like to do here. Hell, I'd even like to just be able to pass mainUser to a function in users.cpp.

I really hate to bump threads, but it just seems like there should be a simple solution to this. Once again the problem is basically that I have two source files: Form1.cs and Program.cs. In Program.cs I have created an instance of object User called mainUser. I would like to modify some of mainUser's values using a button whose source is located within Form1.cs. How can I modify/use mainUser in Form1.cs when mainUser is created in Program.cs? Please note that creating mainUser in Form1.cs doesn't solve this problem because that would essentially mean that all projects would have to be in one source file.

For C++ users: I'm basically looking for a similar method to "extern [object]".

If user were a static class you could access it from anywhere

If user were a static class you could access it from anywhere

I think you misunderstand. I can declare a new User object anywhere, but I simply cannot change the values of a User object that has been declared in another source file. Like this:

User.cs // contains the User class definition
Program.cs // Contains main(). Here is "User mainUser = new User("John Doe")"
   -- I now have a successfully created User called mainUser. It can  
   -- be modified from within Program.cs
Form1.cs // In here is the form source.  In the source is a button's Click event.
   --> button1_Click(object sender, EventArgs e) // the button's Click event.
         --> mainUser.setName(textBox1.Text);  // this is what it should do when clicked.

The result of this setup is a compiler error and the message "The name 'mainUser' does not exists in this context" in Form1.cs.

There just HAS to be a way to do all this. Otherwise every single object ever created would have to be created within the source file for a single Form...

Do you ever have more than 1 user variable defined?

Do you ever have more than 1 user variable defined?

Not in this program, no. It's a simple "let's test out C# to see how it's like" program. In the future, were I to create more complicated programs, there might be more than one user. But in this program the one simple thing I need is to be able to use User mainUser from Program.cs in Form1.cs.

Then I dont understand why you cant have a static class which is availble then throughout all the application.

Then I dont understand why you cant have a static class which is availble then throughout all the application.

Sorry if I'm wrong as I am new to C#, but doesn't static mean that the members of the class could never be changed? The purpose of my program here is that I click the button to change the "name" value in the class.

No. Static means you cant make instances, its created at the start, and dies at the end, but like "Colors.White" is available everywhere.

No. Static means you cant make instances, its created at the start, and dies at the end, but like "Colors.White" is available everywhere.

Alright, I guess that is what I need for the mainUser problem, thanks. Now, if I were going to create some sort of user entry program, in which the several fields could be filled in and a new user is created, would there be any way to modify those newly created instances somewhere else in the program, or is that problem complicated enough to warrant a new topic when I come to it?

Try this - just to prove to yourself how it can work.
Create yourself a new app
on your first form drop a button, textbox and a label
Create a second form (its going to need EXACTLY the same code as form 1, but it will prove a point)
drop another button, textbox and label
Create a new class
set the class call it keepsafe to static
put a private static string of value
put a public static property to read/set Value
in form 1 button click if textbox is empty, take content of keepsafe.Value in label
do the same for form 2
in form 1 button click if textbox isnt empty, set keepsafe.Value to the value of the text box
do the same for form2
in your form1 Load event create a copy of form2 you dont need to keep a reference so dont worry about form level variables..

Now

Run your app

enter text in form 1, see when you click the button on form2 it shows, same if you do it the other way round..

note you also NEVER made an instance of keepsafe.. it was always there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.