By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

making this C# procedural function an OOP function - get rid of theparameters

P: n/a
hi colleagues,

I don't know if this is the right group for but it's in C# so I try.
I have a #3 procedural function called GetInfo.. and those are 3
overloaded methods. I would like to use the OOP approach to refactor
this.

The functions are working fine but I would like to have a OOP approach
can someone help me refactoring this??????

GeTInfo(System) -returns mutiple records - Dataset
GeTInfo(System,Key) -returns multiple records - Dataset
GeTInfo(System,Key,Value) -returns 1 record - Returns String
new approach:
- I created 3 properties
- I deleted the parms in the Functions it looks like GetInfo()

Function:
GetInfo()

3 Properties:
public String System{ get; set; }
public String Key{ get; set; }
public String Value{ get; set; }

Approach:
my aspx page should set the property, it can just the System or it can
be System, Key or it can be System, Key , Value. and then call the
GetInfo() function. The problem I have is: how can I decide how my
SQL should look like in WHERE clause. Because the previous procedural
I passed them as parameter and then used them in the WHERE. But I had
3 functions then (overloaded Methods)... Now I have 1 method... but
how can I decided how my SQL the Where clause should look like?

SELECT Name, Address, Zipcode FROM AddressBOOK where XXXXXXXXXXXXXX
is it with if (propertySystem!= null) { //concat } If {propertyKey!=
null) {//concat}
etc etc.

can someone show the OOP approach?????? or the best practice...
thanks in advance,

mesut
Jul 23 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
My initial question is why not have the three overloaded methods?
GetInfo(System)
{
GetInfo(System, null, null);
}

GetInfo(System, Key)
{
GetInfo(System, Key, null);
}

GetInfo(System, Key, Info)
{
... do code things...
}
But, if you must continue on your current course then I'd build the
WHERE clause in the property accessor.

public string System
{
get{ }
set
{
_WhereClause += " and System = '" + value +"'";
}
}

or some such thing. Or build member strings that can be concatenated
into a WHERE clause when the GetInfo method is called.

Hope that helps,
Tom P.
On Jul 23, 12:05*pm, mesut <mesut.de...@noveonbe.comwrote:
hi colleagues,

I don't know if this is the right group for but it's in C# so I try.
I have a #3 procedural function called GetInfo.. and those are 3
overloaded methods. I would like to use the OOP approach to refactor
this.

The functions are working fine but I would like to have a OOP approach
can someone help me refactoring this??????

GeTInfo(System) -returns mutiple records *- Dataset
GeTInfo(System,Key) *-returns multiple records *- Dataset
GeTInfo(System,Key,Value) *-returns 1 record - Returns String

new approach:
- I created 3 properties
- I deleted the parms in the Functions it looks like GetInfo()

Function:
GetInfo()

3 Properties:
public String System{ get; set; }
public String Key{ get; set; }
public String Value{ get; set; }

Approach:
my aspx page should set the property, it can just the System or it can
be System, Key *or it can be System, Key , Value. *and then call the
GetInfo() function. The problem I have is: *how can I decide how my
SQL should look like in WHERE clause. Because the previous procedural
I passed them as parameter and then used them in the WHERE. But I had
3 functions then (overloaded Methods)... Now I have 1 method... but
how can I decided how my SQL the Where clause should look like?

SELECT Name, Address, Zipcode FROM AddressBOOK where XXXXXXXXXXXXXX

is it with if (propertySystem!= null) { //concat *} *If {propertyKey!=
null) {//concat}
etc etc.

can someone show the OOP approach?????? or the best practice...

thanks in advance,

mesut
Jul 23 '08 #2

P: n/a
public string System
{
get{ }
set
{
_WhereClause += " and System = '" + value +"'";
}
}

or some such thing. Or build member strings that can be concatenated
into a WHERE clause when the GetInfo method is called.

Thinking your SQL belongs in the application (-10 points)
Building SQL by concatenation (-10 points)
Quoting input values instead of using parameterized queries (-100 points)
Exposing your users to SQL injection attack -- priceless
Jul 23 '08 #3

P: n/a
On Jul 23, 12:42*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
public string System
{
* * get{ }
* * set
* * {
* * * * _WhereClause += " and System = '" + value +"'";
* * }
}
or some such thing. Or build member strings that can be concatenated
into a WHERE clause when the GetInfo method is called.

Thinking your SQL belongs in the application (-10 points)
Building SQL by concatenation (-10 points)
Quoting input values instead of using parameterized queries (-100 points)
Exposing your users to SQL injection attack -- priceless
Fair enough, but he asked. I'm still trying to find out why function
overloading is not OOP.

Tom P.
Jul 23 '08 #4

P: n/a
mesut wrote:
hi colleagues,

I don't know if this is the right group for but it's in C# so I try.
I have a #3 procedural function called GetInfo.. and those are 3
overloaded methods. I would like to use the OOP approach to refactor
this.

The functions are working fine but I would like to have a OOP approach
can someone help me refactoring this??????

GeTInfo(System) -returns mutiple records - Dataset
GeTInfo(System,Key) -returns multiple records - Dataset
GeTInfo(System,Key,Value) -returns 1 record - Returns String
That is an OOP approach. Overloading is a part of polymorphism, which is
one of the main principles for OOP.

However, if what the methods do differ too much, you should give them
separate names instead. Also, it's somewhat impractical to have
overloads return different data types.
>
new approach:
- I created 3 properties
- I deleted the parms in the Functions it looks like GetInfo()

Function:
GetInfo()

3 Properties:
public String System{ get; set; }
public String Key{ get; set; }
public String Value{ get; set; }

Approach:
my aspx page should set the property, it can just the System or it can
be System, Key or it can be System, Key , Value. and then call the
GetInfo() function.
That's not a good approach at all. You have gone from an approach where
the method call is self describing, to an approach where it's not.
The problem I have is: how can I decide how my
SQL should look like in WHERE clause. Because the previous procedural
I passed them as parameter and then used them in the WHERE. But I had
3 functions then (overloaded Methods)... Now I have 1 method... but
how can I decided how my SQL the Where clause should look like?

SELECT Name, Address, Zipcode FROM AddressBOOK where XXXXXXXXXXXXXX
is it with if (propertySystem!= null) { //concat } If {propertyKey!=
null) {//concat}
etc etc.

can someone show the OOP approach?????? or the best practice...
thanks in advance,

mesut

--
Göran Andersson
_____
http://www.guffa.com
Jul 23 '08 #5

P: n/a
That is an OOP approach. Overloading is a part of polymorphism, which
is one of the main principles for OOP.
???

Overridding is related to polymorphism, overloading is not.
Jul 23 '08 #6

P: n/a
Ben Voigt [C++ MVP] wrote:
>That is an OOP approach. Overloading is a part of polymorphism, which
is one of the main principles for OOP.

???

Overridding is related to polymorphism, overloading is not.
Everything that I read says that it is... For example:
"This type of polymorphism is sometimes known as overloading."

http://searchcio-midmarket.techtarge...212803,00.html
"Overloading is one type of polymorphism."

http://www.webopedia.com/TERM/O/overloading.html
"This type of polymorphism is common in object-oriented programming
languages, many of which allow operators to be overloaded in a manner
similar to functions (see operator overloading)."

http://en.wikipedia.org/wiki/Polymor...mputer_science)
"Overloading Polymorphism is the use of one method signature, or one
operator such as "+", to perform several different functions depending
on the implementation."

http://en.wikipedia.org/wiki/Polymor...ed_programming
--
Göran Andersson
_____
http://www.guffa.com
Jul 23 '08 #7

P: n/a
mesut wrote:
hi colleagues,

I don't know if this is the right group for but it's in C# so I try.
I have a #3 procedural function called GetInfo.. and those are 3
overloaded methods. I would like to use the OOP approach to refactor
this.

The functions are working fine but I would like to have a OOP approach
can someone help me refactoring this??????

GeTInfo(System) -returns mutiple records - Dataset
GeTInfo(System,Key) -returns multiple records - Dataset
GeTInfo(System,Key,Value) -returns 1 record - Returns String
new approach:
- I created 3 properties
- I deleted the parms in the Functions it looks like GetInfo()

Function:
GetInfo()

3 Properties:
public String System{ get; set; }
public String Key{ get; set; }
public String Value{ get; set; }

Approach:
my aspx page should set the property, it can just the System or it can
be System, Key or it can be System, Key , Value. and then call the
GetInfo() function. The problem I have is: how can I decide how my
SQL should look like in WHERE clause. Because the previous procedural
I passed them as parameter and then used them in the WHERE. But I had
3 functions then (overloaded Methods)... Now I have 1 method... but
how can I decided how my SQL the Where clause should look like?

SELECT Name, Address, Zipcode FROM AddressBOOK where XXXXXXXXXXXXXX
Oh dear. You've hit "the vietnam of computer science".

Good options are to either drop the idea of using OO here, to use an
automated ORM tool, or use an OODBMS.

Alun Harford
Jul 23 '08 #8

P: n/a
Göran Andersson wrote:
Ben Voigt [C++ MVP] wrote:
>>That is an OOP approach. Overloading is a part of polymorphism, which
is one of the main principles for OOP.

???

Overridding is related to polymorphism, overloading is not.

Everything that I read says that it is... For example:
I think you read a bit selective then.

It should be obvious that it is possible to create
a procedural programming language that has function/subroutine
overloading.
"This type of polymorphism is common in object-oriented programming
languages, many of which allow operators to be overloaded in a manner
similar to functions (see operator overloading)."

http://en.wikipedia.org/wiki/Polymor...mputer_science)
This is under something it calls "Ad-hoc polymorphism". And
it says "Strachey [3] chose the term ad-hoc polymorphism to refer to..."
and the reference 3 is from 1967.
"Overloading Polymorphism is the use of one method signature, or one
operator such as "+", to perform several different functions depending
on the implementation."

http://en.wikipedia.org/wiki/Polymor...ed_programming
I don't think this is talking about what you think it is.

I read it as if a sub class must have the methods and the
operators of the sub class. Which is polymorphism.

Arne
Jul 24 '08 #9

P: n/a
Hi colleagues,

thanks for sharing your thoughts with me.

First of all I think I should explain why I want to move to the
property way iso overloading methods.

I'm not strong in OOP but as far as I did my research (maybe not good
enough)... In OOP principles you work with objects.
and an objects has methods and properties as type.

the situation I have is :
my aspx page in presentation layer is calling the class object in the
Business logic layer. So those are separated.

My idea was if we work with objects then I should set the values in
propertie and then call the methods. Therefore I wished to delete the
parameters and use properties. the situation I have then is setting
the parameter then call the object. Since I've 3 methods doing
similair thing just the WHERE clause should be changed.

This is my statement, correct me if I'm wrong:
USE properties iso parameters in functions as much as I can, because
objects works with properties. A property has a get and set accessor
to check the value you pass. So I think using property is much more
OOP then passing parameters in functions... I guess.

let me give another example.

if you have a person class and method that returns address, city,
country of the that person.
The method is called GetPerson in thet Person class.

Which approach would you use....
1) call with parameter passing the peson ID: GetPerson(personID) -
>returning a dataset or a list ---- OR -----
2a) create a property in Person class and name it : PropertyPersonID()
etc.
2b) set the property value and call this method: GetPerson()

