Entwickler-Ecke

Basistechnologien - Auf Variable außerhalb der Klasse zugreifen


alcedo707 - So 11.09.11 20:48
Titel: Auf Variable außerhalb der Klasse zugreifen
Hallo,

Ich verzweifle gerade :cry:
Ich habe schon über 1 Stunde gesucht, aber nie wirklich etwas gefunden :(
Ich möchte die Variablen (vorname, nachname,..) außerhalb der SQLlogin() Klasse verwenden, damit ich auf Form2 auf die zugreifen kann


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
        public string SQLlogin()
        {


            String user = textBox1.Text;
            String pass = textBox2.Text;
            String vorname;
            String nachname;
            String usersql;
            String pwsql;
            String geld_string;
            int geld;
            String HP_full_string;
            int HP_full;
            String Def_string;
            int Def;
            String Exp_string;
            int Exp;
            String spielername;
            String email;
            String HP_string;
            int HP;




                String connectionString = "Data Source =.; Initial Catalog =syntic; Integrated Security =SSPI;";
                     //// SqlCommand cmd = new SqlCommand("SELECT username,password FROM users WHERE username='" + textBox1.Text + "' and password='" + maskedTextBox1.Text + "'", lgnConnection);


                SqlConnection connection = new SqlConnection(connectionString);


                connection.Open();


                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = ("SELECT * FROM dbo.daten WHERE username='" + user + "' and passwort='" + pass + "'");
                SqlDataReader reader = command.ExecuteReader();


                if (reader.HasRows)
                {
                    while (reader.Read())
                    {

                        vorname = reader["vorname"] + "";
                        nachname = reader["nachname"] + "";
                        usersql = reader["Username"] + "";
                        pwsql = reader["Passwort"] + "";
                        geld_string = reader["Geld"] + "";
                        geld = Convert.ToInt32(geld_string);
                        HP_full_string = reader["HP_Full"] + "";
                        HP_full = Convert.ToInt32(HP_full_string);
                        Def_string = reader["Def"] + "";
                        Def = Convert.ToInt32(Def_string);
                        Exp_string = reader["Exp"] + "";
                        Exp = Convert.ToInt32(Exp_string);
                        spielername = reader["spielername"] + "";
                        email = reader["email"] + "";
                        HP_string = reader["HP"] + "";
                        HP = Convert.ToInt32(HP_string);





                        if (user == usersql && pass == pwsql)
                        {
                            //Neue Form !

                            new Form2().Show();
                            this.Hide();


                        }
                        else if (user != usersql || pass != pwsql)
                        {

                        }
                      }
                    }



Ich bitte um Hilfe


C# - So 11.09.11 21:10

Ich weis nicht ob es mit "public string" Klasse auch funktioniert, aber so könnte es gehen:
einfach

C#-Quelltext
1:
public string user = textBox1.Text; //usw...                    

Dann bei Form2:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
public class Form2 : Form
{
  void Main()
  {
    SQLlogin sqllogin = new SQLlogin();
    var variable;
    variable = sqllogin.user; //oder sqllogin.pass ...
  }
}

und wenn das nicht funktioniert, dann probiers mal so:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
public class Form2 : Form
{
  public string name, user, pw;
  public int geld;

  void Main()
  {
    //...
  }
}


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
//Bei deinem Public String SQLlogin()
Form2 form2 = new Form2();
form2.name = name;
form2.user = user;
form2.geld = gled;
//...
form2.Show();


gfoidl - So 11.09.11 21:28

Hallo,

siehe auch Auf Variable außerhalb der Klasse zugreifen [==> Eigenschaften verwenden] [http://www.mycsharp.de/wbb2/thread.php?threadid=98330].


mfG Gü


alcedo707 - So 11.09.11 21:28

public vor die Variablen gehen leider nicht, bei mir steht dann


Quelltext
1:
Ungülter Ausdruck "public",                    


Quelltext
1:
Nur assignment-, call-, increment-, decrement- und "new object"-Ausdrücke können als Anweisung verwendet werden.                    


ps: ja das ist mein thread, aber ich habe auf mehr genauere informationen gehofft, ich suche ja schon stunden :cry:


gfoidl - So 11.09.11 21:37

Hallo,

Zitat:

aber ich habe auf mehr genauere informationen gehofft, ich suche ja schon stunden

Dann lies dir ein Grundlagenbuch durch. In jedem halbswegs guten ist das beschrieben, denn das zählt zu den absoluten Grundlagen der OOP.

Wenn du danach schon stundenlang suchst dann liegst wohl an dir (nicht böse gemeint), aber das Stichwort Eigenschaften (od. in engl. Properties) sollte dich wohl auf die richtige Spur lenken. Sonst liegst wirklich an dir ;-)


mfG Gü


C# - So 11.09.11 21:38

Haste mal meine 2te Idee ausprobiert? Die müsste eigentlich klappen:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
 public string SQLlogin()
        {


            String user = textBox1.Text;
            String pass = textBox2.Text;
            String vorname;
            String nachname;
            String usersql;
            String pwsql;
            String geld_string;
            int geld;
            String HP_full_string;
            int HP_full;
            String Def_string;
            int Def;
            String Exp_string;
            int Exp;
            String spielername;
            String email;
            String HP_string;
            int HP;




                String connectionString = "Data Source =.; Initial Catalog =syntic; Integrated Security =SSPI;";
                     //// SqlCommand cmd = new SqlCommand("SELECT username,password FROM users WHERE username='" + textBox1.Text + "' and password='" + maskedTextBox1.Text + "'", lgnConnection);


                SqlConnection connection = new SqlConnection(connectionString);


                connection.Open();


                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = ("SELECT * FROM dbo.daten WHERE username='" + user + "' and passwort='" + pass + "'");
                SqlDataReader reader = command.ExecuteReader();


                if (reader.HasRows)
                {
                    while (reader.Read())
                    {

                        vorname = reader["vorname"] + "";
                        nachname = reader["nachname"] + "";
                        usersql = reader["Username"] + "";
                        pwsql = reader["Passwort"] + "";
                        geld_string = reader["Geld"] + "";
                        geld = Convert.ToInt32(geld_string);
                        HP_full_string = reader["HP_Full"] + "";
                        HP_full = Convert.ToInt32(HP_full_string);
                        Def_string = reader["Def"] + "";
                        Def = Convert.ToInt32(Def_string);
                        Exp_string = reader["Exp"] + "";
                        Exp = Convert.ToInt32(Exp_string);
                        spielername = reader["spielername"] + "";
                        email = reader["email"] + "";
                        HP_string = reader["HP"] + "";
                        HP = Convert.ToInt32(HP_string);





                        if (user == usersql && pass == pwsql)
                        {
                            //Neue Form !
        Form2 form2 = new Form2();    //WICHTIG: Auf Gorß- und Kleinschreibung achten.
        form2.name = name;
        form2.user = user;
        form2.geld = geld;
        //...
        form2.Show();



                        }
                        else if (user != usersql || pass != pwsql)
                        {

                        }
                      }
                    }


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public partial class Form2: Form
    {
        
  public string name, user, pw;
  public int geld;  

        public Form2()
        {
            InitializeComponent();
        }
    }

Also genauer gehts jetzt echt nicht mehr.


Trashkid2000 - So 11.09.11 21:39

Hallo alcedo707,
für sowas gibt es Properties.
@C# public Valiablen sind nicht gut!
Mit Properties würde in etwa so aussehen:

C#-Quelltext
1:
2:
3:
4:
5:
6:
public class SQLlogin
{
  public string Vorname { get; set; }
  public string Nachname { get; set; }
  ...
}

Zugreifen kannst Du dann so auf eine Property:

C#-Quelltext
1:
2:
3:
SQLlogin login = new SQLlogin();
login.Vorname = "Hans";
string vorname = login.Vorname;


Trashkid2000 - So 11.09.11 21:42

Nachtrag:
das

C#-Quelltext
1:
2:
String user = textBox1.Text;
String pass = textBox2.Text;
ist nicht gut!
Denn die Klasse sollte nicht die Form kennen. Denn der Klasse ist es im Endeffekt egal, woher sie ihre Informationen bezieht oder wo sie die Informationen anzeigt.


alcedo707 - So 11.09.11 22:56

wohin gebe ich die


C#-Quelltext
1:
2:
3:
4:
5:
6:
public class SQLlogin
{
  public string vorname { get; set; }
  public string nachname { get; set; }
  ...
}


hin? Die muss doch außerhalb von


C#-Quelltext
1:
2:
public string SQLlogin() 
{


sein. aber wenn es draußen ist, ist vorname, nachname,.. nicht mehr deklariert...

sorry für die peinliche fragen


gfoidl - So 11.09.11 23:04

Hallo,

dann erstell dafür eine neue Klasse und als Rückrabetype vom Login kannst du ja eine Instanz davon zurückgegeben. Lies dir aber wirklich ein Grundlagenbuch durch. Darin sind all diese Frage erklärt.


mfG Gü