georgepatotk Posted October 17, 2004 Posted October 17, 2004 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 Quote George C.K. Low
Administrators PlausiblyDamp Posted October 17, 2004 Administrators Posted October 17, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Diesel Posted October 17, 2004 Posted October 17, 2004 Aliasing namespaces will make the code less readable to other programmers. Quote
georgepatotk Posted October 18, 2004 Author Posted October 18, 2004 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? Quote George C.K. Low
Administrators PlausiblyDamp Posted October 18, 2004 Administrators Posted October 18, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.