mkhimself mkhimself - 1 year ago 54 Question

Benefit to full reference paths for object declarations?

Is this probably a stupid question? Sure, but i'm more or less just curious to hear other peoples opinions. So Is there a benefit to using the full reference path for an object declaration? MSDN only seemed to mention that it might help when overloading controls or declaring your own class that is already included in the visual basic language.

What i'm referring to is a declaration like so

Dim MyFile As New System.IO.File

In visual studio if you
Import System.IO
then it will tell you that the name can be simplified to just

Dim MyFile As New File

But is there any other benefit or does it really matter whether you just use
or the full reference to
. Is this simply so that if you did not want to
Import System.IO
you could still use the
Class? I'm sure that it's the latter of the two, and its simply just if you do not want to import, but is it better practice to use
instead of just
? And honestly I'm more or less just curious, because i know either way it'll compile and run.

Answer Source

One of the reasons this exists is to solve name conflicts.

For example: Lets say you have a class:


and another class:


If you import both namespaces (CompanyOne.ProductX, and CompanyTwo.ProductY), and then declare a Customer object, you'll get a compile-time error. It won't know which Customer object you were trying to instantiate.

So to resolve this you would use the fully-qualified name of the class to declare your variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download