Enum and string ??

kGenius
06-20-2004, 12:00 AM
Hi,

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
Get
Return _provider
End Get
Set(ByVal Value As String)
_provider = Value
End Set
End Property
End Class


Now I want to display some fixed values when calling the property.
As in:

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
kGenius

PWNettle
06-20-2004, 02: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
MicrosoftJetOLEDB40
SQLOLEDB
MSDAORA
End Enum


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()

MessageBox.Show(sTest)

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 ProviderTypes.MicrosoftJetOLEDB40
Return "Microsoft.Jet.OLEDB.4.0"
Case Else ' Return ToString version of provider types that don't need converting.
Return eProviderType.ToString()
End Select

End Function

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"
MessageBox.Show(GetProviderTypeAsString(ProviderTypes.MicrosoftJetOLED B40))
' This displays "SQLOLEDB"
MessageBox.Show(GetProviderTypeAsString(ProviderTypes.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.

Paul

PWNettle
06-20-2004, 02: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
Get
Return "Microsoft.Jet.OLEDB.4.0"
End Get
End Property

Public Shared ReadOnly Property SQLOLEDB() As String
Get
Return "SQLOLEDB"
End Get
End Property

Public Shared ReadOnly Property MSDAORA() As String
Get
Return "MSDAORA"
End Get
End Property

End Class

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).

Sample Usage:

' Displays "Microsoft.Jet.OLEDB.4.0"
MessageBox.Show(ProviderNames.MicrosoftJetOLEDB40)

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()
MessageBox.Show("Hello World!")
End Sub

..then you could use it from elsewhere in your project, like some other class, by directly invoking it as:

ProviderNames.HelloWorld()

...but I ramble...

Paul

kGenius
06-20-2004, 06: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)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum