views:

95

answers:

3

I may have this wrong, but I've seen the way of creating an overloaded method that calls itself in the definition. It's something like:

public void myFunction(int a, int b)
{
    //Some code here
}

public void myFunction(int a) : this (a, 10)
{ }

This is not the correct syntax, I know, but I can't find the correct syntax anywhere for some reason. What is the correct syntax for this?

+6  A: 

Just do this:

public void myFunction(int a, int b)
{
    //Some code here
}

public void myFunction(int a) 
{
    myFunction(a, 10)
}

You're confusing overloading with overriding. You can call a base constructor from an derived class like this:

public MyConstructor(int foo) 
   :base (10)
{}
Dave Swersky
+1, didn't know you could do that.
Mike Webb
+5  A: 

You are confusing constructor syntax for method syntax. The only way to do that for a method is the obvious:

public void myFunction(int a, int b) 
{ 
    //Some code here 
} 

public void myFunction(int a) 
{ 
     myFunction(a, 10) ;
} 

although as of C#4, you can use a optional parameters:

public void myFunction(int a, int b = 10) 
{ 
    //Some code here 
} 

What you wrote is close to right for constructors:

public class myClass
{

    public myClass(int a, int b)
    {
        //Some code here
    }

    public myClass(int a) : this (a, 10)
    { }

}
James Curran
Exactly what I was looking for. Thanks.
Mike Webb
A: 
public class BaseClass {
     public virtual void SomeFunction(int a, int b) {}
}

public class Derived : BaseClass {
     public override SomeFunction(int a, int b) {
          base.SomeFunction(a, b * 10);
     }
}
Pete
The question is about overloading, not overriding.
Lee