how to return a value from main form to a differnt form or class(C#)
For some unknown reasons this silly thing cant be implemented.
I have an int count
in the main form which I want to return to another class or form.
namespace my_speller
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
int count;
private void btnlogin_MouseUp(object sender, MouseEventArgs e)
{
dbaccess obj = new dbaccess();
for (int i = 0; i < 10; i++)
{
if (txtusername.Text == obj.Usersusername()[i])
{
count = i;
break;
}
}
}
public int namecount()
{
return count;
}
}
}
dbaccess
is another class and I could successfully call a function (Usersusername) defined in that class to my login
form. Everything works fine up to this. Now I want to get the int count
from main form back to dbaccess
class. So I implemented a public function namecount
to return count
. But count
is always zero in the other class. In the main form, I开发者_开发百科 get the value of count
correctly (which is i
). But nothing gets returned when I call from dbaccess
class this way:
login obj = new login();
// do stuff
or from another form in the same program, like this:
namespace my_speller
{
public partial class student : Form
{
public student()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
login obj = new login();
MessageBox.Show(obj.namecount().ToString());
}
The messagebox here should display count
which is some number, but what's displayed is zero. What could possibly be the cause??
The same thing happens when I'm trying to return a string from my main form. It's always null in other classes :(
Thanks in advance
Edit: Can you give the code snippet itself. I cant know the technical terms you might use to help me
private void button3_Click(object sender, EventArgs e)
{
login obj = new login();
MessageBox.Show(obj.namecount().ToString());
}
Every time button3_Click is called, a new login object is instantiated. In other words, obj is not a reference to your main form; it is a reference to another object of the same type as your main form.
Every time a login object is instantiated, count defaults to zero.
This code will create a new instance of your Login form. Each instance will have its own instance variable count.
login obj = new login();
MessageBox.Show(obj.namecount().ToString());
The default value for an integer is 0, so each time you create a new instance of the form it will have the value of 0 in the count variable. If you want to have all instances of the form have the same value for count, you should make count static.
private static int count;
When the variable is static, there will be only one instance of count shared by all instances of the Login form.
var form1 = new login();
// mouse up event fires on form1, value of count is set to 3 (for example)
var form2 = new login();
form2.namecount(); // returns 3
Depending on what you want to do, there are other patterns, like using events, or a mediator that can help pass messages between components. This way when something happens in one form, other forms can react to the change without actually needing to reference or even know about the other forms in the application.
Where do you show the form? What you're doing is code is simply creating a new instance of the login form, and reading the nameCount value, which is still at its initialized value: 0.
I think you should use ShowDialog
and return the result in a DialogResult
. If that's OK, then read the count value.
精彩评论