2

i have a static class with a method that use linq and returns an object. my compiler don´t want to compile it because he needs a definition for the object. can you tell me which opinions i have to define the object?

i search for a tiny solution, i don´t want to create a extra class for it (if there is no need ?)

public static object GetWaveAnimation()
{
    return (from element in configurations.Elements("Animation")
            where element.Attribute("NAME").Value == "Wave"
            select new
                {
                    time = element.Attribute("TIMING").Value,
                    enable = element.Attribute("ENABLED").Value
                }).FirstOrDefault();
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
jwillmer
  • 3,570
  • 5
  • 37
  • 73
  • What's the error message? You are aware that you cannot use `var` to let the compiler infer the return type for you, but returning `object` should compile. – Mehrdad Afshari Mar 10 '11 at 10:19
  • 5
    Noooooo...... remove that catch block. Why do you think you need it? – Mark Byers Mar 10 '11 at 10:20
  • oh, you are right, in the beginning i havent the "FirstOrDefault()" and i wasen´t sure if the function throws a exception if it can´t find the XElement – jwillmer Mar 10 '11 at 10:35

3 Answers3

1

If you want a statically typed (and named) solution, you should create a separate class. There are some hacky ways of avoiding it, but it's not a good idea in general.

Another option is to return IEnumerable<Tuple<string, string>> if you're using .NET 4. That way you lose the "time" and "enabled" names, but keep the idea that it's a pair of strings.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

another solution: Hidden Features of C#?

// Useful? probably not.
private void foo()
{
    var user = AnonCast(GetUserTuple(), new { Name = default(string), Badges = default(int) });
    Console.WriteLine("Name: {0} Badges: {1}", user.Name, user.Badges);
}

object GetUserTuple()
{
    return new { Name = "dp", Badges = 5 };
}    

// Using the magic of Type Inference...
static T AnonCast<T>(object obj, T type)
{
   return (T) obj;
}
Community
  • 1
  • 1
jwillmer
  • 3,570
  • 5
  • 37
  • 73
0

For .net 3.5 just bite the bullet it's the cleanest looking solution.

public struct Wave{
     public X time;
     public Y enable;
}

public static Wave GetWaveAnimation()
    {
        try
        {
            return (from element in configurations.Elements("Animation")
                    where element.Attribute("NAME").Value == "Wave"
                    select new Wave
                        {
                            time = element.Attribute("TIMING").Value,
                            enable = element.Attribute("ENABLED").Value
                        }).FirstOrDefault();
        }
        catch { return null; }
    }

For .net 4.0 you can use dynamic keyword (but you can't call this method from outside your assembly or friend assemblies because anonymous types are internal.)

 public static dynamic GetWaveAnimation()
{
    try
    {
        return (from element in configurations.Elements("Animation")
                where element.Attribute("NAME").Value == "Wave"
                select new
                    {
                        time = element.Attribute("TIMING").Value,
                        enable = element.Attribute("ENABLED").Value
                    }).FirstOrDefault();
    }
    catch { return null; }
}

OR you have the Tuple Option

  public static Tuple<X,Y> GetWaveAnimation()
        {
            try
            {
                return (from element in configurations.Elements("Animation")
                        where element.Attribute("NAME").Value == "Wave"
                        select Tuple.Create(
                                   element.Attribute("TIMING").Value,
                                   element.Attribute("ENABLED").Value
                                )
                            }).FirstOrDefault();
            }
            catch { return null; }
        }
jbtule
  • 31,383
  • 12
  • 95
  • 128