1

I am unsure whether this is possible with an ArrayList or a Dictionary or whether it would be something else, if so I wonder where you could point me in the right direction...

Can you have an ArrayList with Multiple Values i.e.

ArrayList weather = new ArrayList();
weather.Add("Sunny", "img/sunny.jpg");
weather.Add("Rain", "img/Rain.jpg);

To then assign to controls like below.

if (WeatherValue = 0)
{
   Label1.Text = weather[0].ToString;
   Image1.ImageUrl = weather[0].ToString;
}

Or can I do this with a Dictionary

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("Cloudy", "../img/icons/w0.png");  //[0]
dict.Add("Rain", "../img/icons/w1.png");    //[1]  

Label1.Text = dict[0].VALUE1;    //So this would get Cloudy
Image.ImageUrl = dict[0].VALUE2; //This would get ../img/w0.png

How do you call the values of a dictionary separately using [0], and [1] ? etc

1
  • 1
    Theres no reason to still use ArrayList, use the System.Collections.Generic.List<T>-class Commented Mar 17, 2016 at 13:51

2 Answers 2

7

There's no reason to still use ArrayList, use the System.Collections.Generic.List<T>-class. Then you keep compile time safety and you don't need to cast everything.

In this case you should create a custom class:

public class Weather
{
    public double Degree { get; set; }
    public string Name { get; set; }
    public string IconPath { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

Then you can use this readable and maintainable code:

List<Weather> weatherList = new List<Weather>();
weatherList.Add(new Weather { Name = "Sunny", IconPath = "img/sunny.jpg" });
weatherList.Add(new Weather { Name = "Rain", IconPath = "img/Rain.jpg" });

if (WeatherValue == 0) // whatever that is
{
    Label1.Text = weatherList[0].Name;
    Image1.ImageUrl = weatherList[0].IconPath;
}

Update: according to your edited question. A dictionary doesn't make much sense because you can't access it via index(it has no order) but only via key. Since that would be the weather-name you have to know it beforehand. But it seems that you don't have it.

So either loop all key-value pairs in the dictionary and use the key for the name and the value for the path or simply use a real class which would be much better.

If you don't want to create a class there's only one thing that comes to my mind, the Tuple:

List<Tuple<string, string>> weatherList = new List<string, string>();
weatherList.Add(Tuple.Create("Sunny", "img/sunny.jpg"));
weatherList.Add(Tuple.Create("Rain", "img/Rain.jpg"));

if (WeatherValue == 0) // whatever that is
{
    Label1.Text = weatherList[0].Item1;
    Image1.ImageUrl = weatherList[0].Item2;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Perhaps turn 'Name' into an enum called weatherType?
@sr28: sure, there are many ways to improve or extend this class. It should just give OP an idea.
I would also create my own class. But then there's also the Tuple class which can be used
@FredrikRedin: added the tuple approach
Thanks I have got this working using a Class and a List like you said.
0

You can use a Dictionary

Dictionary<string, string> weather =   new Dictionary<string, string>();

values.Add("Sunny", "img/sunny.jpg");
values.Add("Rain", "img/Rain.jpg");

The simplest way to call element in a dictionnary is using foreach loop

foreach (var pair in weather )
    {
        Console.WriteLine("{0}, {1}",pair.Key,pair.Value);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.