Sign in to follow this  

Need help with C# Method Overloads

This topic is 3817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this