views:

213

answers:

3

I am new to developing with Visual Basic, but have lots of experience in C#/C/C++/JAVA/Haskell and a few others.

Is there some reason I am maintaining code and finding code examples where the type declaration just seems lazy? I have seen countless Strings, that are only ever used as the String type, declared as an object. Before the object oriented features of VB.NET came in, was this necessary to assure that methods could take in multiple types? Why would anyone do this?

+5  A: 

At a guess...laziness and old VB6 habits being carried forward.

Scott Dorman
So this was so methods could take in multiple members, correct? Instead of overloading functions you would create one method that takes in an as any and then do something with that?
Close, but most likely so they didn't have to specify the actual data type. Having a method take a variable list of parameters is different than having one that accepts one parameter of any type. It sounds like what you have is the "one parameter of any type" scenario, which would be equivalent to using a Variant in VB6.
Scott Dorman
+3  A: 

In VB6 there was the variant type that could take in various types and it was meant to be used with caution and care. When .Net came out the only conversion for that (besides changing your code a lot) would to have used object as the type.

I would definitely call it a hangover effect or code conversion effect from VB6 if the code appears to need that flexibility as you described for a method to take in multiple types.

However, if you are seeing code for string types that were declared as objects this is far worse than just laziness and a hangover from VB6! It sounds like horribly poor design and a lack of care.

klabranche
+1  A: 

Some VB6 experts, for instance "the Mandelbrot Set", advised using Variants for all variables. You could call it early duck typing?! This was rather controversial - many of us thought it was a recipe for disaster.

But there must be a lot of code like this out there. Before modern conversion tools appeared, upgrading it to VB.NET would be very hard. The Visual Studio upgrade tool simply panics every time it sees a Variant. If you really, really needed to upgrade with limited resources, I suppose changing all variants to Object might be the least-bad option. Obviously the resulting code would be horrible.

You could gradually refactor. When you work on an area of code, you could change the declarations to a more specific type.

MarkJ
So how were you going to debug this sea of variants: wait for something to go wrong at runtime and hope for a helpful error? Did this way of coding actually work?Great answer though.
Well, I always avoided this method like the plague, so I suppose I don't know whether it really worked or not! But I'd be quite surprised if it did work.
MarkJ