Overload Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to Inherit from a Method? I don't want to have to repeat
the code in my method over and over for the overloaded versions. If I need
to make changes I don't want to have to reapeat them in each overloaded
version of my method.

Better way to do this?

Thank you,
Paul
 
Soli3d said:
Is there a way to Inherit from a Method? I don't want to have to repeat
the code in my method over and over for the overloaded versions. If I need
to make changes I don't want to have to reapeat them in each overloaded
version of my method.

Better way to do this?

The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:

public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}

// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
 
Jon,

Thank you much. How would this work if I wanted
to pass different types to the main method? Int16 vs
Double? Do I have to make the main method an Object
and cast as needed?

Cheers,
Paul
 
Thanks, That I got..:) What I need is to keep
all the logic for the overloaded methods in
the main method. ie..

foo (val, val, val, val) //main
{
....
}

foo (double a, double b, double c)
{
}

foo (double a, double b)
{
}

foo (int a, int b, int c)
{
}

ect...

Thanks Guys... You both probably already answered
this, I just numb...:) Working on it though.

Paul
 
Another possibility, if the methods are working with different types
and therefore can't share all of their code, is to do something like
this:

public void DoSomeStuff(int something)
{
...
DoSomeCommonStuff();
...
}

public void DoSomeStuff(double somethingElse)
{
...
DoSomeCommonStuff();
...
}

private void DoSomeCommonStuff()
{
...
}

In other words, split out what code you can into a common, private
method or methods, and call that / those method(s) from each of the
overloads.
 
Thanks for the push. I like this way.
Code:
class Tumble
{
static void Main(string[] args)
{
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
}

static public void foo(int a, int b, int c)
{
int result = (a + b + c);
Console.WriteLine(result.ToString());
}

static void foo(int a, int b)
{
int c = 3;
foo(a, b, c);
}

static void foo(int a)
{
int b = 2;
foo(a, b);
}

static void foo()
{
int a = 1;
foo(a);
}
}

Cheers,
Paul
 
Soli3d said:
Thank you much. How would this work if I wanted
to pass different types to the main method? Int16 vs
Double? Do I have to make the main method an Object
and cast as needed?

It depends on what your methods need to do. You may be able to extract
just the common parts of the code, then make each of the overloaded
methods brief.
 
If it is with a repetition of the same type, that you work with, and those
are last parameters, your can also use the variable argument list way as in:

void foo (params int [] all)
{
int sum = 0 ;
foreach (int i in all) sum += i ;
Console.WriteLine ("{0}", sum) ;
}

called as : foo (); foo (1); foo (1,2); foo (1,2,3); etc...

/LM

Soli3d said:
Thanks for the push. I like this way.
Code:
class Tumble
{
static void Main(string[] args)
{
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
}

static public void foo(int a, int b, int c)
{
int result = (a + b + c);
Console.WriteLine(result.ToString());
}

static void foo(int a, int b)
{
int c = 3;
foo(a, b, c);
}

static void foo(int a)
{
int b = 2;
foo(a, b);
}

static void foo()
{
int a = 1;
foo(a);
}
}

Cheers,
Paul

Jon Skeet said:
The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:

public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}

// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
 
Luc, Nice! Thanks muck.

Paul

Luc E. Mistiaen said:
If it is with a repetition of the same type, that you work with, and those
are last parameters, your can also use the variable argument list way as in:

void foo (params int [] all)
{
int sum = 0 ;
foreach (int i in all) sum += i ;
Console.WriteLine ("{0}", sum) ;
}

called as : foo (); foo (1); foo (1,2); foo (1,2,3); etc...

/LM

Soli3d said:
Thanks for the push. I like this way.
Code:
class Tumble
{
static void Main(string[] args)
{
foo();
foo(1);
foo(1, 2);
foo(1, 2, 3);
}

static public void foo(int a, int b, int c)
{
int result = (a + b + c);
Console.WriteLine(result.ToString());
}

static void foo(int a, int b)
{
int c = 3;
foo(a, b, c);
}

static void foo(int a)
{
int b = 2;
foo(a, b);
}

static void foo()
{
int a = 1;
foo(a);
}
}

Cheers,
Paul

Jon Skeet said:
Is there a way to Inherit from a Method? I don't want to have to repeat
the code in my method over and over for the overloaded versions. If I
need
to make changes I don't want to have to reapeat them in each overloaded
version of my method.

Better way to do this?

The normal way is to make one overload which has all the information
you could possibly need, and call it from other methods, passing in
parameters. For instance:

public object LookupItem (object key, object defaultValue)
{
... do the lookup ...
}

// Same as LookupItem (key, defaultValue) but with a default value of
// null
public object LookupItem (object key)
{
return LookupItem (key, null);
}
 
Back
Top