Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I dont have access to an public const c# in an other class

Tags:

c#

constants

I tried to access in my Test.cs to strings in my other class TerminalStrings.cs but was not able.... how can I access to them ?

TerminalStrings.cs :

namespace RFID_App
{
    class TerminalStrings
    {
        public static readonly string identiError = "ERROR";
        public const string identiSuccess = "SUCCESS";
    }
}

In Test.cs:

 namespace RFID_App
    {
          class Test
        {
            public Test()
            {
               string test;
               TerminalStrings stringlist = new TerminalStrings();
               test = stringlist.identiError; //this was not possible 
            }
        }
    }
like image 312
Samy Avatar asked Dec 12 '25 15:12

Samy


2 Answers

const are implicitly static, you can't access them with instance member instead you need the class name to access.

test = TerminalStrings.identiError;

See: Why can't I use static and const together? - By Jon Skeet

like image 111
Habib Avatar answered Dec 14 '25 05:12

Habib


The consts are not member variables, they are on the class.

string test = TerminalStrings.identiError;
like image 36
krillgar Avatar answered Dec 14 '25 03:12

krillgar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!