none
What does these lines of code do>? RRS feed

  • Question

  • Hi there im tring to learn about linq by reading the unit 18 of  Sams.Asp.net.3.5.unleashed book written by Stephen walther. i have some problem with some of line of code that i specify them with *
     class GenericMethods
      {
      public static List<T> GetListFromCommand<T>(SqlCommand Command) 1*
       where T : ICreatable, new( ) 2*
       {
        List<T> results = new List<T>( );
        using (Command.Connection)
         {
          Command.Connection.Open( );
          SqlDataReader reader = Command.ExecuteReader( );
          while (reader.Read( ))
           {
            T NewThing = new T( );3 * 
            NewThing.Create( reader );4 * 
            results.Add( NewThing ); 5*
           }
          return results;
         }
       }
      
      }
      public interface ICreatable6 *
      {
       
       void Create(SqlDataReader reader); 7*  }
      class Movie:ICreatable
      {
      
       public int Id { get; set; }
       public string Title { get; set; }
       public void Create( SqlDataReader reader) 8*
       {
        Id = (int)reader["Id"];
        Title = (string)reader["Title"];
       }
    
    
    }
    

    *1)what does <T> do there
    *2)what happen there what does the "where" do and  ICreatable and new() what language they are!
    *3) wow i can't understand it anymore. its made what kind of instand!
    *4) how does the Create Run. why we use Interface, can't we use a function.
    *5)what will add into generic list
    *6) are we hade to use interface or we can use it in another way>
    *7) how we use this function
    *8) what happen there.
    ----------------------------------------------
    and of cours there is another line of code that use the above classes:
    string conString = WebConfigurationManager.ConnectionStrings[“con”].ConnectionString;
    SqlConnection con = new SqlConnection(conString);
    SqlCommand cmd = new SqlCommand(“SELECT Id, Title FROM Movie”, con);
    List<Movie> movies = GenericMethods.GetListFromCommand<Movie>(cmd);*9
    
    *9) whats happen, movie class used as a type!!!!

    Saturday, July 30, 2011 3:04 PM

Answers

  • Hi Hussein,

     

    IMHO, your questions are not related to Linq, maybe you should try to post it in a more appropriate forum.

    I think your questions related mostly to C# Generics.

    You can find more information about generics here: http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx

     

    HTH


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Monday, August 1, 2011 3:24 PM
  • Hi Hussein,

     

    public static List<T> GetListFromCommand<T>(SqlCommand Command) 1*
     where T : ICreatable, new() 
    

     

    This is a generic method that can be called for any types that apply to the given constraints:

    - the type has to implement the ICreatable interface and needs a default constructor (new)

    The result is a List of Ts. They will be created in the line T NewThing = new T(); and be added to the result in line results.Add(NewThing);

    The interface ICreatable is to define a contract of T (the Thing in your example) to verify that the method Create(SqlDataReader) exists.

    Your code reads the result of the sql statement and creates an instance of T, in this case a Movie, for each row of the sql result.

    Hope this helps, but Crick is right, this isn't Linq2Sql it is just the C# generics concept.


    "It's time to kick ass and chew bubble gum... and I'm all outta gum." - Duke Nukem
    Monday, August 1, 2011 4:56 PM

All replies

  • Hi Hussein,

     

    IMHO, your questions are not related to Linq, maybe you should try to post it in a more appropriate forum.

    I think your questions related mostly to C# Generics.

    You can find more information about generics here: http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx

     

    HTH


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    Monday, August 1, 2011 3:24 PM
  • Hi Hussein,

     

    public static List<T> GetListFromCommand<T>(SqlCommand Command) 1*
     where T : ICreatable, new() 
    

     

    This is a generic method that can be called for any types that apply to the given constraints:

    - the type has to implement the ICreatable interface and needs a default constructor (new)

    The result is a List of Ts. They will be created in the line T NewThing = new T(); and be added to the result in line results.Add(NewThing);

    The interface ICreatable is to define a contract of T (the Thing in your example) to verify that the method Create(SqlDataReader) exists.

    Your code reads the result of the sql statement and creates an instance of T, in this case a Movie, for each row of the sql result.

    Hope this helps, but Crick is right, this isn't Linq2Sql it is just the C# generics concept.


    "It's time to kick ass and chew bubble gum... and I'm all outta gum." - Duke Nukem
    Monday, August 1, 2011 4:56 PM