I've got this enum: namespace MyApp.Internal.Enums {     public enum EServerType     {         API     } }  I am trying to use the Factory pattern to get a class according to a string, and this is my factory (with both the working code and the error): using MyApp.Handlers.Interfaces; using MyApp.Internal.Enums; using MyApp.Services.Interfaces;  namespace MyApp.Handlers {     public class ServerCallerFactory : IServerCallerFactory     {         public IServerCaller GetServerCaller(string serverType)         {             if (serverType.ToUpper() == EServerType.API.ToString())             {              }              switch (serverType.ToUpper())             {                 case EServerType.API.ToString():                     break;                 default:                     break;             }         }     } }  I prefer switching over the string instead of using multiple if statements of course. The if block isn't throwing any compiler error, however, the switch block is! Error thrown by compiler: CS0426 The type name 'API' does not exist in the type EServerType. Why is this happening and how to fix it?
7882
What's the best way to convert a string to an enumeration value in C#? I have an HTML select tag containing the values of an enumeration. When the page is posted, I want to pick up the value (which will be in the form of a string) and convert it to the corresponding enumeration value. In an ideal world, I could do something like this: StatusEnum MyStatus = StatusEnum.Parse(&quot;Active&quot;);  but that isn't a valid code.
86715
I'm looking at some GXT code for GWT and I ran across this use of Generics that I can't find another example of in the Java tutorials. The class name is  if you want to look at all of the code.  Here are the important parts:  private RpcMap map;  public &lt;X&gt; X get(String property) {   if (allowNestedValues &amp;&amp; NestedModelUtil.isNestedProperty(property)) {     return (X)NestedModelUtil.getNestedValue(this, property);   }   return map == null ? null : (X) map.get(property); }   X is defined nowhere else in the class or anywhere in the hierarchy, and when I hit "go to declaration" in eclipse it just goes to the &lt;X&gt; in the public method signature.  I've tried to call this method with the following two examples to see what happens:  public Date getExpiredate() {     return  get("expiredate"); }  public String getSubject() {     return  get("subject"); }   They compile and show no errors or warnings.  I would think at the very least I would have to do a cast to get this to work.  Does this mean that Generics allow a magic return value that can be anything and will just blow up at runtime?  This seems counter to what generics are supposed to do.  Can anyone explain this to me and possibly give me a link to some documentation that explains this a little better?  I've went through Sun's 23 page pdf on generics and every example of a return value is defined either at the class level or is in one of the parameters passed in.
38779
I'm new to Apex and development in general... So, I'm playing around coding and doing some badges in Trailhead. I created a small piece of code, as below, and have no actual use: public class GenderIdentifier {     public void DetermineGender(String Gender){                 if (Gender == 'Man'){             Gender = 'Male';             } else {             Gender = 'Female';         }            } }  If I make the method 'non-static' it works, and I can see it working fine executing as a anonymous block. If I make it static, I can't execute the anonymous block, as I get an Error like:  Non static method cannot be referenced from a static context: void GenderIdentifier.DetermineGender(String)  What am I doing wrong? It would make sense to have a method like this as Static. I think I misunderstood the concept of static methods.
110792
The code looks like below:  namespace Test {     public interface IMyClass     {         List&lt;IMyClass&gt; GetList();     }      public class MyClass : IMyClass     {         public List&lt;IMyClass&gt; GetList()         {             return new List&lt;IMyClass&gt;();         }     } }   When I Run Code Analysis i get the following recommendation.     Warning    3   CA1002 : Microsoft.Design : Change 'List' in 'IMyClass.GetList()' to use Collection, ReadOnlyCollection or KeyedCollection   How should I fix this and what is good practice here?
98970
I'm not sure if this is a strange thing to do or not, or if it is some how code smell...but I was wondering if there was a way (some sort of oop pattern would be nice) to "cast" a base type to a form of its derived type. I know this makes little sense as the derived type will have additional functionality that the parent doesn't offer which is in its self not fundamentally sound. But is there some way to do this? Here is a code example to so I can better explain what I"m asking.  public class SomeBaseClass {     public string GetBaseClassName {get;set;}     public bool BooleanEvaluator {get;set;} }  public class SomeDerivedClass : SomeBaseClass {     public void Insert(SqlConnection connection) {           //...random connection stuff           cmd.Parameters["IsItTrue"].Value = this.BooleanEvalutar;           //...     } }  public static void Main(object[] args) {     SomeBaseClass baseClass = new SomeBaseClass();     SomeDerivedClass derClass = (SomeDerivedClass)baseClass;      derClass.Insert(new sqlConnection()); }   I know this seems goofy but is there any way to accomplish something of this sort?
91389
I can't seem to access instance members of the surrounding class from inside an enum, as I could from inside an inner class. Does that mean enums are static? Is there any access to the scope of the surrounding class's instance, or do I have to pass the instance into the enum's method where I need it?  public class Universe {     public final int theAnswer;      public enum Planet {         // ...         EARTH(...);         // ...          // ... constructor etc.          public int deepThought() {             // -&gt; "No enclosing instance of type 'Universe' is accessible in this scope"             return Universe.this.theAnswer;         }     }      public Universe(int locallyUniversalAnswer) {         this.theAnswer = locallyUniversalAnswer;     } }
17522
I've been programming in Java for a while and just got thrown onto a project that's written entirely in C#.  I'm trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#'s enums seem to be more simplistic than the Java 1.5+ implementation.  Can anyone enumerate the differences between C# and Java enums, and how to overcome the differences?  (I don't want to start a language flame war, I just want to know how to do some things in C# that I used to do in Java).  For example, could someone post a C# counterpart to Sun's famous Planet enum example?  public enum Planet {   MERCURY (3.303e+23, 2.4397e6),   VENUS   (4.869e+24, 6.0518e6),   EARTH   (5.976e+24, 6.37814e6),   MARS    (6.421e+23, 3.3972e6),   JUPITER (1.9e+27,   7.1492e7),   SATURN  (5.688e+26, 6.0268e7),   URANUS  (8.686e+25, 2.5559e7),   NEPTUNE (1.024e+26, 2.4746e7),   PLUTO   (1.27e+22,  1.137e6);    private final double mass;   // in kilograms   private final double radius; // in meters   Planet(double mass, double radius) {       this.mass = mass;       this.radius = radius;   }   public double mass()   { return mass; }   public double radius() { return radius; }    // universal gravitational constant  (m3 kg-1 s-2)   public static final double G = 6.67300E-11;    public double surfaceGravity() {       return G * mass / (radius * radius);   }   public double surfaceWeight(double otherMass) {       return otherMass * surfaceGravity();   } }  // Example usage (slight modification of Sun's example): public static void main(String[] args) {     Planet pEarth = Planet.EARTH;     double earthRadius = pEarth.radius(); // Just threw it in to show usage      // Argument passed in is earth Weight.  Calculate weight on each planet:     double earthWeight = Double.parseDouble(args[0]);     double mass = earthWeight/pEarth.surfaceGravity();     for (Planet p : Planet.values())        System.out.printf("Your weight on %s is %f%n",                          p, p.surfaceWeight(mass)); }  // Example output: $ java Planet 175 Your weight on MERCURY is 66.107583 Your weight on VENUS is 158.374842 [etc ...]
5983
The title is kind of obscure. What I want to know is if this is possible:  string typeName = &lt;read type name from somwhere&gt;; Type myType = Type.GetType(typeName);  MyGenericClass&lt;myType&gt; myGenericClass = new MyGenericClass&lt;myType&gt;();   Obviously, MyGenericClass is described as:  public class MyGenericClass&lt;T&gt;   Right now, the compiler complains that 'The type or namespace 'myType' could not be found." There has got to be a way to do this.
59063
I'm programming WCF using the ChannelFactory which expects a type in order to call the CreateChannel method. For example:   IProxy proxy = ChannelFactory&lt;IProxy&gt;.CreateChannel(...);   In my case I'm doing routing so I don't know what type my channel factory will be using. I can parse a message header to determine the type but I hit a brick wall there because even if I have an instance of Type I can't pass that where ChannelFactory expects a generic type.   Another way of restating this problem in very simple terms would be that I'm attempting to do something like this:  string listtype = Console.ReadLine(); // say "System.Int32" Type t = Type.GetType( listtype); List&lt;t&gt; myIntegers = new List&lt;&gt;(); // does not compile, expects a "type" List&lt;typeof(t)&gt; myIntegers = new List&lt;typeof(t)&gt;(); // interesting - type must resolve at compile time?   Is there an approach to this I can leverage within C#?
25435
Is there a way to create an instance of a class based on the fact I know the name of the class at runtime. Basically I would have the name of the class in a string.
34848
I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception.  For instance:    System.out.println("obj: "+obj);   instead of:  System.out.println("obj: "+obj.toString());   Is there any difference apart from the null case? Can the latter case work, when the former does not?  Edit: What exactly is done, in case of the implicit call?
80615
I am working on code generation and ran into a snag with generics. Here is a "simplified" version of what is causing me issues.  Dictionary&lt;string, DateTime&gt; dictionary = new Dictionary&lt;string, DateTime&gt;(); string text = dictionary.GetType().FullName;   With the above code snippet the value of text is as follows:   System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0,   Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib,   Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]   (Line breaks added for better readability.)  Is there a way to get the type name (type) in a different format without parsing the above string? I desire the following result for text:  System.Collections.Generic.Dictionary&lt;System.String, System.DateTime&gt;
7075
I have a function which parses one string into two strings.  In C# I would declare it like this:  void ParseQuery(string toParse, out string search, out string sort) {     ... }   and I'd call it like this:  string searchOutput, sortOutput; ParseQuery(userInput, out searchOutput, out sortOutput);   The current project has to be done in C++/CLI.  I've tried  using System::Runtime::InteropServices;  ...  void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort) {     ... }   but if I call it like this:  String ^ searchOutput, ^ sortOutput; ParseQuery(userInput, [Out] searchOutput, [Out] sortOutput);   I get a compiler error, and if I call it like this:  String ^ searchOutput, ^ sortOutput; ParseQuery(userInput, searchOutput, sortOutput);   then I get an error at runtime.  How should I declare and call my function?
15373
I'm mainly a C# developer, but I'm currently working on a project in Python.  How can I represent the equivalent of an Enum in Python?
104601
Type.GetType("System.String")   Is there a lookup for the aliases available somewhere?  Type.GetType("string")   returns null.
80627
I have a string variable that represents the name of a custom class. Example:   string s = "Customer";   I will need to create an arraylist of customers. So, the syntax needed is:  List&lt;Customer&gt; cust = new ..    How do I convert the string s to be able to create this arraylist on runtime?
67578
I haven't written any C++ in years and now I'm trying to get back into it. I then ran across this and thought about giving up:  typedef enum TokenType {     blah1   = 0x00000000,     blah2   = 0X01000000,     blah3   = 0X02000000 } TokenType;   What is this? Why is the typedef keyword used here? Why does the name TokenType appear twice in this declaration? How are the semantics different from this:  enum TokenType {     blah1 = 0x00000000,     blah2=0x01000000,     blah3=0x02000000 };