473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2001
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
Jan 23 '06 #2

"Jason Huang" <Ja************ @hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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
2921
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 me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
1
1589
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 composition how is that done. Give some easy example if you have some.
7
2060
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 always following message from the compiler, although COUNT of the DataView (according to MSDN) is virtual. the element '...
2
4170
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 void OnKeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e) { /* Do Nothing */ } } public class Class1 : Main
11
1787
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 from the class view and changed it fro public int Add(object value t public int Add(MyItemClass value This seems to work, but it generates a...
15
3770
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
5475
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 all applications will use sessions and to insure that all application use this method, the session properties cannot be overriden. Within the...
4
2351
by: Kevin Frey | last post by:
I have the following situation: Assembly #1: class NativeSearchCriteria // NOTE: a native class { }; public ref class TheBaseClass {
2
5711
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 inherits from bindinglist<mycustomobjectbut I cannot override Add method. This because this bindinglist is a datasource for a datagrid and need to avoid...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.