locked
Suggestion for Tuple Attributes RRS feed

  • Question

  • User13020107 posted

    I like Tuples. Here is an example of how I think Tuples would be more effective:

    public Tuple<string[Description], int[Count], double[Price]> ReturnProductDescriptionInventoryLevelAndPrice(int prodId) 
        return "Hello World!", 42, 4.2; 
    }
    public void SomeOtherMethod()
    {
       var productInfo = ReturnProductDescriptionInventoryLevelAndPrice(5);
       var prodDescription = productInfo.Description;
       var inventoryLevel = productInfo.Count;
       var prodPrice = productInfo.Price;
    }
    By applying attributes to the Tuple, the result would be more descriptive (it would not use Item1, Item2, etc) and could be included in public interfaces.

    Jonathan

     

    Saturday, April 9, 2011 2:03 PM

All replies

  • User-964259575 posted

    Just an association. With "dynamic" in .NET 4 you could do things like this:

    private dynamic GetSomeData(bool flag)
    {
        if (flag)
        {
            return new
                {
                    Description = "Hello World!",
                    Count = 42,
                    Price = 4.2
                };
        }
        else
        {
            return new
                {
                    AnotherProperty = "xyz",
                    Value = true
                };
        }
    }
    
    public void SomeOtherMethod()
    {
        var x1 = GetSomeData(true);
        var description = x1.Description;
        var count = x1.Count;
        var price = x1.Price;
    
        var x2 = GetSomeData(false);
        var anotherProperty = x2.AnotherProperty;
        var value = x2.Value;
    
        var q = x2.Count; // gives RuntimeBinderException
    }

    Saturday, April 9, 2011 5:52 PM
  • User13020107 posted

    I would like to thank you for the suggestion.  I never thought of using the Dynamic in that manner.  I only thought of using the Dynamic when referring to some kind of outside scripting language.  I think it makes great sense as a private method and I will likely use this in the future. 

    It does not, however, address the issue of being able to use Tuples for return values on public interfaces.  I am guessing there is not a real code example now available for this.  I am guessing that Microsoft would need to make some changes to the compiler for a future version (hint: maybe for SP2?).

    Thank you anders as you have increased my personal toolkit with this dynamic suggestion.

    Jonathan

    Friday, April 15, 2011 10:42 AM
  • User-964259575 posted

    I haven't used "dynamic" myself in this manner, and I'm not convinced this would be considered a good practise, but maybe there are some cases where it would fit in nicely. It was just meant as an eye-opener, or thinking out of the box.

    For public interfaces I would recommend you to create custom classes, in your example something like this

    public class ProductDescriptionInventory
    {
        string Description { get; set; }
        int Count { get; set; }
        double Price { get; set; }
    }

    Saturday, April 16, 2011 3:13 PM