tags:

views:

88

answers:

3

Hi,

I am probably totally missing the point here but....

How can I pass a 'var' into another method?

(I am using linq to load XML into an Enumerable list of objects).

I have differernt object types (with different fields), but the final step of my process is the same regardless of which object is being used.

XNamespace xmlns = ScehmaName;
var result = from e in XElement.Load(XMLDocumentLocation).Elements(xmlns + ElementName)
             select new Object1
             {
                 Field1 = (string)e.Element(xmlns + "field1"),
                 Field2 = (string)e.Element(xmlns + "field2")
             };

var result2 = Enumerable.Distinct(result);

This code will vary for the different type of XML files that will be processed. But I then want to iterate though the code to check for various issues:

foreach (var w in result2)
{
    if (w.CheckIT())
    {
        //do something
    }  
}

What I would love is the final step to be in a method in the base class, and I can pass the 'var' varible into it from each child class.

+2  A: 

Your objects are typed as Object1. You should be able to create a method like this:

private void CheckAll(IEnumerable<Object1> result)
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}

And use it like this:

CheckAll(result2);

If your functions all deal with different types and CheckIt is not established in a common base type, then this isn't possible (without reflection) in C# 3. C# 4 introduces dynamic typing (specifically "duck typing" in this case) that will allow this.

Adam Robinson
+2  A: 

The type of w is Object1 (in the code as written above). You can find this yourself by hovering your mouse over the var keyword and seeing the tooltip that Visual Studio shows you.

It's hard to tell from your question, but it sounds like the CheckIt method is defined on a base type of Object1. If it is you can write your method to check them as

protected void CheckAll<T>(IEnumerable<T> result) where T : <base type with CheckIt method>
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}
Ben Lings
Ben,Your assumption is correct. I will have Object2, Object3 etc.The checkIt method is defined in the base class of each object.
Danny
Ben,I can't seem ot get the code your provided to compile.It's not liking the <base type with CheckIt method> part of the code
Danny
Replace `<base type with CheckIt method>` with the name of base class that has the `CheckIt` method.
Ben Lings
Doh! Sorry... was being a bit thick there... thanks! Will try that now.
Danny
Thanks so much Ben. It did just what I needed, and worked really well.
Danny
+1  A: 

It is worth noting that there is nothing like "var" variable. Any variable declared using the var keyword has an actual type (that is inferred by the compiler). You only don't have to write this type explicitly, but it still there (and the compiler replaces the var declaration with the actual type when it starts processing your code). This means that:

var n = 10 
// .. is exactly the same thing as:
int n = 10

And in your example:

var result = 
  from e in ... select new Object1 { ... };
// .. is exactly the same thing as:
IEnumerable<Object1> result = 
  from e in ... select new Object1 { ... };

The situation becomes more complicated if you use anonymous types (e.g. new { Name="Test" }) because these don't have a name that you could use in your code (so you're forced to work with them using var), but since your code doesn't contain anonymous types, this shouldn't be a problem for you. It is useful to realize that var is feature that's technically not realted to anonymous types in any way.

Tomas Petricek