How to Properly Declare Array of Custom Objects in Swift?

First, I’m going to assume you didn’t want 2d arrays. If you did, I’ll answer your question from that perspective below. var foundation = [baseMakeUp]() Creates an empty array of baseMakeUp called foundation. You can’t use subscripting to add elements to an array, you can only use it to change existing elements. Since your array … Read more

Determine if a type is static

static classes are declared abstract and sealed at the IL level. So, you can check IsAbstract property to handle both abstract classes and static classes in one go (for your use case). However, abstract classes are not the only types you can’t instantiate directly. You should check for things like interfaces (without the CoClass attribute) … Read more

How to instantiate an object with a private constructor in C#?

You can use one of the overloads of Activator.CreateInstance to do this: Activator.CreateInstance(Type type, bool nonPublic) Use true for the nonPublic argument. Because true matches a public or non-public default constructor; and false matches only a public default constructor. For example: class Program { public static void Main(string[] args) { Type type=typeof(Foo); Foo f=(Foo)Activator.CreateInstance(type,true); } … Read more

Instantiate a class from its textual name

Here’s what the method may look like: private static object MagicallyCreateInstance(string className) { var assembly = Assembly.GetExecutingAssembly(); var type = assembly.GetTypes() .First(t => t.Name == className); return Activator.CreateInstance(type); } The code above assumes that: you are looking for a class that is in the currently executing assembly (this can be adjusted – just change assembly … Read more

Can’t Instantiate Map…well why not?

The built-in Map is an interface, which cannot be instantiated. You can choose between lots of implementing concrete classes on the right side of your assignment, such as: ConcurrentHashMap HashMap LinkedHashMap TreeMap and many others. The Javadocs for Map lists many direct concrete implementations.

How does one instantiate an array of maps in Java?

Not strictly an answer to your question, but have you considered using a List instead? List<Map<String,Integer>> maps = new ArrayList<Map<String,Integer>>(); … maps.add(new HashMap<String,Integer>()); seems to work just fine. See Java theory and practice: Generics gotchas for a detailed explanation of why mixing arrays with generics is discouraged. Update: As mentioned by Drew in the comments, … Read more

Why do I get an error instantiating an interface?

The error message seems self-explanatory. You can’t instantiate an instance of an interface, and you’ve declared IUser as an interface. (The same rule applies to abstract classes.) The whole point of an interface is that it doesn’t do anything—there is no implementation provided for its methods. However, you can instantiate an instance of a class … Read more

tech