Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to return a value from main form to a differnt form or class(C#)
    primarykey
    data
    text
    <p>For some unknown reasons this silly thing cant be implemented. </p> <p>I have an <code>int count</code> in the main form which I want to return to another class or form.</p> <pre><code>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 &lt; 10; i++) { if (txtusername.Text == obj.Usersusername()[i]) { count = i; break; } } } public int namecount() { return count; } } } </code></pre> <p><code>dbaccess</code> is another class and I could successfully call a function (Usersusername) defined in that class to my <code>login</code> form. Everything works fine up to this. Now I want to get the <code>int count</code> from main form back to <code>dbaccess</code> class. So I implemented a public function <code>namecount</code> to return <code>count</code>. But <code>count</code> is always zero in the other class. In the main form, I get the value of <code>count</code> correctly (which is <code>i</code>). But nothing gets returned when I call from <code>dbaccess</code> class this way:</p> <pre><code> login obj = new login(); // do stuff </code></pre> <p>or from another form in the same program, like this:</p> <pre><code>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()); } </code></pre> <p>The messagebox here should display <code>count</code> which is some number, but what's displayed is zero. What could possibly be the cause??</p> <p>The same thing happens when I'm trying to return a string from my main form. It's always null in other classes :(</p> <p>Thanks in advance</p> <p>Edit: Can you give the code snippet itself. I cant know the technical terms you might use to help me</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload