C# override problem

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

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
 
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
 
Jason Huang said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top