Reputation: 7
I have form1
, class1
and form2
.
What I'm trying to do is to get a text from a textbox in form1
and save it to class1
and then again copy it to form2
from class1
.
Let me describe via code:
class1.cs
public string username;
form1.cs
class1 user = new class1();
user.username = textbox1.text;
form2.cs
class1 user = new class1();
label1.text = user.username;
The problem is: When I try to call username
variable in form2
it returns blank. It just doesn't work. I dont know what I am missing.
Upvotes: 0
Views: 43
Reputation: 408
Make class 1 static. You will be able to call the class from any other instantiated objects, increment (or change) your variable and then retrieve it from any other objects. The synchronized part is something that stops two objects from accessing the incrementCounter() method at the same time. Something like this:
/* This class is thread-safe */
public final class CountHits {
private static int counter;
private static final Object lock = new Object();
public void incrementCounter() {
synchronized (lock) {
counter++;
}
}
public int getCounter() {
return counter;
}
}
Upvotes: 0
Reputation: 517
If you instantiate a new class1 in form 2, you won't never get your username in label1.
Upvotes: 1
Reputation: 3506
The user
in form2 is not the same one that in form1. So in form1 your user
including a name, and in form2 it including an initialize value (blank). You can pass this user
from form1 to form2 by Session or Cookies.
Upvotes: 0