Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. instantiating a generic object [modified]

instantiating a generic object [modified]

Scheduled Pinned Locked Moved C#
questiondatabasesysadmin
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mabrahao
    wrote on last edited by
    #1

    Hi guys, i have a method like this: public List listObj() {} and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks. The code: public class MysqlDireto { private static MysqlDireto _instancia; private static string connectionstring = "Server=;Database=;Uid=;Pwd=;"; public static MysqlDireto Instancia { get { return _instancia ?? (_instancia = new MysqlDireto()); } } public List<T> ObterLista<T>() { MySqlConnection conn = new MySqlConnection(connectionstring); string comando = "SELECT * FROM " + typeof(T).Name; MySqlCommand cmd = new MySqlCommand(comando, conn); conn.Open(); MySqlDataReader leitor = cmd.ExecuteReader(); List<T> lista = new List<T>(); if (leitor.HasRows) { while (leitor.Read()) { T obj = Foo<T>(); obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null); lista.Add(obj); } } conn.Close(); return lista; } public T Foo<T>() where T : class, new() { return new T(); } }

    modified on Wednesday, June 8, 2011 2:11 PM

    E 1 Reply Last reply
    0
    • M mabrahao

      Hi guys, i have a method like this: public List listObj() {} and i want to instantiate a object with this type T, i tried this: T obj = new T(); doesnt work, so i tried T obj = default(T); and doesnt work too... how can i instantiete this obj??? Thanks. The code: public class MysqlDireto { private static MysqlDireto _instancia; private static string connectionstring = "Server=;Database=;Uid=;Pwd=;"; public static MysqlDireto Instancia { get { return _instancia ?? (_instancia = new MysqlDireto()); } } public List<T> ObterLista<T>() { MySqlConnection conn = new MySqlConnection(connectionstring); string comando = "SELECT * FROM " + typeof(T).Name; MySqlCommand cmd = new MySqlCommand(comando, conn); conn.Open(); MySqlDataReader leitor = cmd.ExecuteReader(); List<T> lista = new List<T>(); if (leitor.HasRows) { while (leitor.Read()) { T obj = Foo<T>(); obj.GetType().GetProperty("Id").SetValue(obj, leitor["Id"], null); lista.Add(obj); } } conn.Close(); return lista; } public T Foo<T>() where T : class, new() { return new T(); } }

      modified on Wednesday, June 8, 2011 2:11 PM

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      you have to add new() in the where clause of the generic definition.

      public T Foo() where T : new(){
      return new T();
      }

      Or such

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      M 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        you have to add new() in the where clause of the generic definition.

        public T Foo() where T : new(){
        return new T();
        }

        Or such

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        M Offline
        M Offline
        mabrahao
        wrote on last edited by
        #3

        i got this msg: Error 800 'T' 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

        D E 2 Replies Last reply
        0
        • M mabrahao

          i got this msg: Error 800 'T' 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

          D Offline
          D Offline
          dasblinkenlight
          wrote on last edited by
          #4

          I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).

          M 1 Reply Last reply
          0
          • M mabrahao

            i got this msg: Error 800 'T' 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

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            public class Test {
            public Test() {

            	}
            }
            public class Bar {
            	public static void Test<T>() where T : Test, new() {
            		T t = new T();
            	}
            }
            

            ...
            Bar.Test <Test>();

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            M 1 Reply Last reply
            0
            • D dasblinkenlight

              I think the error message is clear about what you need to fix: pass your generic class a type that can be instantiated with new T() (i.e. a non-abstract, with a parameterless constructor).

              M Offline
              M Offline
              mabrahao
              wrote on last edited by
              #6

              but the T class is a non-abstract and has no constructor parameter

              D 1 Reply Last reply
              0
              • M mabrahao

                but the T class is a non-abstract and has no constructor parameter

                D Offline
                D Offline
                dasblinkenlight
                wrote on last edited by
                #7

                The class that you pass as the generic parameter for T must be like the 'Test' class in the example that Ennis posted below.

                1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  public class Test {
                  public Test() {

                  	}
                  }
                  public class Bar {
                  	public static void Test<T>() where T : Test, new() {
                  		T t = new T();
                  	}
                  }
                  

                  ...
                  Bar.Test <Test>();

                  Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                  M Offline
                  M Offline
                  mabrahao
                  wrote on last edited by
                  #8

                  Thanks a lot, it was missing "Test,"

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups