Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What kind of collection?

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Greetings,

I need your advice on something here. I'm currently working on an application where I need to maintain a collection that will store a string value and an associated value of type double.

Example:
"Requirements" .2
"Code" .4
..etc

Essentially, project phases and their associated weights. I'd like to use a collection that will allow me to reference either the string value, or the double ("weight") by an index. What kind of collection should I be using?

Currently, Im using 2 arrays...I just don't like the fact that they're independant of one another..
ex:

double[] yWeights = { .2, //Requirements
.2, //Design
.05, //Complete
.05, //Test
.2, //Analysis
.1, //UAT
.4 }; //Code

string[] xValues = { "Requirements",
"Design",
"Complete",
"Test",
"Analysis",
"UAT",
"Code" };

thanks in advance - let me know if I'm not makin sense :)

 

I believe that's what the Hashtable class is designed for.

(See System.Collections)

 
you could look into using a Dictionary object.

Code:
using System.Collections.Generic;
...

Dictionary<string, double> weights = new Dictionary<string, double>();
weights.Add("Requirements", 0.2D);
// etc...

double aWeight = weights["Requirement"];



mr s. <;)

 
Hey there,
Yes, that's what I was thinking at first too...But it's a bit of an issue getting values out by index number. No issue getting them out by key - that doesn't really help me though.

Ex:
Code:
        static void hashtabledemo()
        {
            System.Collections.Hashtable weightedVals = new System.Collections.Hashtable();
            weightedVals.Add("Requirements", .2);
            weightedVals.Add("Design", .2);
            weightedVals.Add("Complete", .05);
            weightedVals.Add("Test", .05);
            weightedVals.Add("Analysis", .2);
            weightedVals.Add("UAT", .1);
            weightedVals.Add("Code", .4);

            for (int i=0; i < weightedVals.Count; i++)
            {
                Console.WriteLine(weightedVals[0]);//prints nothing
                Console.WriteLine(weightedVals["Requirements"]);//prints correct value - but i need to refer to elements by index
            }
        }

I run into a similar challenege utilizing dictionary (lack of ability to refer to elements by index, rathert then a string index. Is there a way around this? Thanks for the responses :)

 
Surely if you had a class phase with two public properties PhaseName and Weighting and produced a collection (phases) of phase you could access it as:

collPhases.Item(x).PhaseName and collPhases.Item(x).Weighting

where collPhases is your collection and x is the index of interest

Hope this helps.

[vampire][bat]
 
... sorry round brackets VB; square brackets C# so

collPhases.Item(x).PhaseName and collPhases.Item(x).Weighting


should be

collPhases.Item[x].PhaseName and collPhases.Item[x].Weighting


[vampire][bat]
 

Suprised to hear that you want to access the items by index number. It doesn't look like there is any intrinsic order in the items you presented.

You should be able to use a foreach construct which gives you DictionaryEntry objects that contain key/value pairs.

Alternatively you could use a SortedList and include an ordinal in the value. Kind of like this:
[tt]
weightedVals.Add("1-Requirements", .2);
weightedVals.Add("2-Design", .2);
weightedVals.Add("3-Complete", .05);
:
:
[/tt]
Then the foreach should give you the values in the order you want.

 
From a hashtable you can get the ICollection of keys. Then you .MoveNext() on the keys and that allows you to iterate through the collection.

The next option would be to create your own class that manages the collection.

public class KeyValue
{
public string key;
public string value;

public KeyValue(string Key, string Value)
{
key = Key;
value = Value;
}
}

public class CustomHashTable
{
ArrayList items = new ArrayList();

public int Add(string key, string value)
{
items.Add(new KeyValue(key, value));
return items.Count - 1; //sends back the index
}

public string GetValue(int keyindex)
{
return ((KeyValue)items[keyindex]).value;
}

public string GetKey(int keyindex)
{
return ((KeyValue)items[keyindex]).key;
}

public string GetKey(string value)
{
foreach(KeyValue kv in items)
{
if (kv.value == value)
{
return kv.Key;
}
}
}
}

And so on. Seems strange that you are using an index to grab keyvalue pairs though.

Dictionary is also a good route.
 
Use a struct (Declare two members of the struct) and add that struct to an arraylist...
 
Hey all,
Thanks for everyones advice. Everyones questioning of utilizin an index to grab key value pairs made me rethink some logic. I've reorg'd some stuff to avoid having a global collection with project phases and associated weights and opted for something a bit more flexible. Thanks for everyones help!





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top