ok.... which is more OOP... I think approach 2 is OOP. I guess...

Ok let's say I only know the person's first name and last name and I
would like to get all persons starting with these info. I would also
like name the method GetPerson. Because it's related to the person.

What I do is create 2 more properties in Person class name the
properties like 'propertyFirstName' and 'propertyLastName' then call
the GetPerson() method....

ok.... how would you handle this ? property approach??????

thanks
-mesut
ps: or would you just create 2 methods and overload them??????????????
Jul 24 '08 #10

P: n/a
mesut wrote:
Hi colleagues,

thanks for sharing your thoughts with me.

First of all I think I should explain why I want to move to the
property way iso overloading methods.

I'm not strong in OOP but as far as I did my research (maybe not good
enough)... In OOP principles you work with objects.
and an objects has methods and properties as type.

the situation I have is :
my aspx page in presentation layer is calling the class object in the
Business logic layer. So those are separated.

My idea was if we work with objects then I should set the values in
propertie and then call the methods. Therefore I wished to delete the
parameters and use properties. the situation I have then is setting
the parameter then call the object. Since I've 3 methods doing
similair thing just the WHERE clause should be changed.

This is my statement, correct me if I'm wrong:
USE properties iso parameters in functions as much as I can, because
objects works with properties. A property has a get and set accessor
to check the value you pass. So I think using property is much more
OOP then passing parameters in functions... I guess.
Not at all. Neither method is more object oriented than the other.

