Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Dear mates,

 

How do I change the namespace of the .net classes?

 

For example:

"Oledb.OleDBConnection" I want to represent it as "ABC"

So, when I want to declare a new OleDBConnection, I do

 

Dim Conn as new ABC

George C.K. Low

  • Administrators
Posted

The namespace refers to the string prefixed to the class name. e.g.

OleDbConnection is really

System.Data.OleDb.OleDbConnection

with System.Data.OleDb being the namespace and OleDbConnection is the class name.

 

Both C# and Vb (and presumably other .Net languages also) have ways of shortening this syntax:

VB.Net has the Imports Keyword and C# has the using directive. Both allow you either just allow you to use the class name without prefixing the namespace or alias the namespace to another string.

 

e.g.

'full namesapce
Module Module1

   Sub Main()
       Dim o As System.Data.OleDb.OleDbConnection
   End Sub

End Module

'Import namespace, no aliasing
Imports System.Data.OleDb

Module Module1

   Sub Main()
       Dim o As OleDbConnection
   End Sub

End Module

'import and alias
Imports pegs = System.Data.OleDb

Module Module1

   Sub Main()
       Dim o As pegs.OleDbConnection
   End Sub

End Module

'aliasing classname 
Imports pegs = System.Data.OleDb.OleDbConnection

Module Module1

   Sub Main()
       Dim o As pegs
   End Sub

End Module

 

note these methods will still require the references to be set up correctly.

I personally would really avoid the last method as it will make it very difficult to identify what a variable's real data type is as this will not be obvious at the point of declaration.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

PlausiblyDamp,

 

Thanks for ur help, that's exactly what i want.

 

I will choose fot the last method because it will be much more easier for me to swap databases from one to another..

For example,

 

When I am using MS access, I will do

Imports pegs = System.Data.OleDb.OleDbConnection

Module Module1

   Sub Main()
       Dim o As pegs
   End Sub

End Module

 

And when I want to swap to MSSQL, I just need to change a line of code.

 

Imports pegs = System.Data.sqlclient.sqlclientconnection

Module Module1

   Sub Main()
       Dim o As pegs
   End Sub

End Module

 

How do you think about this?

George C.K. Low

  • Administrators
Posted

In all honesty if you require a provider neutral method of accessing data bases then this method will lead to problems later - what if OleDBconnection exposes methods that SQlconnection doesn't (and vice versa)?

 

You will be better of declaring your variables through the interfaces provided under system.data

 

e.g.

Dim conn as IDbConnection

conn = New OleDB.OleDBConnection()
'etc.

 

to be truely flexible you could also create the correct type of connection etc. at runtime based on an external config file - you may find it worth reading up on the concept of a 'Factory Pattern' as a way to acheive this.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...