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; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace CoDeFS { public partial class CoDeFSmain : Form {
public CoDeFSmain() { InitializeComponent(); }
private void Load_OleDBConnection_on_start (object sender, EventArgs e) { } |
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; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace CoDeFS { class DB_Connect { 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;
public void dbconnection_state() { if (Db_connection_btn.Text == "disconnect") { CoDeFSmain }
if (Db_connection_btn.Text == "connect") { } }
public void dbconnect() { con.Close(); con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"; con.ConnectionString += pfad_codefsinterface; try { con.Open(); } catch (OleDbException er) { MessageBox.Show(er.Message); }
Db_connection_btn.Text = "disconnect"; checkcon = true; dbconnection_state(); }
public void dbdisconnect() { try { con.Close(); } catch (OleDbException er) { MessageBox.Show(er.Message); }
Db_connection_btn.Text = "connect"; checkcon = false; dbconnection_state(); }
} } |
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 { private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Vom Windows Form-Designer generierter Code
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(); this.CoDeFS_Tasksymbol.Icon = ((System.Drawing.Icon)(resources.GetObject("CoDeFS_Tasksymbol.Icon"))); this.CoDeFS_Tasksymbol.Text = "CoDeFS"; this.CoDeFS_Tasksymbol.Visible = true; this.Db_connection_btn.Location = new System.Drawing.Point(19, 20); this.Db_connection_btn.Name = "Db_connection_btn"; this.Db_connection_btn.Size = new System.Drawing.Size(64, 21); this.Db_connection_btn.TabIndex = 0; this.Db_connection_btn.Text = "connect"; this.Db_connection_btn.UseVisualStyleBackColor = true; 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(799, 503); 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
UGrohne: 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: .
Waswiewo :shock: ? In einer autogenerierten Klasse solltest du nie etwas ändern, musst du auch gar nicht, dafür ist gibt es ja partielle Klassen.
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2025 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!