In your attempt to make the code more object oriented, you have actually
managed to make it less object oriented, or at least less managable. If
you have methods with specific parameters, it's obvious by just looking
at the method signatures what you can do with them, and it's not even
possible to call the methods with any other set of data than the
intended. This is robust, managable and thus good. By using properties
and a parameterless method, the code has lost the control over it's
usage, and you can easily call the method with a completely wrong set of
data. The code is not self-documenting any more, and you have to read
the documentation to understand which properties to set to get a
specific result.

If you want it more object oriented, you should have methods that
returns an object or a list of objects, not a DataSet. The object that
everything revolves around should be the data that you fetch, not
something unidentifiable that you use to fetch the data with.

If you don't want to create objects for the data, but want to keep the
data in a DataSet (which is a reasonable compromise), then your code is
pretty much as object oriented as it can get.
let me give another example.

if you have a person class and method that returns address, city,
country of the that person.
The method is called GetPerson in thet Person class.

Which approach would you use....
1) call with parameter passing the peson ID: GetPerson(personID) -
>returning a dataset or a list ---- OR -----
2a) create a property in Person class and name it : PropertyPersonID()
etc.
2b) set the property value and call this method: GetPerson()

ok.... which is more OOP... I think approach 2 is OOP. I guess...
3) A call to GetPerson(personId) returning a Person object.
Ok let's say I only know the person's first name and last name and I
would like to get all persons starting with these info. I would also
like name the method GetPerson. Because it's related to the person.

