Not quite an enum, not quite a dictionary

  • Thread starter Thread starter psychopif
  • Start date Start date
P

psychopif

I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces. I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

What would be the best solution?
 
I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces. I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

What would be the best solution?

Hmmm... Maybe a custom EnumConverter?

http://weblogs.asp.net/mnolton/pages/RepurposingEnumerations.aspx
 
Interesting to say the least. I think it is a litle too complicated
for my project though. It seems to adds a lot of complexity to the
code.
 
Interesting to say the least. I think it is a litle too complicated
for my project though. It seems to adds a lot of complexity to the
code.

There are other ways of course... You could create a readonly
collection class. You could implement a type conversion operator
(CType). You could create a helper class that formats the enum name...

etc, etc, etc :)
 
I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces. I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

What would be the best solution?

Are the strings absolutely constant, or might they vary will UI locale ? If
the later, the use resources, as in My.Resources.String1. If they are
constant then declaring them as constants in a class will do the trick, eg:

Class MyStrings
Public Const String1 As String = "abc"
Public Const String2 As String = "def"
End Class
 
I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces. I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

You could make an enum using underscore instead of space to gain all the
benefits of an enum. As needed, you could do such things as overriding
ToString to replace underscore with space, etc.
 
I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces. I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

Creating enumerations of items with a certain arbitrary data type
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=anytypeenums&lang=en>

You could use constants or 'ReadOnly' string fields instead of the
properties.
 
You got in my idea all answers on the first part of your question, however
your question conflicts

The first part
I have a list of strings each represented by a number. I want those
string to be constant and put as litle hit as I can on the performance
of my application. The strings are mutualy exclusive, juste like an
Enum. I would use the Enum, but my string contains special caracter
and spaces.

The second part
I would use a dictionary, but then the string could be
changed at runtime and it would add unnecesary overhead to my
application.

For the latter would generics do a fine job.

What is it?

Cor
 
You got in my idea all answers on the first part of your question, however
your question conflicts

The first part


The second part


For the latter would generics do a fine job.

What is it?

Cor

The string are constant. It's the type of supplier in a financial
application. The problem is, in some flat files, the type are written
in string, but in the database, they have been mapped to an integer
field to save space.

ex:

1 Intermédiaire
2 Personne morale
3 Personne physique
...
etc.

As you can see, the type have space in them and some spécial caracter
i'd rather not have or can't in a enum name. What i would realy liked
would have been an enum where I could have used the space and special
caracter
 
Does an approach like this suite your needs?

Public Class DaysOfWeek
Public Enum DoW
Sun
Mon
Tue
Wed
Thu
Fri
Sat
End Enum

Private Shared Days As Type = GetType(DoW)
Public Shared ReadOnly Names() As String = [Enum].GetNames(Days)

Shared Sub New()
'change any of the names you want here
Names(DoW.Sun) = "Sunday"
Names(DoW.Sat) = "Saturday"
End Sub

Shared Property Item(ByVal Index As DoW) As String
Get
Return Names(Index)
End Get
Set(ByVal value As String)
Names(Index) = value
End Set
End Property

End Class
 
Back
Top