Bug 6311 - Add Bundle.Add() methods
Summary: Add Bundle.Add() methods
Status: ASSIGNED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 4.2.x
Hardware: PC Mac OS
: Low enhancement
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2012-07-27 15:24 UTC by Jonathan Pryor
Modified: 2016-09-02 21:13 UTC (History)
3 users (show)

See Also:
Tags: XATriaged
Is this bug a regression?: ---
Last known good build:


Attachments

Description Jonathan Pryor 2012-07-27 15:24:11 UTC
The developer is supposed to create & fill Bundle instances:

    Bundle b = new Bundle ();
    b.PutString ("InputText", "text from client")

If Bundle provided Add() overloads, we could use collection initializers instead:

    var b = new Bundle () {
        { "InputText", "text from client" },
    };
Comment 1 Atsushi Eno 2012-08-07 05:02:16 UTC
I don't think it is good addition. This C# syntax makes use of collection initializer, and Bundle is not a mere collection of string. The first argument to PutString() is meant to be a key to the value, and does not accept more than two strings. And there are many other variants of the second argument. Collection initializer requires you to implement IEnumerable, but Bundle is not an IEnumerable of string.
Comment 2 Jonathan Pryor 2012-08-07 11:52:24 UTC
> Bundle is not a mere collection of string.

Correct, it's a collection of keys to values, just like Dictionary<TKey, TValue>, which does support collection initializers. Just provide an Add() method with all the required arguments:

> partial class Bundle {
>     public void Add (string key, string value)
>     {
>         PutString (key, value);
>     }
> 
>     public void Add (string key, short value)
>     {
>         PutShort (key, value);
>     }
>     // ...
> }

Just like Dictionary<TKey, TValue>, this would allow specifying both the key and the value in the initializer:

> var b = new Bundle () {
>     { "url", "https://example.com" },
>     { "port", (short) 80 },
> };

The Add() method can be overloaded to take all of value types as well.

As for implementing IEnumerable, yes that's required, but it's also fairly trivial to implement:

> partial class Bundle : IEnumerable {
>     public void IEnumerator GetEnumerator ()
>     {
>         foreach (var key in KeySet ())
>             yield return Get (key);
>     }
> }

Note You need to log in before you can comment on or make changes to this bug.