List and Pair of Strings

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I need to hold a list of values pairs.
Basically I need to pass a list of RSS channels Titles/Urls.

I could create a List<Channel> and where channel has two properties:
Title and Url.
But since this is something so simple and that I only use once I think
this might be "to much for that".
I was wondering if there is another option.

Or if you think I should create a Channel class for this, please let
me know.

Thanks,
Miguel
 
Hello,

I need to hold a list of values pairs.
Basically I need to pass a list of RSS channels Titles/Urls.

I could create a List<Channel> and where channel has two properties:
Title and Url.
But since this is something so simple and that I only use once I think
this might be "to much for that".

Why? What disadvantage is there to creating a simple container class that
precisely describes the relationship between the two values and keeps them
in one place?
I was wondering if there is another option.

Sure, there are always lots of other options:

-- .NET 4 includes a series of "Tuple" generic classes, with one type
parameter for each property declared in the class. If you're able to use
..NET 4, you can just use that implementation, or you could define a
similar one. At its most basic, it would look something like this:

class Tuple<T1, T2>
{
T1 _t1;
T2 _t2;

public Tuple(T1 t1, T2 t2)
{
_t1 = t1;
_t2 = t2;
}

public Item1 { get { return _t1; } }
public Item2 { get { return _t2; } }
}

The .NET implemention includes implementations of the
IStructuralEquatable, IStructuralComparable, and IComparable interfaces
(the first two also being new to .NET 4). If you only need the container
behavior, you could skip doing that. Either way, once you've got the
"Tuple" classes, you don't need to declare a whole new class just for
simple containers.

-- You could maintain parallel structures, such as List<Title> and
List<Url>

Or if you think I should create a Channel class for this, please let
me know.

Personally, I don't see anything wrong with creating a dedicated container
class. What you _should_ do is up to you, and depends mostly just on what
you want the code to look like.

Pete
 
Hi,
I need to hold a list of values pairs.
Basically I need to pass a list of RSS channels Titles/Urls.

I could create a List<Channel> and where channel has two properties:
Title and Url.
But since this is something so simple and that I only use once I think
this might be "to much for that".
I was wondering if there is another option.

Or if you think I should create a Channel class for this, please let
me know.

how about the KeyValuePair structure?

List<KeyValuePair<string, string>> lstChannels =
new List<KeyValuePair<string, string>>();
KeyValuePair<string, string> kpvChannel=
new KeyValuePair<string, string>("MyTitle","MyURL");
lstChannels.Add(kpvChannel);

Cheers,
Olaf
 
Back
Top