C# override problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Huang

    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


  • Marc Gravell

    #2
    Re: C# override problem

    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(stri ng 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(stri ng someParameter) {
    SomeCollection col = repareInitialVa luesFromDatabas e(someParameter );
    CheckForDuplica teCustomers(col );
    AddEmployeesToU I(col);
    AddCustomersToU I(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


    Comment

    • Nick Hounsome

      #3
      Re: C# override problem


      "Jason Huang" <JasonHuang8888 @hotmail.com> wrote in message
      news:%23bDvMFAI GHA.3904@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > 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.[/color]

      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


      Comment

      Working...