1

I need to read values for chart from KeyValuePair<long,long> but i want show only one axes values (y) for some points, so i tried add null at first parameter but chart can not be showed .

What i can do? Thanks.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Sejn
  • 63
  • 10

2 Answers2

1

I'm guessing you are using a Collection of KeyValuePair<long, long> and that these two values are the x and y coordinates. For settings only value, I would recommend setting the other value to -1. This would be fine as you are not using ulong.

Say these assumptions are correct and your collection is called Points.

I would recommend using linq where y is your desired value:

 Points.Select(p => p.Value).ToArray();

or

 Points.Select(p => p.Value).ToList();

This will return a new array or list, depending, containing all of the y values. If you needed the x values you would use Key instead of Value. As you are only grabbing the value, it doesn't matter if the Key (x) is null, -1 or whatever.

Taking this a step further, you could select all of the y values, where x meets a condition. I'm guessing this would be useful to you based on your saying

only one axes values (y) for some points

Again, where Points is the collection of KeyValuePair<long, long>

int example = 5;
Points.Where(p => p.Key == example).Select(p => p.Value).ToArray();

or

Points.Where(p => p.Key != -1).Select(p => p.Value).ToArray();

Continuation(See first comment):

Queue<KeyValuePair<long, long>> queue = new Queue<KeyValuePair<long, long>>();
KeyValuePair<string, Queue<KeyValuePair<long, long>>> pair = new KeyValuePair<string, Queue<KeyValuePair<long, long>>>("test", queue);

pair.Value.Enqueue(new KeyValuePair<long, long>(-1, 5));

I get no problem with this code, works fine.

Possible other solution:

Instead of having a KeyValuePair<long,long>. You could have KeyValuePair<long,long[]>. The long[] would store one or two values based on your need. The only limitation would be that you would have to assume that if the array is of length one, then it is only storing y and not x, otherwise if the array is of size 2, then assume it is in an {x, y} format.

tjheslin1
  • 1,378
  • 6
  • 19
  • 36
  • I don't have collection, this is my structure---KeyValuePair>>--- and at this queue are values of x and y axes. How can i add to this queue which has KeyValuePair just value of y? I tried queue.Enqueue(new KeyValuePair(-1, elem.Value)) but it doesn't work. – Sejn Dec 16 '13 at 17:21
  • _I tried queue.Enqueue(new KeyValuePair(-1, elem.Value)) but it doesn't work_. I followed this and have posted code in my answer(see Continuation). I did not get any errors with that code, runs fine. Maybe it's a syntax problem you had? Are you happy with entering -1 for `y` in the case that `x` is not needed? – tjheslin1 Dec 16 '13 at 18:22
  • Another option is store use `KeyValuePair`. I go into more detail in my answer edit where the code is easier to read. – tjheslin1 Dec 16 '13 at 18:26
  • Glad I cold help. Did you go for the `long[]` as the value? – tjheslin1 Dec 18 '13 at 11:39
0

If I am not wrong, you are using a dictionary and if is the case, the first value of the dictionary is for the X value and the second value is for the Y value.

In a dictionary, the first value is the key and must be unique. i know that when you try to add two keys with the same value, the dicitionary throw an exception, but really I don't know if you can set null values in the key.

If the dictionary let set null value to the key, then the problem perhaps is that it can find the value because there are many null keys.

You could try to use another way to store the values, perhaps in a structure, altught if you have many points, the search of the values it will be slower.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193