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();
}
3
  • 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. Commented Mar 10, 2011 at 10:19
  • 5
    Noooooo...... remove that catch block. Why do you think you need it? Commented Mar 10, 2011 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 Commented Mar 10, 2011 at 10:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In the OP's case, IEnumerable is probably not needed. A simple Tuple would do.
I´m using .NET 3.5 IENumerable was one of my first ideas but i cant get it straight. can u post me a example with IEnumerable please?
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;
}

Comments

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; }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.