Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top