none
Passing var object to a function? RRS feed

  • Question

  • Hi All,

     

    I'm using Linq To SQL to return results from a stored procedure in an SQL2000 database.

     

    Once I have the result object, I iterate through it using a foreach statement.

     

    I'd like to be able to pass the record object to a function / method, but it looks like i can't pass an object of type var.

     

    Can anyone tell me how I go about doing this?

     

     

    Code Snippet

    var query = from outon in db.stpOutstandingOngoing_Servnum_V2()

    orderby outon.datecreated ascending, recid ascending

    select outon;

    foreach (var rec in query)

    {

    if (rec.createdby == 10)

    {

    AMethod(rec);

    }

    }

     

     

     

    And I'd like to pass the 'rec' object to a method:

     

    Code Snippet

    public void AMethod(var rec)

    {

     

    }

     

     

    Can anyone help me work out how to do this?

     

    Thank you!

     

    pt

    Wednesday, April 9, 2008 4:41 AM

Answers

  • In C# 2008, var keyword is used for type inference. It is not an arbitrary type as it is in JavaScript. Consider this code:
    var i = 10;      // i is an int
    var d = 10.002     // d  is double
    var query = from c in db.Customers where ....        //query is IEnumerable<Customers>

    If you have a function taking double as a parameter, you can pass d. If any function taking int, you can pass i. The var keyword is used for type inference only. Once the value of the keyword is infered from the RHS of the assignment, then it is embedded with the variable that it is of a particular type. Initializing is must when using the var keyword.
    If you write:
    var p;
    It is not allowed. Because the type of p can't be infered.
    So make your function to take the data type of your query parameter as. Hover the mouse over the query object, tool tip will show the data type.

    Saturday, April 12, 2008 1:40 PM

All replies

  • Hi,

     

    the result of your query is of type IQueryable<T>. Since you select outon the type T is the type of outon.

    Assume type of outon in ClassOuton you can write your query as

    IQueryable<ClassOuton> query = ....

    IQueryable<T> derives from class IEnumerable<T> and your method signature can be

    public void AMethod(IEnumerable<ClassOuton> rec) {...}

     

    Hope this will help you.

    Philipp

    Wednesday, April 9, 2008 7:16 AM
  • Hi Philipp,

     

    Thanks for your reply, but I'm not sure I understand your comments.

     

    Is there any way, using code of similar complexity as in my original post, you can demonstrate what you mean?

     

    Many thanks!

     

    pt

    Thursday, April 10, 2008 5:15 AM
  • Hi pt,

     

    I used the NorthWind database in my examples.

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    // var DC = new NorthWindDataContext();

    NorthWindDataContext DC = new NorthWindDataContext();

    // var qry1 = from c in DC.Customers

    IOrderedQueryable<Customer> qry1 = from c in DC.Customers

    orderby c.CompanyName ascending,

    c.ContactName ascending

    select c;

     

    // IOrderedQueryable<Customer> derives from IQueryable<Customer>

    // and can be passed to DisplayQuery()

    DisplayQuery(qry1);

     

    // you can 'expand' your query by using

    // from c in qry1

    // and define new clauses like

    // where c.City == "London"

    //

    //var qry2 = from c in qry1

    IQueryable<Customer> qry2 = from c in qry1

    where c.City == "London"

    select c;

    DisplayQuery(qry2);

     

    // 'expand' your query in the method

    IQueryable<Customer> qry3 = CustomerInCity(qry1, "Berlin");

    DisplayQuery(qry3);

     

    // prevent closing console if in debug mode

    // hit any key to close

    Console.ReadKey();

    }

     

    // this could be your AMethod

    // you can also define this method as

    // static void DisplayQuery(IEnumerable<Customer> qry)

    // but in this case you can 'expand' your query

    // in the method body (without casting)

    static void DisplayQuery(IQueryable<Customer> qry)

    {

    Console.WriteLine("Display Query Result:");

    // foreach (var item in qry)

    foreach (Customer item in qry)

    {

    Console.WriteLine("{0}:{1}/{2}", item.CustomerID, item.CompanyName, item.ContactName);

    }

    }

     

    // you define method which expand the query and return the new query

    static IQueryable<Customer> CustomerInCity(IQueryable<Customer> customers, string city)

    {

    // var qry = from c in customers

    IQueryable<Customer> qry = from c in customers

    where c.City == city

    select c;

    return qry;

    }

    }

    }

     

     

    The important point is that you always select the whole object customer

    select c;

    If you select only some fields like

    select new {c.CompanyName, c.ContactName};

    it's not so easy because you need a type in the definition of the method

    static IQueryable<Customer> CustomerInCity(IQueryable<Customer> customers, string city)

    an the second select defines an anonymous type.

     

    In the definitions in my sample I used the truly types but I always (at least I hope I do) wrote the var usage in a comment line.

     

    regards

    Philipp

    Thursday, April 10, 2008 11:39 AM
  • In C# 2008, var keyword is used for type inference. It is not an arbitrary type as it is in JavaScript. Consider this code:
    var i = 10;      // i is an int
    var d = 10.002     // d  is double
    var query = from c in db.Customers where ....        //query is IEnumerable<Customers>

    If you have a function taking double as a parameter, you can pass d. If any function taking int, you can pass i. The var keyword is used for type inference only. Once the value of the keyword is infered from the RHS of the assignment, then it is embedded with the variable that it is of a particular type. Initializing is must when using the var keyword.
    If you write:
    var p;
    It is not allowed. Because the type of p can't be infered.
    So make your function to take the data type of your query parameter as. Hover the mouse over the query object, tool tip will show the data type.

    Saturday, April 12, 2008 1:40 PM
  • type of parameter in your function cannot be of type "var", beacause "var" is not a data type.
    compiler at run time determines the type of such variable.
    and so, it must be assigned of some value first.

    if you want ot pass a prameter of type of your record, write this type explicitly.
    if you need to pass parameter of different types, then you can:
    * override your method for each type
    * specify the type "object" whitch means, that any type can be passed, and in the body
    of method you can analyze th type of parameter using "is" keyword and then cast it usign "as" keyword.

    Thursday, July 10, 2008 11:35 AM
  •  xeonix X_x wrote:
    type of parameter in your function cannot be of type "var", beacause "var" is not a data type.
    compiler at run time determines the type of such variable.
    and so, it must be assigned of some value first.

    if you want ot pass a prameter of type of your record, write this type explicitly.
    if you need to pass parameter of different types, then you can:
    * override your method for each type
    * specify the type "object" whitch means, that any type can be passed, and in the body
    of method you can analyze th type of parameter using "is" keyword and then cast it usign "as" keyword.



    Hi xeonix_nya, the compiler infers data type during the coding, when you write the right hand side of the statement. THere is no need to compile or run the app for data tyep of var object to be infered.
    Sunday, August 17, 2008 11:41 AM
  •  xeonix X_x wrote:
    beacause "var" is not a data type. compiler at run time determines the type of such variable.

    Let me just correct that for the sake of future readers - var is determined at compile time.

    Tuesday, August 19, 2008 7:43 AM
    Answerer
  • The simple answer is that you cannot.

     

    A var is an inferred type - all function and method parameters and result sets must be of a specific type. Var is used to hold temporary result sets within method bodies, especially when using anonymous types.. e.g.

     

    Code Snippet
    var query = from c in db.Customers select new { c.CustomerID, c.CompanyName};

     

     

     

    However, if I wanted to pass this result set to a method, I'd need to use a specific class or structure.

     

     

     

    Tuesday, August 19, 2008 7:48 AM
    Answerer