JavaSa
JavaSa

Reputation: 6242

Compilation error regarding declaring generic nested class

I get a compilation errors regarding this piece of code:

Error 1 Invalid token '(' in class, struct, or interface member declaration
Error 2 Cannot use more than one type in a for, using, fixed, or declaration

Any idea why? In addition, is it possible to declare dictionary as follows?

public class S
{
        private class ObInfo<T>
        {
            private string _type;
            private T _value;

            public ObInfo<T>(string i_Type, T Value)
            {
                this._type = i_Type;
                this._value = Value;
            }

            public ObInfo() 
               {}
       }

       private static Dictionary<int,ObInfo> sObj= new Dictionary<int,ObInfo>();
}

Upvotes: 4

Views: 231

Answers (3)

Eric Lippert
Eric Lippert

Reputation: 660445

SLaks' answer is fine, but to clarify one point: you asked why the error was happening, not how to fix it. The error is being reported because the compiler is reasoning that you intended to say:

   private class ObInfo<T>
   {
        public ObInfo<T> SomeMethodNameHere(string i_Type, T Value)

That is, it thinks you are trying to make a method -- or possibly a field or event -- and you have typed the return type, ObInfo<T> but forgotten the method name. Whether this is a method, field or event, the ( is unexpected, and so that's the error.

Obviously this is not the best error message possible, as it confused you. It might be better to add another heuristic to speciifcally detect the situation that you're in.

I'd be interested to know why you made this error. Did you think:

  • The ctor is a method whose name is the same as the class name, and the T is part of the class name.
  • The ctor is a generic method, and a generic method has to have a type parameter declared for it.
  • Something else.

?

If you thought the first thing: the T is not part of the class name. If you thought the second thing: if that were true then you'd be declaring a second type parameter in scope called T, which is a little confusing, no?

Upvotes: 7

Default Writer
Default Writer

Reputation: 2566

You can simply put static field inside the class. So you will have different static dictionaries for each implementation of generic class

public class S
{
        private class ObInfo<T>
        {
            private string _type;
            private T _value;

            public ObInfo(string i_Type, T Value)
            {
                this._type = i_Type;
                this._value = Value;
            }

            public ObInfo() 
            {}

           private static Dictionary<int,ObInfo<T>> sObj= new Dictionary<int,ObInfo<T>>();
       }
}

Upvotes: 1

SLaks
SLaks

Reputation: 888007

public ObInfo<T>(...) {

Constructors cannot take generic parameters.
Remove the <T> and everything will work.

All methods (and types) inside a class inherit that class's generic parameters; you should only create generic methods inside generic classes if the methods need a separate type parameter. (this should be avoided; it's very confusing)


Also, open generic types are not actually types; you cannot have a Dictionary<int,ObInfo> without specifying the type parameter for ObjInfo.
Instead, you can either use a non-generic interface for the dictionary, or move the type parameter to the outer class and have a separate dictionary per type parameter.

Upvotes: 7

Related Questions