Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have problem to acces data in my class.With data from Class to Main Form i have no problem. I try more ways but nothing working for me (i do not know why). I need in Main form set few strings and this strings have accesable from my Class.

For example Main form:
C#
private void button1_Click(object sender, EventArgs e)
{
    TestClass tc = new TestClass();

    if (tc.Lunch() == true)
        MessageBox.Show("Data passed");
    else
        MessageBox.Show("Error");
}

private string myprop;
public string MyProperty
{
get{return  myprop ;}
set{myprop = "Hello" ;}
}

and Class:
C#
class TestClass
    {
        public bool Lunch()
        {
            Form1 f1 = new Form1();
            string newStr = f1.MyProperty;

            if (newStr == "Hello")
                return true;
            else
                return false;
        }
    }


Actualy for passing data from Main Form to class using user property but this is much slover
C#
Properties.Settings.Default.UserString = "Hello";
Properties.Settings.Default.Save();

Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 26-May-15 20:07pm    
Sorry, it sounds gibberish... Which class, what's the problem? And what, form is not a class. And data, more typically, is not the data of a class, but the data on the instance of the class (instance members).
—SA
[no name] 26-May-15 20:18pm    
It's because your "TestClass" is instanciating a brand new uninitialized Form1 and you are trying to see if some value exists (it doesn't). One way is to pass the existing instance of Form1 to your test class.
Sergey Alexandrovich Kryukov 26-May-15 22:17pm    
Will you make this point in a separate answer?
—SA

1 solution

The problem is that you still have no clue on general programming. In this case, your problem is understanding types vs instances of types, instantiation, scope, instance vs static member. These are bare basics of "pre-OOP" programming in OOP systems, including .NET. Without this knowledge, you are not recommended to do any UI at all. You can train yourself on simplest exercises based on console-only applications.

For the detail of solving the problem of communication between form instances of different classes (in your case, with some non-form class, which won't make and practical difference), please see my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

But if this article is not 100% clear for you, you are not yet ready to do the UI or other advanced topics; in this case, get back to basics, to gain some more confidence. You will come back to more complex stuff a bit later, maybe pretty soon, don't worry.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900