none
What is the "type" of a class?

    Question

  • Sorry for the noob question. I'm coming from PHP to C#.

    I'm trying to create a method I can re-use and pass the name of a class (I think C# calls them "Models").

    What would the "type" be for ClassName? (If I call this function, I would pass in the name of a Class)

    getData(ClassName) { 
    connection.CreateTableAsync<ClassName>();
    ...
    }

    Like, if I have a class named "Recipes", it's type is "Recipes". So I would write in

    connection.CreateTableAsync<Recipes>(); 
    ...


    But I don't want to have to do this:

    Users.cs

    connection.CreateTableAsync<Users>()

    Recipes.cs

    connection.CreateTableAsync<Recipes>()

    Comments.cs

    connection.CreateTableAsync<Comments>()


    I'm not sure how to specify what the "type" is for a Class, so that I can create a general Database class that handles everything, like I would do in PHP. 

    I realize I'm only showing 1 line of code. Obviously, connecting to a SQLite database involves more than 1 line of code. The example code is irrelevant. I just don't want to drop the same 5 lines of code into every single class for every single table in my database. That's not what I would do in PHP, and I'm sure that's not how I should do it in C#.

    Sunday, May 26, 2019 7:42 PM

All replies

  • What you are looking for is called "Generics", and what you are calling the "type of the class" would be the Generic parameter type. You use it like this.

    public T getData<T>() { 
       connection.CreateTableAsync<T>(); 
       ...
       T myVariable = ...
       ...
       return myVariable;
    }
    
    // call it like this:
    User u = getData<User>()

    Note that we are using here the parameter T as your "ClassName". It can be called anything, doesn't have to be T, but T is traditional.

    Sunday, May 26, 2019 8:27 PM
    Moderator
  • Yes, Generics is what you're looking for, and I was just about to leave a reply along those lines, but then refreshed the page and saw that @Alberto already did. But, @Alberto left out two important bits in his example.

    public T GetData<T>() where T : class, new()
    {
        T myVariable = new T();
        connection.CreateTableAsync<T>();
        ...
        ...
        return T;
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, May 26, 2019 9:08 PM
    Moderator

  • public T GetData<T>() where T : class, new()
    {
        T myVariable = new T();
        connection.CreateTableAsync<T>();
        ...
        ...
        return T;
    }

    I kind of flubbed my reply too. Oh well, let me try again. It should probably be more like this:

    public T GetData<T>() where T : class, new()
    {
        T myData = new T();
        connection.CreateTableAsync<myData>();
        ...
        ...
        return myData;
    }

    Although, some of this will depend on what you intend to do in the CreateTableAsync() method … you haven't said.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, May 26, 2019 10:10 PM
    Moderator
  • I am pretty sure that your first version had the correct call for CreateTableAsync:

     connection.CreateTableAsync<T>();

    That is, it should be <T> and not <myData>.

    Monday, May 27, 2019 12:44 PM
    Moderator
  • Yes, you're correct, Alberto. I'm not familiar with SQLite (I use SQL Server) and didn't realize (until I Googled after I saw your reply), that the CreateTableAsync was a method on the SQLite connection object. Thanks for the clarification!  =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, May 27, 2019 2:47 PM
    Moderator