2

Is it possible to have a string array as the value in a dictionary? I need to save the description (Hour2) as the key and as value being able to access both the price (elements_PR(4)) and the time offset (1). Is there a good way to do that?

Dim pr_val(2) As String
Dim PR As New Dictionary(Of String, pr_val)

PR.Add("Hour2", {elements_PR(4), "1"})
3
  • 1
    If you fix the declaration i don't see any problem Dim PR As New Dictionary(Of String, string()). Well, of course elements_PR(4) is a string right? Commented Oct 9, 2013 at 12:52
  • Dim PR As New Dictionary(Of String, String()) and PR.Add("Hour1", {"",""}) doesn't work either. The error: Comma, ')' or valid expression continuation expected. Commented Oct 9, 2013 at 13:11
  • It works for me. It sounds like you are missing a parenthesis. Commented Oct 9, 2013 at 14:20

4 Answers 4

1

There is no reason why you can't do it - you could also use values of type Tuple(Of String, String), if the number of elements is fixed in advance - in this case, 2. It'd be easier to perform comparisons on, and would also be immutable, which is often a good idea.

Sign up to request clarification or add additional context in comments.

Comments

1

You sure can. Try something like this:

Dim PR As New Dictionary(Of String, IEnumerable(Of String))
PR.Add("Hour2", {elements_PR(4), "1"})

2 Comments

I can't get it to work either. I tried changing the declaration and add the following: PR.Add("Hour1", {"",""}). But it gives the error: Comma, ')' or valid expression continuation expected.
Are you sure you aren't missing a parenthesis?
1

It seems to me that you could create a class representing the price and the time offset. Therefore, you could do something like PR.Add("Hour2", instanceOfClass).

Depending on the meaning of the description in your situation, you could even include it in your class. It would allow you to use another approach with a List(Of YourClass) containing a list of items with the description, the price and the time offset. To retrieve an item by "key", you could then use some Linq.

Comments

0

Short answer - yes. Assuming the following was declared somewhere:

Dim elements_PR(4) As String : elements_PR(4) = "Hello"
Dim PR As New Dictionary(Of String, String())

You can either do:

PR.Add("Hour2", {elements_PR(4), "1"})

or

Dim pr_val() As String = {elements_PR(4), "1"}
PR.Add("Hour1", pr_val)

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.