Autor Beitrag
JayK
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Fr 29.07.05 17:34 
Hallo,
tut mir leid, aber ich weiß bei dieser (bestimmt banalen) Sache nicht weier... :(
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
public class MainForm
{
  public int TopicWinCount;
}

----------
// selber namespace, andere Datei
public class OtherForm
{
  MainForm.TopicWinCount++;
}

Fehler:
An object reference is required for the nonstatic field, method or property 'Namespace.MainForm.TopicWinCount'


Was ist hier falsch?

Schonmal Danke im vorraus,
JayK


Moderiert von user profile iconChristian S.: Topic aus .NET verschoben am Sa 17.09.2005 um 22:11
Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Fr 29.07.05 17:39 
Du musst entweder ein public static int TopicWinCount; daraus machen, dann ist das Instanz-übergreifend, oder du musst eine Instanz der Klasse MainForm erzeugen, um auf das (instance-) Feld TopicWinCount zugreifen zu können. Dann gehört das Feld aber nur dieser einen Instanz.

_________________
Ist Zeit wirklich Geld?
JayK Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Fr 29.07.05 17:43 
Eine Inztanz von MainForm wird zu Beginn des Programmablaufs erzeugt (bei Main() )
Aber ich werd wohl das mit dem static machen...
EDIT: static klappt. Danke. :-D
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Sa 30.07.05 11:41 
user profile iconJayK hat folgendes geschrieben:
Eine Inztanz von MainForm wird zu Beginn des Programmablaufs erzeugt (bei Main() )

Kann es sein, dass du die Groß und Kleinschreibung bei den Bezeichnern etwas durcheinander gebracht hast?

Zitat:
Aber ich werd wohl das mit dem static machen...

Frägt sich nur, ob du das nur gewählt hast, weil es einfacher ist, oder weil es zur Logik des Programms passt.

_________________
Ist Zeit wirklich Geld?
JayK Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Sa 30.07.05 12:27 
user profile iconAndyB hat folgendes geschrieben:
Kann es sein, dass du die Groß und Kleinschreibung bei den Bezeichnern etwas durcheinander gebracht hast?

Wo soll ich die Groß-/Kleinschreibung durcheinander gebracht haben???
MainForm wird beim Run(...) erzeugt.

user profile iconAndyB hat folgendes geschrieben:
Zitat:
Aber ich werd wohl das mit dem static machen...

Frägt sich nur, ob du das nur gewählt hast, weil es einfacher ist, oder weil es zur Logik des Programms passt.

Ich hab das erstmal gewählt, weil ich anders das Problem nicht finde :-( und logik :gruebel: MainForm hab ich sowieso nur 1-mal und demzufolge will ich auch nur 1-mal den Zähler haben... :roll:
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Sa 30.07.05 14:37 
user profile iconJayK hat folgendes geschrieben:
Wo soll ich die Groß-/Kleinschreibung durcheinander gebracht haben???
MainForm wird beim Run(...) erzeugt.

MainForm und mainForm.

Zitat:
und logik :gruebel: MainForm hab ich sowieso nur 1-mal und demzufolge will ich auch nur 1-mal den Zähler haben... :roll:

Wenn du static davor schreibst, gehört das Feld zur Klasse. Lässt du es weg, gehört es zum Objekt.

_________________
Ist Zeit wirklich Geld?