Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a class called UserId
C#
class UserId
    {
        public string username;
        public string userlevel;
        public string firstname;
        public string lastname;
        
    }


and on Main form of my app on load event I have this code ( yesterday solved here in codeproject:) )
C#
SqlCommand commanduser = new SqlCommand("SELECT * from Users  WHERE Username = @Username", cs);
            commanduser.Parameters.Add("@Username", SqlDbType.NVarChar);
            commanduser.Parameters["@Username"].Value = username.Text;

            SqlDataAdapter dataAdapteruser = new SqlDataAdapter(commanduser);

            DataTable dataTableuser = new DataTable();

            dataAdapteruser.Fill(dataTableuser);

            userid.username = (String)dataTableuser.Rows[0]["Username"];
            userid.userlevel = (String)dataTableuser.Rows[0]["UserLevel"];
            userid.firstname = (String)dataTableuser.Rows[0]["FirstName"];
            userid.lastname = (String)dataTableuser.Rows[0]["LastName"];

and this works great when I am using those strings later on on this form, but the problem is when I open another form on this app I have to use those values from strings but they are empty

I am thinking I have to somehow call this method from Main form but don't know how to do it

or it came to my mind to create another class with this method which I will place on any form

any advice I accept
thank you
Posted
Updated 15-Aug-13 2:24am
v3
Comments
[no name] 15-Aug-13 7:23am    
Okay.... and? Did you maybe have some sort of a question or problem?
shonezi 15-Aug-13 7:26am    
well I don't know how to call this method on another form and I want to avoid writing entire code on each form. that's my problem actually :)

1 solution

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
shonezi 15-Aug-13 8:04am    
thank you very much :)
Sergey Alexandrovich Kryukov 15-Aug-13 8:06am    
You are very welcome.
Good luck, call again.
—SA

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