Entwickler-Ecke

C# - Die Sprache - Fehler CS0103 in ClassA im Hinblick auf buttons aus Form1


iwb-augsburg - Mi 14.11.07 15:30
Titel: Fehler CS0103 in ClassA im Hinblick auf buttons aus Form1
Hallo, ich bin ein totaler Anfänger in Sachen C#.
Ich möchte in einer Klasse "DB_Connect"auf die Buttons einer Form "CoDeFSmain" zugreifen und bekomme dabei die Fehlermeldung CS0103.
Es ist mir klar woran es in etwa liegt. Ich habe einfach keine Ahnung wie man das Problem löst. Hier der Code:
Es geht um die Buttons und Lables, wie z.B Db_connection_btn bei DB_Connect.cs



CoDeFSmain.cs - hier sind die Buttons eigentlich gewesen

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;                                                            // für die Datenbank
using System.Data.SqlClient;                                                        // für die Datenbank
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CoDeFS
{
    public partial class CoDeFSmain : Form
    {

        public CoDeFSmain()
        {
            InitializeComponent();
        }



        // Diese Methode wird beim Starten geladen. (muss manuell in "CoDeFSmain.Designer.cs" im Abschnitt "CoDeFSmain" eingefügt werden) 
        private void Load_OleDBConnection_on_start (object sender, EventArgs e)
        {
            //checkcon = false;
            //readdb_btn.Enabled = false;
            //viewColumnName_btn.Enabled = false;
        }



DB_Connect.cs - hier sollten nun die Buttons angesprochen werden

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:
88:
89:
90:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;                                                            // für die Datenbank
using System.Data.SqlClient;                                                        // für die Datenbank
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CoDeFS
{
    class DB_Connect
    {
        // Variablen
        private OleDbCommand cmd;
        private OleDbConnection con = new OleDbConnection();
        private bool checkcon;
        private string pfad_codefsinterface = "Data Source=C:\\Programme\\COSIMIR® Factory\\Control\\Projects\\CognitiveFactory\\Cogfacinterface.mdb";
        private string readstate = "no data";
        private string sqlString;



        // ANFANG: BLOCK - DATENBANKVERBINDUNG

        // zeigt den Status der Datenbankverbindung an
        public void dbconnection_state()
        {
            if (Db_connection_btn.Text == "disconnect")
            {
                CoDeFSmain
                //ViewDbState_lbl.Text = "connected";                                 // ändert den Text der DBStatus-Anzeige
                //ViewDbState_lbl.ForeColor = System.Drawing.Color.Green;             // ändert die Farbe der DBStatus-Anzeige
            }

            if (Db_connection_btn.Text == "connect")
            {
                //ViewDbState_lbl.Text = "not connected";
                //ViewDbState_lbl.ForeColor = System.Drawing.Color.Red;
            }
        }



        // Baut die Verbindung zur Datenbank auf
        public void dbconnect()
        {
            con.Close();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";             // Datenbankart
            con.ConnectionString += pfad_codefsinterface;                           // Pfad für die Datenbank

            try
            {
                con.Open();
            }
            catch (OleDbException er)
            {
                MessageBox.Show(er.Message);
            }

            Db_connection_btn.Text = "disconnect";
            checkcon = true;
            dbconnection_state();
        }



        // Löst die Verbindung zur Datenbank
        public void dbdisconnect()
        {
            try
            {
                con.Close();
                //MessageBox.Show("Disconnection OK");
            }
            catch (OleDbException er)
            {
                MessageBox.Show(er.Message);
            }

            Db_connection_btn.Text = "connect";
            checkcon = false;
            dbconnection_state();
            //dbread_state();
        }

        // ENDE: BLOCK - DATENBANKVERBINDUNG
    }
}



CoDeFSmain.Designer.cs - falls es notwendig ist hier auch was zu ändern


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:
namespace CoDeFS
{
    partial class CoDeFSmain
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CoDeFSmain));
            this.CoDeFS_Tasksymbol = new System.Windows.Forms.NotifyIcon(this.components);
            this.Db_connection_btn = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // CoDeFS_Tasksymbol
            // 
            this.CoDeFS_Tasksymbol.Icon = ((System.Drawing.Icon)(resources.GetObject("CoDeFS_Tasksymbol.Icon")));
            this.CoDeFS_Tasksymbol.Text = "CoDeFS";
            this.CoDeFS_Tasksymbol.Visible = true;
            // 
            // Db_connection_btn
            // 
            this.Db_connection_btn.Location = new System.Drawing.Point(1920);
            this.Db_connection_btn.Name = "Db_connection_btn";
            this.Db_connection_btn.Size = new System.Drawing.Size(6421);
            this.Db_connection_btn.TabIndex = 0;
            this.Db_connection_btn.Text = "connect";
            this.Db_connection_btn.UseVisualStyleBackColor = true;
            // 
            // CoDeFSmain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.ClientSize = new System.Drawing.Size(799503);
            this.Controls.Add(this.Db_connection_btn);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "CoDeFSmain";
            this.Text = "CoDeFS";
            this.Load += new System.EventHandler(this.Load_OleDBConnection_on_start);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.NotifyIcon CoDeFS_Tasksymbol;
        private System.Windows.Forms.Button Db_connection_btn;
    }
}


Moderiert von user profile iconUGrohne: Quote- durch C#-Tags ersetzt


Leuchtturm - Mi 14.11.07 15:46

1. Sprich niemals von einer nicht FormKlasse einen Control an, sprich die DB_ConnectKlasse von der Form an.
2. Woher soll die Klasse Db_Connect den Button kennen wenn er als private deklariert ist?

CS0103 hat folgendes geschrieben:
Es wurde versucht, einen Namen zu verwenden, der in der Klasse, im Namespace oder im Bereich nicht vorhanden ist.


iwb-augsburg - Mi 14.11.07 16:09

OK, Danke für die Antwort.
Aber gibt es denn wirklich gar keine Möglichkeit mein Vorhaben so umzusetzen, wie ich es möchte (Also Controls "fremd" ansprechen)


Kha - Mi 14.11.07 17:13

Du kannst dem Designer befehlen, dem Komponenten-Feld einen anderen Zugriffsmodifizierer als private zu verpassen. Die entsprechende Eigenschaft musst du aber selbst suchen, denn designtechnisch ist das einfach eine absolut hässliche Lösung und ich möchte nicht vorgeworfen bekommen, zu schlechtem Design zu verleiten :zwinker: .
// Diese Methode wird beim Starten geladen. (muss manuell in "CoDeFSmain.Designer.cs" im Abschnitt "CoDeFSmain" eingefügt werden)Waswiewo :shock: ? In einer autogenerierten Klasse solltest du nie etwas ändern, musst du auch gar nicht, dafür ist gibt es ja partielle Klassen.