471,605 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

C# override problem

Hi,

In my C# Windows form MyForm, it has a function MyFunction which is a big
function and has lots of codes.
I am thinking the override for MyFunction, one MyFunction has a parameter
which will pass a value into the function, while the origin MyFunction
doesn't pass that value.
Now comes my question, since the MyFunction is a long function, is the
"Override" for that function a good idea, or there're other better
solutions?
Any help will be appreciated.
Jason
Jan 23 '06 #1
2 1936
I don't see any problem at all with an override, but don't duplicate the
code-base; something like:

public SomeReturnType MyFunction() {
return MyFunction(null);
}

public SomeReturnType MyFunction(string someParameter) {
// lots of code
}

i.e. have the more trivial forms call the more parameterised version, but
simply providing the defaults. The fuller version must accept that some of
the parameters may be in their default state, and act accordingly.

If your code as-written doesn't lend itself to this, consider writing a
single private version, which *all* of the public version call, specifying
everything they need. You could even use a private enum to help clarify
minor differences between the calls (although personally I would prefer to
do without).

As another point - unless your MyFunction is irreducibly complex, I would
strongly consider refactoring it into several small, clearly defined private
functions. This will help maintenance, as well as ensuring each block knows
exactly what it should be doing. Since these blocks are private, you can use
whatever naming namkes it really, really obvious what is going on.

e.g.

public SomeReturnType MyFunction(string someParameter) {
SomeCollection col = repareInitialValuesFromDatabase(someParameter);
CheckForDuplicateCustomers(col);
AddEmployeesToUI(col);
AddCustomersToUI(col);
// etc
}

The human brain can only remember so many variables etc before going "pop";
even with expandable #regions, personally I still prefer to refactor
logically separate code into separate methods. This also allows you to
re-use discreet sections from other methods.

Marc
Jan 23 '06 #2

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

In my C# Windows form MyForm, it has a function MyFunction which is a big
function and has lots of codes.
I am thinking the override for MyFunction, one MyFunction has a parameter
which will pass a value into the function, while the origin MyFunction
doesn't pass that value.
Now comes my question, since the MyFunction is a long function, is the
"Override" for that function a good idea, or there're other better
solutions?
Any help will be appreciated.


1) This isn't override it is overload. Override is when you override a base
class method.

2) It is common practice to simulate default arguments by creating one
method with all the parameters and a overloads with less that just call the
"all parameter" method with the "defaults". I assume that this is what you
intend. On no account use copy and paste.

P.S. I have no idea why C#doesn't support default arguments since it would
be trivial to just implement them in this way
Jan 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Ruben Van Havermaet | last post: by
1 post views Thread by Tony Johansson | last post: by
7 posts views Thread by Thomas Kehl | last post: by
2 posts views Thread by ~toki | last post: by
11 posts views Thread by songie D | last post: by
15 posts views Thread by Cliff_Harker | last post: by
8 posts views Thread by bdeviled | last post: by
2 posts views Thread by Ste | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.