06-20-2004, 01:00 AM
I'm building a class ("Genius.DataAccess") to get quickly a data-connection and communication with several databases.
I want to set and return a property "ProviderName".
Public Class DataCommunication
Public Property ProviderName() As String
Set(ByVal Value As String)
_provider = Value
Now I want to display some fixed values when calling the property.
Dim gen as Genius.DataAccess.DataCommunication
gen.ProviderName = ...
Behind the equal-sign there should a dropdownlist with the possible values. (ie "Microsoft.Jet.OLEDB.4.0", "SQLOLEDB", "MSDAORA")
I've read some things about enums but it seems to be hard to convert these to strings.
Thanks in advance
06-20-2004, 03:26 AM
I'm disapppointed. I would've thought that a self-appointed genius would be able to figure this out in his sleep!
I can think of two possible solutions here. One is to create your enum:
Private Enum ProviderTypes
And when you want to use it as a string value all you have to do is invoke ToString on it:
Dim sTest As String
sTest = ProviderTypes.MicrosoftJetOLEDB40.ToString()
The problem with this approach is that if you actually wanted to use the Enum values as part of a connection string or something they wouldn't be entirely correct, since you have to drop the punction out of ones like "Microsoft.Jet.OLEDB.4.0" because you simply can't have periods in variable/object names.
If you wanted to have the nice intellisense popup provided by using an enum as a property - and you wanted the true values of the enum constants, you could add in a little function that did some converting for you, for ex:
Private Function GetProviderTypeAsString(eProviderType As ProviderTypes) As String
Select Case eProviderType
Case Else ' Return ToString version of provider types that don't need converting.
The function above would return a more useful string for use with connection strings or whatever.
Simple usage (and a great way to verify/test that it works):
' This displays "Microsoft.Jet.OLEDB.4.0"
' This displays "SQLOLEDB"
You'd show the property to the consumer of your class as the enum - for easy setting and displaying with intellisense. When it came time to actually do something with that property, like use it in a connection string, you'd use the conversion function to get the proper value for it.
06-20-2004, 03:47 AM
Here's yet another solution - I think it's more elegant than the others.
Instead of using an enum you simulate one by creating a seperate class that contains shared properties - each property represents one of your provider types. The shared keyword lets the property be accessed without instantiating an instance of the class - so it ends up looking and working just like an enum - except you can return any data type you want instead of an integer.
The class might look like this (ProviderNames.vb) :
Public Class ProviderNames
Public Shared ReadOnly Property MicrosoftJetOLEDB40() As String
Public Shared ReadOnly Property SQLOLEDB() As String
Public Shared ReadOnly Property MSDAORA() As String
And you use it just like you would an enum. You don't have to declare an instance of 'ProviderNames' to use it because of the Shared properties (in C# we can also use the 'sealed' keyword with the class declaration that prevents classes of this type from being instantiated at all - I couldn't find the VB.Net equivalent).
' Displays "Microsoft.Jet.OLEDB.4.0"
You'd just need to create a property of type 'ProviderNames' for your class.
The Shared keyword is pretty cool - it's somewhat like the old 'Global' of VB6. With it you can do some nice things that are almost like code modules - like create subs and functions that don't require class instantiation to use. For example, if you added this to the 'ProviderNames.vb' class:
Public Shared Sub HelloWorld()
..then you could use it from elsewhere in your project, like some other class, by directly invoking it as:
...but I ramble...
06-20-2004, 07:32 AM
You should not be dissappointed:
Genius is 1% of inspiration and 99% of perspiration - quote by Edison :D
I've got the function with the select case's already. But I needed the second neater solution. More elagant, but very obvious. I just woke up and thought I dreamed about the solution. It was the forum though !
Thanks for that :D
kGenius (in a newbie-way)