Namespace / Class Question Newbie

batutabunsing

Member
Joined
Mar 3, 2008
Messages
19
Programming Experience
Beginner
Hello everyone. Newbie here in VB.Net VS2005, Winforms Development

Just some clarification with regards to classes in VB.Net.

I have a winform, named frmCarInvoice, and the VB codes are contained in frmCarInvoice.vb.

I have created a new class named MyClass.vb, and the structure of the class is as follows

Namespace Car
Public Class CarFx
Public Shared Function DoSomething() as String
'Do something here
End Function

Public Shared Function DriveSomething() as Integer
'Do some driving.
End Function
End Class
End Namespace

Now, how do I utilize the functions contained in the class MyClass.vb inside my frmCarInvoice.vb? Like for example, I would only invoke the function contained in the class without the full qualifying namespace.

Dim strReturnValue as String = DoSomething()

Instead of:
Dim strReturnValue as String = Car.CarFx.DoSomething

Do I need to declare sort of an imports statement? The intellisense does not show the namespace or the class name.

Advice would be highly appreciated. Thanks.
 
If you want to invoke the method without any qualification then you must import the class name at the top of the code file:
VB.NET:
Imports RootNamespace.Car.CarFx
RootNamespace is the root namespace for your project, which will be the same as the project name by default.
 
Dim strReturnValue as String = DoSomething()

Instead of:
Dim strReturnValue as String = Car.CarFx.DoSomething

Do I need to declare sort of an imports statement?

If both the forma nd the class are in the same namespace, then ClassName.MethodName() will suffice. I recommend you do not attempt to completely strip out the classname too, for the same reason that I would not recommend removing all the folders from your C drive and just dumping every file in the root of the drive. Hierarchies exist for a reason, removing them causes more confusion during reading (repeated operation) than it saves time writing (one-time operation)
 
Back
Top