Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Enum and string ??


Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2004, 12:00 AM
kGenius kGenius is offline
Freshman
 
Join Date: Jun 2004
Location: Belgium
Posts: 25
Default Enum and string ??


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".
Code:
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:
Code:
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
Reply With Quote
  #2  
Old 06-20-2004, 02:26 AM
PWNettle PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

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:

Code:
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:

Code:
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:

Code:
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):

Code:
' This displays "Microsoft.Jet.OLEDB.4.0" MessageBox.Show(GetProviderTypeAsString(ProviderTypes.MicrosoftJetOLEDB40)) ' 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
Reply With Quote
  #3  
Old 06-20-2004, 02:47 AM
PWNettle PWNettle is offline
Verbose Coder

Retired Moderator
* Guru *
 
Join Date: Dec 1999
Location: Phoenix, Arizona
Posts: 3,011
Default

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

Code:
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:

Code:
' 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:

Code:
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:

Code:
ProviderNames.HelloWorld()

...but I ramble...

Paul

Last edited by PWNettle; 06-20-2004 at 02:52 AM.
Reply With Quote
  #4  
Old 06-20-2004, 06:32 AM
kGenius kGenius is offline
Freshman
 
Join Date: Jun 2004
Location: Belgium
Posts: 25
Default

You should not be dissappointed:
Quote:
Genius is 1% of inspiration and 99% of perspiration
- quote by Edison

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

kGenius (in a newbie-way)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->