What I do is create 2 more properties in Person class name the
properties like 'propertyFirstName' and 'propertyLastName' then call
the GetPerson() method....

ok.... how would you handle this ? property approach??????

thanks
-mesut
ps: or would you just create 2 methods and overload them??????????????
--
Göran Andersson
_____
http://www.guffa.com
Jul 24 '08 #11

P: n/a
Arne Vajhřj wrote:
Göran Andersson wrote:
>Ben Voigt [C++ MVP] wrote:
>>>That is an OOP approach. Overloading is a part of polymorphism, which
is one of the main principles for OOP.

???

Overridding is related to polymorphism, overloading is not.

Everything that I read says that it is... For example:

I think you read a bit selective then.
I don't understand how a clear statement like "Overloading is one type
of polymorphism." could possibly be read selectively?

Do you have anything else that I could read?
It should be obvious that it is possible to create
a procedural programming language that has function/subroutine
overloading.
The same is true for any of the OO principles. Just because it could be
implemented in a procedural language, doesn't make it non-OO.
>"This type of polymorphism is common in object-oriented programming
languages, many of which allow operators to be overloaded in a manner
similar to functions (see operator overloading)."

http://en.wikipedia.org/wiki/Polymor...mputer_science)

This is under something it calls "Ad-hoc polymorphism". And
it says "Strachey [3] chose the term ad-hoc polymorphism to refer to..."
and the reference 3 is from 1967.
Yes, OO has been around for a while. Just because something was said a
few years ago, doesn't automatically make it wrong.
>"Overloading Polymorphism is the use of one method signature, or one
operator such as "+", to perform several different functions depending
on the implementation."

http://en.wikipedia.org/wiki/Polymor...ed_programming

I don't think this is talking about what you think it is.

I read it as if a sub class must have the methods and the
operators of the sub class. Which is polymorphism.

Arne
What do you think that I think it is? ;)

I don't see anything in that paragraph that even suggests that is has
anything to do with subclassing.

--
Göran Andersson
_____
http://www.guffa.com
Jul 24 '08 #12

P: n/a
Göran Andersson wrote:
Ben Voigt [C++ MVP] wrote:
>>That is an OOP approach. Overloading is a part of polymorphism,
which is one of the main principles for OOP.

???

Overridding is related to polymorphism, overloading is not.

Everything that I read says that it is... For example:
C'mon, don't you mean, the first batch of search engine hits for "overload
AND polymorphism".

But not all information on the web is accurate.

Polymorphism is all about the LSP and *requires* dynamic dispatch. There
are languages which implement overloading with dynamic dispatch but C# (nor
VB.NET, VB6, Java, JavaScript, non-template C++) are not among them.
Overload resolution in C# is done statically and is therefore not
polymorphic.
Jul 24 '08 #13

P: n/a
Ben Voigt [C++ MVP] wrote:
Göran Andersson wrote:
>Ben Voigt [C++ MVP] wrote:
>>>That is an OOP approach. Overloading is a part of polymorphism,
which is one of the main principles for OOP.
???

Overridding is related to polymorphism, overloading is not.
Everything that I read says that it is... For example:

C'mon, don't you mean, the first batch of search engine hits for "overload
AND polymorphism".
Not at all.
But not all information on the web is accurate.

Polymorphism is all about the LSP and *requires* dynamic dispatch. There
are languages which implement overloading with dynamic dispatch but C# (nor
VB.NET, VB6, Java, JavaScript, non-template C++) are not among them.
Overload resolution in C# is done statically and is therefore not
polymorphic.
You are talking about subtype polymorphism. Just because it's true for
that specific concept, doesn't invalidate all other forms of polymorphism.

Parametric polymorphism, for example, is done by the compiler, so by
your definition it would not be polymorphism at all.

--
Göran Andersson
_____
http://www.guffa.com
Jul 25 '08 #14

P: n/a
thanks everybody. It was a nice discussion I mean I learnt a lot of
jargons. I shall keep my overloaded methods, because it seems to be
that it's right. I shall return List i to use more OOP iso Datasets

thanks, mesut
Jul 25 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.