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

M

mesut

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
 
T

Tom P.

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.
 
B

Ben Voigt [C++ MVP]

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
 
T

Tom P.

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.
 
G

Göran Andersson

mesut said:
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.
 
B

Ben Voigt [C++ MVP]

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.
 
G

Göran Andersson

Ben said:
???

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.techtarget.com/sDefinition/0,,sid183_gci212803,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/Polymorphism_(computer_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/Polymorphism_in_object-oriented_programming
 
A

Alun Harford

mesut said:
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
 
A

Arne Vajhøj

Göran Andersson said:
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/Polymorphism_(computer_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/Polymorphism_in_object-oriented_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
 
M

mesut

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??????????????
 
G

Göran Andersson

mesut said:
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) -
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.
 
G

Göran Andersson

Arne said:
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/Polymorphism_(computer_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.
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.
 
B

Ben Voigt [C++ MVP]

Göran Andersson said:
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.
 
G

Göran Andersson

Ben said:
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.
 
M

mesut

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
 

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

Top