mkhimself mkhimself - 3 months ago 7
Vb.net 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
File
or the full reference to
System.IO.File
. Is this simply so that if you did not want to
Import System.IO
you could still use the
File
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
System.IO.File
instead of just
File
? And honestly I'm more or less just curious, because i know either way it'll compile and run.

Answer

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

For example: Lets say you have a class:

CompanyOne.ProductX.Customer

and another class:

CompanyTwo.ProductY.Customer

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.

Comments