I agree in principle with what you are saying. I was really commenting on
what appeared to be pretty broad-brush.
In my opinion it really comes down to a matter of trade-offs, which, of
course, is another general life principle.
If one has a single encapsulated function a'la:
Public MyFunc(oCaller As Object) As Object
Select Case oCaller.GetType.Name
Case "abc"
Return whatever
Case else
Return whateverelse
End Select
End Function
as opposed to a number of overloaded functions to handle all known object
types and one comes up with a new object type then I know which approach I
would rather maintain.
I believe it is more a case of 'what works for you' rather than aiming for
'acedemic correctness'.
Fergus Cooney said:
Hi Stephany,
Fergus wrote:
|| > Public Function MyFunc (..., oCaller As Object) As Object
|| >
|| > But in VB.NET that is very bad programming!!
|| > There are much better ways to achieve the same effect.
General life principle:
When there are better ways to achieve a given effect, choosing a
non-better way is bad, maybe even 'very bad'.
In this case, the purpose of oCaller is solely to say what type of
variable is on the receiving end of an assignment using
MyFunc(). If you read Genival's question and the answers again, and decide
what solution said:
you end up with a MyFunc like the one above.
Stephany wrote:
|| Why is passing a 'caller' object as a parameter to a procedure, and
|| testing it's type within the procedure, 'very bad programming' in VB.Net.?
My statement was referring to the code above it and was not a comment
about passing Objects in general. However...
I don't think passing in Objects is good practice if it means that
there must be code to work out whether it's a valid object
and what to do in each case, etc. It's fine if your method works
polymorphically, although I think the object type should be as
restrictive as possible, ie using the highest possible base class or an interface.
|| It is exactly how event handlers are constructed with the
|| sender As System.Object parameter
I'd love to have event handlers that had specific types. [Not to
mention a decent name. "sender" has never grabbed me as
particularly useful.] An event handler, eg KeyPress, must cater for any
object that could possibly expect a key press. Plenty of
these objects have yet to be invented. Therefore the KeyPress Event
Handler has to have an Object as its parameter.
If you know in advance what types a method of yours is going to have
to cater for, you are in the position of using VB.NET to
its fullest and choosing one of the 'better ways'.
If overloads, virtual methods or specialised methods can be used
instead of passing Objects around, then more of the the