switch(string)

Robert Fraser fraserofthenight at gmail.com
Wed Jun 18 01:57:51 PDT 2008


David Ferenczi wrote:
> The following code gives the following compilation error:
> 
> Error: case must be a string or an integral constant, not BAR1
> Error: case must be a string or an integral constant, not BAR2
> 
> --------------------------8<------------------------------------
> int main(string[] args)
> {
>     string BAR1 = "bar1";
>     string BAR2 = "bar2";
>     string myString = "bar3";
> 
>     switch (myString)
>     {
>         case BAR1:
>             break;
> 
>         case BAR2:
>             break;
> 
>         defualt:
>             break;
>     }
> 
>     return 0;
> }
> --------------------------8<------------------------------------
> 
> Can somebody explain me why?
> 
> Thanks,
> David

BAR1 and BAR2 are not constant. In D1, try:
     const BAR1 = "bar1";
In D2, you can also try:
     invariant BAR1 = "bar1";
     enum BAR1 = "bar1";
The enum version and the const version in D1 will not take up any storage.


More information about the Digitalmars-d-learn mailing list