Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Static Eigenschaft-Auf den Member x kann nicht mit einem Instanzenverweis zugegriffen

Empfohlene Antworten

Veröffentlicht

Hi,

folgendes Problem, bezüglich C#.

Sobald ich eine Klasse erstelle die eine statische Variable enthält und ich auf diese zugreifen möchte, kommt eine Fehlermeldung. Mir wird die Variable auch nicht angezeigt, wenn ich (wie im Beispiel) bit. eingebe.

Hat da jemand eine Idee, wo der Fehler liegen könnte?

Gruß

Fasko

Fehler 1 Auf den Member "StatischTest.A.zahl" kann nicht mit einem Instanzenverweis zugegriffen werden. Qualifizieren Sie ihn stattdessen mit einem Typnamen. c:\users\jo\documents\visual studio 2013\Projects\StatischTest\StatischTest\Program.cs 33 31 StatischTest

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace StatischTest

{



    public class A

    {

        public static int zahl;


        public void foo() 

        {

            zahl++;


        }


    }



    class Program

    {

        static void Main(string[] args)

        {                                                                                                                                                                                                                                                                        


            A bit = new A();

            bit.foo();



            Console.WriteLine(bit.zahl);


            Console.ReadKey();




        }

    }

}

Wie die Fehlermeldung schon sagt: Du greifst über eine Instanz (bit) auf eine Klasseneigenschaft (static zahl) zu. Wenn du statische Eigenschaften nutzen möchtest (was Vor- und Nachteile, aber hauptsächlich Nachteile hat), musst du über die Klasse zugreifen, also A.zahl. Oder du machst zahl zur Instanzvariablen (ohne static) und greifst so zu, wie du es haben möchtest.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.