简体   繁体   中英

Calling a hashtable with a string in C#

As you can see I have and hashtable that is called by GetEdinburgh() but this programs needs to run mutable times and each time it needs to call a diffrent hashtable. how do I replace the GetEdinburgh() with a string like GetTown() ?

static Hashtable GetEdinburgh()
{
    Hashtable hashtable = new Hashtable();
    hashtable.Add("Aberdeen", 129);
    hashtable.Add("Ayr", 79);
    hashtable.Add("Fort_William", 131);
    hashtable.Add("Glasgow", 43);
    hashtable.Add("Inverness", 154);
    hashtable.Add("St_Andrews", 50);
    hashtable.Add("Stirling", 36);
    return hashtable;
}


static void Main(string[] args)
{
    int total = 1000;
    string Town = "";

    Hashtable hashtable = GetEdinburgh(); //how can I change this to a string?
    foreach (DictionaryEntry entry in hashtable)
    {
        if (Convert.ToInt32(entry.Value) < total)
        {
            total = Convert.ToInt32(entry.Value);
            Town = entry.Key.ToString();
        }

    }
    Console.WriteLine(Town + ", " + total + "km");
    Console.ReadLine();
}

I might have not been specific about my problem. The current code above works fine but I need to expand it. I need to call more hash tables than the supplied one above but I cant call it directly. I need to have a string value that changes each time the a loop is implemented to call a new Table. But I cant convert a system.Collections.hashtable to string.

Better you can write a method like this:

static Hashtable GetHashtable(string Table )
    {
        Hashtable hashtable = new Hashtable();
           switch(table)
          {
        case "Edinburgh":
        hashtable.Add("Aberdeen", 129);
        hashtable.Add("Ayr", 79);
        hashtable.Add("Fort_William", 131);
        hashtable.Add("Glasgow", 43);
        hashtable.Add("Inverness", 154);
        hashtable.Add("St_Andrews", 50);
        hashtable.Add("Stirling", 36);
        break;
           ................
        return hashtable;
    }

and you can pass the string to the method and get the values.

Click answered if the answer really answers your question..

EDIT2

Even more simply, if you want to get the matching value from a Hashtable using a string key do this,

Hashtable distancesFromEdinburgh = GetEdinburgh();
string town = "Ayr";
int distanceFromEdinburghToAyr = distancesFromEdinburgh[town];

EDIT

The answer is the very simplest, and worst way is,

static HashTable GetTown(string town)
{
    Hashtable hashtable;

    switch(town)
    {
        case "Edinburgh":
            hashtable = new Hashtable();
            hashtable.Add("Aberdeen", 129);
            hashtable.Add("Ayr", 79);
            hashtable.Add("Fort_William", 131);
            hashtable.Add("Glasgow", 43);
            hashtable.Add("Inverness", 154);
            hashtable.Add("St_Andrews", 50);
            hashtable.Add("Stirling", 36);
            break;

            // Other cases here
    }

    return hashtable;
}

Ok, don't use HashTable for a start. The code below uses a number of up to date features and has equivalent functionality to your posted code.

What is the question again?

private enum Towns
{
    Aberdeen,
    Ayr,
    FortWilliam,
    Glasgow,
    ...
}

private static IDictionary<Towns, int> GetEdinburgh()
{
    return new Dictionary
    {
        { Key = Aberdeen, Value = 129 },
        { Key = Ayr, Value = 79 },
        { Key = FortWilliam, Value = 131 },
        { Key = Glasgow, Value = 43 },
        ...
    };
}

static void Main(string[] args)
{
    var closest = this.GetEdinburgh()
        .Where(p => p.Value < 1000)
        .Min(p => p.Value);

    Console.WriteLine("{0}, {1}km", closest.Key, closest.Value);
    Console.ReadKey();
}

If you want a common get town function you could declare it like this.

private static IDictionary<Towns, int> GetTown(Towns town)
{
    switch(town)
    {
        case Towns.Edinburgh:
            return new Dictionary
            {
                { Key = Aberdeen, Value = 129 },
                { Key = Ayr, Value = 79 },
                { Key = FortWilliam, Value = 131 },
                { Key = Glasgow, Value = 43 },
                ...
            }

        case Towns.Ayr
            ...

        ...

        default:
           throw new ArgumentException(
               string.Format("{0} not implemented", town),
               "town"); 
    };
}

This implementation of this function would differ depending on how you store or define the distances. The current implementation assumes they are hardcoded which would be fast but a little brittle to maintain.

If you have the coordinates of the towns locations you could calculate the distance between them, "as the crow flies" on each call.

If you want to generate a "real" best travel distance you need a map and a rather more complicated path finding algorithm.

What have you got, what do you want to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM