0

I have the following class but I can't seem to get the desired results with C#.

public class AppOsType  {
    public static class IOS {
        public static int IOS()
        {
            return 100;
        }
        public static string ToString() 
        {
            return "iOS";
        }
    }
    ... // additional values
}   

I want to get the following results:

AppOsType.IOS // returns 100
AppOsType.IOS.ToString() // returns "iOS"

But I'm getting an error saying AppOsType.IOS is a type when i do the following:

Assert.AreEqual(100, AppOsType.IOS);

What am I missing?

Edit: left out static.

CincauHangus
  • 431
  • 3
  • 21

5 Answers5

2

Not sure why all the static and inner class stuff is needed for, why don't you keep it simple and define an enum:

 public enum AppOsType
 {
      IOS = 100
 }

Then use

var ios = AppOsType.IOS;
var number = (int)ios;
var name = ios.ToString();

If you need to return a translated string based on enum, you could add a dictionary:

 var translations = new Dictionary<AppOsType, string>()
 {
     { AppOsType.IOS, "iOs" }
 }

and then

var ios = AppOsType.IOS;
var number = (int)ios;
var name = translations[ios];
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • Can't change existing implementation, otherwise I'll trigger a major refactor. :( All I can do is just change the `AppOsType` like classes. – CincauHangus Mar 27 '14 at 10:32
1

If you really need this nested static class inside the AppOsType class then you need to change something because a method cannot have the same name of the class and a constructor cannot return values. (Think to the fact as if the return value of the constructor is already defined to be the instance of the class)

void Main()
{
    Console.WriteLine(AppOsType.IOS.Version);
    Console.WriteLine(AppOsType.IOS.ToString());
}

public class AppOsType  
{
    // .... other members here ?? ...
    public static class IOS 
    {
        public static readonly int Version;
        static IOS()
        {
            // In the static constructor you could set the readonly
            // static property
            Version = 100;
        }
        public static string ToString() 
        {
            return "iOS";
        }
    }
} 
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • No it's not necessary to have the static class, feel free to reconstruct the class, but still achieving the same result with the two expected results – CincauHangus Mar 27 '14 at 10:30
  • Then, if you have only these two properties, why not make them part of the outer class? I can't see any logical reason to not do that – Steve Mar 27 '14 at 10:31
0

Its becaus IOS is a class. AppsOsType.IOS points to the static class. If your Method public int IOS() would be static, you can access it using AppOsType.IOS.IOS()

Chrisi
  • 371
  • 3
  • 15
0

If you don't want an enum for some reason, to make it work with desired syntax, you need a public property (not a ctor or method) :

public class AppOsType  {
    public static class IOS {
        public static  int IOS
        {
            get { return 100; }
        }
        public static string ToString() 
        {
            return "iOS";
        }
    }
}   
Guillaume
  • 12,824
  • 3
  • 40
  • 48
0

You are using the same name IOS so change this

AppOsType.IOS // returns 100 [error]  

to

AppOsType.IOS.IOS // might returns 100