Autor Beitrag
storestore
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 397
Erhaltene Danke: 7

WIN 7
C#
BeitragVerfasst: So 18.09.11 10:43 
Hallo,
ich habe mal eine Frage: Kann mir einer vlt. Den Unterschied zwischen Static und nicht Static in c# erklären? Ich habe es auch schon gegooglet, aber ich habe es immer nich muht verstanden!

MfG storestore

_________________
Der Pc ist nur so schlau, wie derjenige der in steuert!
"Don't Quit. Suffer now, and live the rest of your life as a champion"
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: So 18.09.11 12:06 
Der MSDN-Eintrag static (C#) erklärt es doch ganz gut.
Statische Member existieren eben nur einmalig pro Klasse (Typ) und nicht für jede Instanz davon.
storestore Threadstarter
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 397
Erhaltene Danke: 7

WIN 7
C#
BeitragVerfasst: So 18.09.11 14:32 
Th69 hat folgendes geschrieben:
Statische Member existieren eben nur einmalig pro Klasse (Typ) und nicht für jede Instanz davon.

Achso, also wenn ich jetzt eine Instanz erstelle, kann ich nicht auf statische Member zugreifen, stimmt das?

mfg storestore

Moderiert von user profile iconTh69: quote korrigiert

_________________
Der Pc ist nur so schlau, wie derjenige der in steuert!
"Don't Quit. Suffer now, and live the rest of your life as a champion"
huuuuuh
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 665
Erhaltene Danke: 19

win xp, (win vista), win 7
VS 2008 Express Edition, VS 2010 Express Edition, VS 2010 Professionell
BeitragVerfasst: So 18.09.11 14:37 
Doch doch, mit einer Instanz kannst du auf statische Member zugreifen. Der statische Member ist nur überall der gleiche. Statische Member können nicht auf Instanzmember zugreifen.
ujr
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 102
Erhaltene Danke: 12



BeitragVerfasst: So 18.09.11 14:38 
user profile iconstorestore hat folgendes geschrieben Zum zitierten Posting springen:
wenn ich jetzt eine Instanz etstelle, kann ich nicht auf statische Member zugreifen, stimmt das?


Nein. Du greifst aber in jeder Instanz auf die selbe statische Variable zu (weil sie eben zum Typ gehört und der für jede Instanz gleich ist).

Edit: huuuuuh war schneller - unabhängig von diesen Antworten solltest Du Dir aber wirklich die Dokumentation anschauen!
storestore Threadstarter
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 397
Erhaltene Danke: 7

WIN 7
C#
BeitragVerfasst: So 18.09.11 14:43 
Achso, das heißt wenn ich etwas in einer Methode eine static member reinschreibe und die Methode aufrufe greift er nicht auf dieses Member zu, stimmt das?

_________________
Der Pc ist nur so schlau, wie derjenige der in steuert!
"Don't Quit. Suffer now, and live the rest of your life as a champion"
ujr
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 102
Erhaltene Danke: 12



BeitragVerfasst: So 18.09.11 14:47 
user profile iconstorestore hat folgendes geschrieben Zum zitierten Posting springen:
wenn ich etwas in einer Methode eine static member reinschreibe und die Methode aufrufe greift er nicht auf dieses Member zu, stimmt das?


Ich würde mal sagen, Nein. Verstehe aber nicht so richtig wie Du das meinst. Warum probierst Du es nicht einfach aus? Wenn Du eine konkrete Nachfrage zu einem Code-Schnippsel hast, kann man es daran besser beschreiben.
storestore Threadstarter
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 397
Erhaltene Danke: 7

WIN 7
C#
BeitragVerfasst: So 18.09.11 14:55 
Ich meine auf eine Statisches objekt kann ich ohne den Klassen namen nicht zugreifen
z.B.
ausblenden 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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
<span style="color: red">testtest(); // Geht nicht</span>
        }
        static class test
        {
            public static void testtest()
            {
                Console.WriteLine("ada");
                Console.ReadLine();
            }
        }
    }
}

_________________
Der Pc ist nur so schlau, wie derjenige der in steuert!
"Don't Quit. Suffer now, and live the rest of your life as a champion"
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: So 18.09.11 15:03 
Stell dir eine Klasse "Kreis" vor.
Diese hat ein statisches Attribut "Farbe" und ein nicht-statisches "Größe".
Du erstellst nun vier Objekte von dieser Klasse.

Setzt du nun für Objekt 1 die Größe, dann verändert sich jeweils nur die Größe vom ersten Kreis.
Setzt du das Attribut Farbe, dann ändert sich die Farbe für alle Kreise.

Ich hoffe, das ist so ein wenig "plastischer" ;-)

Dein Beispiel funktioniert so übrigens nicht:
Du versuchst in der statischen Main Methode deiner Klasse "Program" eine statische Klasse "test" zu definieren.

Klassendefinitionen erfolgen nicht im Methoden.

Es wäre hier vielleicht ratsam mal ein Tutorial oder Buch zu lesen ;-)


tüdeldü...
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
static class test
        {
            public static void testtest()
            {
                Console.WriteLine("ada");
                Console.ReadLine();
            }
        }

 class Program
    {
        static void Main(string[] args)
        {
          test.testtest();
        }
        
    }

_________________
Delphi: XE - OS: Windows 7 Professional x64


Zuletzt bearbeitet von Horschdware am So 18.09.11 15:04, insgesamt 1-mal bearbeitet
ujr
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 102
Erhaltene Danke: 12



BeitragVerfasst: So 18.09.11 15:03 
user profile iconstorestore hat folgendes geschrieben Zum zitierten Posting springen:
Ich meine auf eine Statisches objekt kann ich ohne den Klassen namen nicht zugreifen


Ja, das ist richtig. War das eine Feststellung oder Frage? Falls letzteres, hast Du die ja eigentlich selbst beantwortet.
storestore Threadstarter
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 397
Erhaltene Danke: 7

WIN 7
C#
BeitragVerfasst: So 18.09.11 15:48 
Zitat:
Ich hoffe, das ist so ein wenig "plastischer" ;-)
Joub, danke jetzt habe ichs kappiert!


Zitat:
Es wäre hier vielleicht ratsam mal ein Tutorial oder Buch zu lesen ;-)

Wisso umbedingt Visual c# 2008 für Dummis

Zitat:
tüdeldü..

Ich hoffe du meinst nicht das tüdeldü von "Hangover" der Film
MFG storestore

_________________
Der Pc ist nur so schlau, wie derjenige der in steuert!
"Don't Quit. Suffer now, and live the rest of your life as a champion"
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: So 18.09.11 22:00 
user profile iconstorestore hat folgendes geschrieben Zum zitierten Posting springen:


Wisso umbedingt Visual c# 2008 für Dummis

Einfach nur ein beliebiges Beispiel. Nur weil "für Dummies" oder "für Kids" dran steht, heisst das nicht dass die Bücher schlecht sind oder für Minderbemittelte gedacht sind. Stör dich einfach nicht an der Bezeichnung.

Zitat:

Ich hoffe du meinst nicht das tüdeldü von "Hangover" der Film

Kenne ich nicht. Tut mir leid

_________________
Delphi: XE - OS: Windows 7 Professional x64