473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1991
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
1
by: Tony Johansson | last post by:
Hello experts! I know it's easy to override when you have inheritance. But if you instead use object composition having a pointer to a class X. If you want to override when having this object...
7
by: Thomas Kehl | last post by:
Hello. I provided a class, which derives from the class DataView. The only thing, which I would like to do in this class am to overwrite the Property COUNT. I wars it however simply not. I get...
2
by: ~toki | last post by:
How can i take the control of the key events in Class2 ? This is the code snipped that i'd tried (after try some others): public class Main : System.Windows.Forms.Form { protected virtual...
11
by: songie D | last post by:
Does c# support overriding by name and not signature For instance I am building a custom collection class, that ca only take a certain type of object For this, I have added an override for add...
15
by: Cliff_Harker | last post by:
Why can't I do this in C# public class A { public A virtual whatever( A a ) { } } public class B : A
8
by: bdeviled | last post by:
I am deploying to a web environment that uses load balancing and to insure that sessions persist across servers, the environment uses SQL to manage sessions. The machine.config file determines how...
4
by: Kevin Frey | last post by:
I have the following situation: Assembly #1: class NativeSearchCriteria // NOTE: a native class { }; public ref class TheBaseClass {
2
by: Ste | last post by:
Good morning, I have a bindinglist of my custom objects and need to check, before adding items, if an item exists in list and then cancel inserting. I have tried to create an object that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.