Need help with C# Method Overloads

Started by
4 comments, last by JimDaniel 16 years, 9 months ago
I'm learning about AI programming and decided to create some helper methods to go along with the examples. I wanted to create an overloaded method to use when calculating the trigonometric formulas "SohCahToa" using the two variables you already have as parameters. The reason I wanted to create one method for this is that the formulas and variables are all closely related. But the way I have it now, the methods' signatures are the same, so of course I'm not allowed to create an overload. Here is the rough template I made:

        public float SohCahToa(float radians, float opposite)
        {
            //logic
        }
        public float SohCahToa(float radians, float adjacent)
        {
            //logic
        }
        public float SohCahToa(float radians, float hypotenuse)
        {
            //logic
        }
        public float SohCahToa(float radians, float opposite)
        {
            //logic
        }
        public float SohCahToa(float adjacent, float opposite)
        {
            //logic
        }
        public float SohCahToa(float hypotenuse, float opposite)
        {
            //logic
        }
        public float SohCahToa(float hypotenuse, float adjacent)
        {
            //logic
        }
Is there anyway around this? Or is there a better way to do it? Thanks for any advice you can give...
Advertisement
If you want multiple functions, and they have to have those parameters, you'll have no choice but to give them different names.

Alternatively, you could use just one function, and make one of the parameters an enumeration that specifies what the two values are.

enum SohCahToaParameters
{
OppositeHypotenuse,
AdjacentHypotenuse,
OppositeAdjacent,
etc
}

float opposite = blah.SohCahToa(SohCahToaParameters.AdjacentHypotenuse, adjacent, hypotenuse);
Also you could take the nullable approach:

public float SohCahToa(float? radians, float? opposite, float? adjacent, float? hypotnuse){}Then you could call it like:SohCahToa(1.3f, null, 2.4, null, null);


I personally would have seperate names, but that is just my preference.
Couldn't you use just one, and put a switch in that will decide which logic to use? Or maybe just if statements that would call a seperately named function. You could name them all differently, but have one function that would be called, sort out which logic to use, call the appropriate function, and return that.
Quote:Original post by JimDaniel
I'm learning about AI programming and decided to create some helper methods to go along with the examples. I wanted to create an overloaded method to use when calculating the trigonometric formulas "SohCahToa" using the two variables you already have as parameters. The reason I wanted to create one method for this is that the formulas and variables are all closely related.

But the way I have it now, the methods' signatures are the same, so of course I'm not allowed to create an overload.

Here is the rough template I made:

        public float SohCahToa(float radians, float opposite)        {            //logic        }        public float SohCahToa(float radians, float adjacent)        {            //logic        }        public float SohCahToa(float radians, float hypotenuse)        {            //logic        }        public float SohCahToa(float radians, float opposite)        {            //logic        }        public float SohCahToa(float adjacent, float opposite)        {            //logic        }        public float SohCahToa(float hypotenuse, float opposite)        {            //logic        }        public float SohCahToa(float hypotenuse, float adjacent)        {            //logic        }

Is there anyway around this? Or is there a better way to do it? Thanks for any advice you can give...


Actually it is easier then you think it might be.

The problem you have is that you have different "types" of floats, so just make struct to hold your floats.

struct Hypotenus, struct Opposite, struct Adjacent, struct radians. Then you can overload to your hearts content.

theTroll
Your suggestions made me rethink my strategy and I ended up creating a class called SohCahToa with 9 different methods to account for every possible combination. Works fine.

Thanks for the help.

This topic is closed to new replies.

Advertisement