Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific value from look-up table?

This is the look-up class:

public class DistanceAngle
{
    public int distance;
    public int distanceAngle;
    public int valueOfBoth;

    public static void getDisatnceAngleLookup()
    {
        List<DistanceAngle> distanceAngles = new List<DistanceAngle>
                {
                   new DistanceAngle{distance = -5400, distanceAngle = -220, valueOfSideAngle = 320},
                   new DistanceAngle{distance = -5200, distanceAngle = -210, valueOfSideAngle =  290},
                   new DistanceAngle{distance = -5000, distanceAngle = -200, valueOfSideAngle = 200},                                      
                };

        //Distance as Key Value and look for distanceAngle
        Lookup<int, int> lookup = (Lookup<int, int>)distanceAngles.ToLookup((p => p.distance),
                                                                    p => p.distanceAngle);

        int count = lookup.Count;

        // Select a collection of distanceAngles by indexing directly into the Lookup.
        IEnumerable<int> cgroup = lookup[-5400];

        // Output the results.
        Console.WriteLine("\nPackages that have a key of 'C':");
        foreach (int str in cgroup)
            Console.WriteLine(str);
    }
}

From the interface, if the user enters A = 6000 B = 400 C = -5400 by B-A. If C value is equal to the distance = -5400 from the LookUp calss then get the value distanceAngle = -220 and fill to the datagrid specific column from 1 to 5 cells.

If C value is eqaual to the distance = -5400 and get the value valueOfSideAngle = 320 and fill to the datagrid specific column from 6 to 10 cells.

This way I can get the column index : datagridview1.Columns["columnName"].Index;

This is the function, where I'm performing the calculation:

private void b_calculate_Click(object sender, EventArgs e)
    {

        int value1;
        int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                         CultureInfo.InvariantCulture.NumberFormat, out value1);

        int value2;
        int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                         CultureInfo.InvariantCulture.NumberFormat, out value2);

        var value3 = value2 - value1;


    }

How can I get the value from the lookup table which is equal to value3 and fill to the datagridview. Any help...!

like image 919
linguini Avatar asked Feb 20 '26 14:02

linguini


1 Answers

Firstly you need two values(side angle and distance angle) from a single key that is distance which requires some multidimensional collection. Anything with more than one value for a key gets complex and you should use a complex data structure only if it warrants. Lets look at the options:

1) Your method: Lookup<int, int>

Lookup in this fashion is applicable only when you are having more entries with same key but different values. In other words, if you have:

List<DistanceAngle> distanceAngles = new List<DistanceAngle>
{
   new DistanceAngle{distance = -5400, distanceAngle = -220},
   new DistanceAngle{distance = -5400, distanceAngle = -210},
   new DistanceAngle{distance = -5000, distanceAngle = -200}
};

And if you do:

var lookup = (Lookup<int, int>)distanceAngles.ToLookup(p => p.distance, 
                                                       p => p.distanceAngle);

you will get a lookup of Count 2 which means the keys are 5400 and 5000 but you will have two values for the key 5400. And the values will be in an IEnumberable<int>. Hence you have two values in value collection for key 5400, but one value in the IEnumerable<int> value collection for key 5000. This is not what you are essentially looking for.

What you need is something like Collection<TKey, TValue1, TValue2>.

2) List<DistanceAngle>:

I would say the best option is to go ahead with the list you have and create a function to get the distance angle and side angle from distance. Since you said in your comments that the list wont be too large, you can create extension method or so, something like this:

public static int GetDistanceAngle(this List<DistanceAngle> distanceAngles, 
                                   int distance)
{
    var d = distanceAngles.FirstOrDefault(d => d.distance == distance);
    if (d == null)
        return 0; //your value
    return d.distanceAngle;
}

public static int GetSideAngle(this List<DistanceAngle> distanceAngles, 
                               int distance)
{
    var d = distanceAngles.FirstOrDefault(d => d.distance == distance);
    if (d == null)
        return 0; //your preferred value
    return d.sideAngle;
}

Now you can call:

private void b_calculate_Click(object sender, EventArgs e)
{
    int value1;
    int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value1);

    int value2;
    int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value2);

    var value3 = value2 - value1;

    var sideAngle = distanceAngles.GetSideAngle(value3);
    var distanceAngle = distanceAngles.GetDistanceAngle(value3);
    //etc
}

3) If you need fast lookups you can rely on Dictionary (if there wont be duplicates) or Lookup (if there will be) but using something like Dictionary<int, Tuple<int, int>> or Lookup<int, Tuple<int, int>> will be cumbersome.

4) You could do: Dictionary<int, DistanceAngle> or Lookup<int, DistanceAngle>

(I'll show Dictionary one, both of which are the same).

public static Dictionary<int, DistanceAngle> getDisatnceAngleLookup()
{
    List<DistanceAngle> distanceAngles = new List<DistanceAngle>
    {
       new DistanceAngle{distance = -5400, distanceAngle = -220, valueOfSideAngle = 320},
       new DistanceAngle{distance = -5200, distanceAngle = -210, valueOfSideAngle =  290},
       new DistanceAngle{distance = -5000, distanceAngle = -200, valueOfSideAngle = 200},                                      
    };

    //Distance as Key Value and look for distanceAngle
    return distanceAngles.ToDictionary(p => p.distance, p => p);
}

}

Now accessing by key is easy:

private void b_calculate_Click(object sender, EventArgs e)
{
    int value1;
    int.TryParse(t_LongitudeRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value1);

    int value2;
    int.TryParse(t_LongSecondaryRadTextBox.Text, NumberStyles.Any,
                     CultureInfo.InvariantCulture.NumberFormat, out value2);

    var value3 = value2 - value1;

    var sideAngle = distanceAngles[value3].sideAngle;
    var distanceAngle = distanceAngles[value3].distanceAngle;
    //etc
}
like image 152
nawfal Avatar answered Feb 22 '26 02:02

nawfal