2 Replies - 1616 Views - Last Post: 04 November 2012 - 07:25 AM Rate Topic: -----

#1 karthikin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 12

Get value from the lookup table class

Posted 04 November 2012 - 06:41 AM

This is my lookup table code:
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);
    }
}


This is my function:
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;

          

        }


If the `value3`is equal to the `distance`value from the table then get the value of `distanceAngle `. How can i do this??

Is This A Good Question/Topic? 0
  • +

Replies To: Get value from the lookup table class

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Get value from the lookup table class

Posted 04 November 2012 - 07:20 AM

Let me tell you where I'm confused...

You're able to write all this other code including LINQ statements, create and populate List<T> - but you can't figure out how to get the value of a variable from an instance of a class that you built? That's just really, really odd. It really makes me think you didn't write the code you're showing us.

But... moving on.

You have a class of 'DistanceAngle'. From your program you need to make an instance of it such as DistanceAngle myDistanceAngle = new DistanceAngle();

Then you can reference the .Distance variable as myDistanceAngle.Distance
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Get value from the lookup table class

Posted 04 November 2012 - 07:25 AM

If what you really need is an explaination of the concepts of classes as the title suggests, maybe this will help:

Think of objects in coding just as you would objects in the real world.

A Dodge Ram is an object.
It is made up of smaller objects: Engine, doors, tires
Each of those is made up of smaller objects: Bolts, pistons, etc.

Objects in coding can inherit from each other, usually from the general to the specific.

  • Class vehicle
    • Class truck : vehicle
      • Class Ram : Truck
        • class 2500FWD : Ram


A class is the blueprint for instanciating (making an instance of) the object.

DodgeRam is a class describing how to make an instance, but itself is not an actual thing you can interact with.

myDodgeRam is an instance of the class DodgeRam

I can do things with the object instance myDodgeRam

myDodgeRam.SerialNumber = 123456789;
myDodgeRam.FillUpTank();
float fuelLevel = myDodgeRam.FuelTankPercentageFull;
if (myDodgeRam.IsReady) myDodgeRam.StartEngine();


Anything defined in the base class is available to a child.

class truck : vehicle
{

    public float FuelTankPercentageFull
    {
       get; set;
    }
}

class DodgeRam : truck
{
    // I don't have to define a FuelTankPercentageFull here because I inherit it from my parent
}


Methods defined as virtual in the base class can be overridden by the child class (at least in .NET languages like C#). This is often to account for more specific needs.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Do something to start the engine
       return true; // No checks or requirements
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return true;
        }
        return false;
    }
}


You can even have a child class call the base classes methods which is often the smart way to go.

class truck : vehicle
{

    public virtual bool StartEngine()
    {
       // Notice there are no safety checks before trying to start up.
       try
       {
          // Do something to start the engine
          return true; // because we succeeded
       }
       catch(exception error)
       {
           return false; // because there was an error
       }
    }
}

class DodgeRam : truck
{
    public override bool StartEngine()
    {
        // Do a safety check first
        if (IsSeatBeltsEngaged && IsFootOnBrake)
        { 
            return base.StartEngine();
            // Now all the electrical work is in the base class
            // and not repeated in every child.
        }
    }
}

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1