Hallo zusammen!
Ich bin noch ein blutiger Anfänger und möchte in meinem Hauptprogrammcode in der Klasse "Program" auf eine Eigenschaft der Klasse "Form1 : Form" zugreiffen. Die EIgenschaft heisst "filename" und enthält den Pfadnamen der Datei, die gerade geöffnet ist (ich programmiere ein kleines Notepad).
Beim Erstellen der Variable habe ich den getter auf public gestellt:
public string filename {get; private set};
Wenn ich folgenden Code in der Klasse "Program" eingebe, wird die Eigenschaft "filename" jedoch nicht angezeigt:
windowsformsapplication.form1.filename
Hier ist der Programmcode von der Klasse "Form1":
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:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public string filename { get; private set; } public Form1() { InitializeComponent(); }
private void beenden_click(object sender, EventArgs e) { Application.Exit(); }
private void neu_click(object sender, EventArgs e) { textbox.Text = ""; }
private void öffne_click(object sender, EventArgs e) { if (openfiledialog.ShowDialog() == DialogResult.OK) { string path = openfiledialog.FileName; textbox.Text = System.IO.File.ReadAllText(path); filename = path; } }
private void speichernunter_click(object sender, EventArgs e) { if (savefiledialog.ShowDialog() == DialogResult.OK) { string path = savefiledialog.FileName; System.IO.File.WriteAllText(path, textbox.Text); } }
private void speicher_click(object sender, EventArgs e) { System.IO.File.WriteAllText(filename, textbox.Text); } } } |
Hier ist der Programmcode der Klasse "Program":
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
| using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;
namespace WindowsFormsApplication1 { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); WindowsFormsApplication1.Form1.filename } } } |
Kann mir jemand erklären, wie ich auf die EIgenschaft zugreiffen kann?
Vielen Dank für eure Hilfe
