locked
How to instantiate variable of Type T in generics

    Question

  • Hi,

    I am getting below error !

    Error ...( Cannot create an instance of the variable type 'T' because it does not have the new() constraint )

    see Line 4 below:

    - - - - - - - - - - - - - -- -  -- - - - - - - - - - - - - - - -- -  -- -  - - - - - - - - - - - - - -- -  -- -  - - - - - - - - - - - - - -- -  -- -

    1.TryGetListItemValue<T>(SPListItem item, string internalName, out T value)
    2.       {
    3.            bool flag;

    4.          value = new T();                            - - - - - > >//Here getting above Error !

    5.           try
    6.           {
    7.              object obj = item[internalName];
    8.              if (obj != null)
    9.              {
    10.                    value = (T)obj;
    11.                  return true;
    12.                }
                    flag = false;
                }
                catch (Exception)
                {
                    flag = false;
                }
                return flag;
    20.      }

     

    Thanks !

    Wednesday, September 28, 2011 1:38 PM

Answers

  • change if expression as below:

    if (obj != null && obj.GetType().Equals(T.GetType()))
       value = (T)obj;
       return true;
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    Thursday, September 29, 2011 12:11 AM

All replies

  • T in your example can be everything, even a type that does not have a default constructor.

    http://msdn.microsoft.com/en-us/library/d5x73970.aspx tells you about the constraints possible and if you want a default constructor, you need the where T : new()

    With kind regards,

    Konrad

    Wednesday, September 28, 2011 1:47 PM
  • mafab,

    You're going to need to add a new constraint to your class.  You need to add this whenever you plan to create an instance of any of your type T parameters.   Like this:

    class MyGenericClass<T> where T : new()

    You can read more about this constraint here:

    http://msdn.microsoft.com/en-us/library/x3y47hd4.aspx


    Tom Overton
    • Proposed as answer by RizwanSharp Thursday, September 29, 2011 5:28 AM
    Wednesday, September 28, 2011 1:50 PM
  • Hi,

    I used where T:new()

    thanks for it :)

    but i am getting below error ..

    ( 'string' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method )

    - - - - - - - - - - - - - -- -  -- - - - - - - - - - - - - - - -- -  -- -  - - - - - - - - - - - - - -- -  -- -  - - - - - - - - - - - - - -- -  -- -

    my Code:

     string s = null;
               bool flag = Common.TryGetListItemValue<string>(properties.ListItem, "EventId", out s);
               if (flag)

    Thanks !

    Wednesday, September 28, 2011 2:01 PM
  • String has no default constructor. Check http://msdn.microsoft.com/en-us/library/system.string.aspx.

    To call a default constructor, the class must have one.

    With kind regards,

    Konrad

    Wednesday, September 28, 2011 2:05 PM

  • mafab,

    Instead, call your generic method using a named type as T, one that has a public parameterless constructor. 

    class Myclass { public Myclass() {} }
     Myclass m = new Myclass();
     bool flag = Common.TryGetListItemValue<Myclass>(properties.ListItem, "EventId", out m); if (flag)

     

     


    Tom Overton
    • Edited by Tom_Overton Wednesday, September 28, 2011 2:16 PM
    Wednesday, September 28, 2011 2:15 PM
  • Do you even need to create a new T in your method?  Would it not be acceptable to just return null if no item can be found?
    Wednesday, September 28, 2011 2:33 PM
  • Hi,

    how to even need to create a new T in your method?

    i mean how to return null if no item found.

    Wednesday, September 28, 2011 2:52 PM
  • Hi,

    how to even need to create a new T in your method?

    i mean how to return null if no item found.

    just replace value = new T();  with value = null;
    Wednesday, September 28, 2011 4:01 PM
  • change if expression as below:

    if (obj != null && obj.GetType().Equals(T.GetType()))
       value = (T)obj;
       return true;
    }
    


    Any fool can know. The point is to understand.(Albert Einstein)
    Thursday, September 29, 2011 12:11 AM