Enumeration

S

shapper

Hello,

I have a enumeration named Levels and the function Levels2String:

Public Function Levels2String(ByVal myLevel As Level, ByVal culture
As String) As String

...

Basically the function returns a string for a given culture and level.

Now I created a list of type Level: Generic.List(Of Level)

What is the best way to convert the list of type level to a list of
type string using function Levels2String?

I believe I should make a loop and access each item in list of type
level and apply it the function but I am getting problems with it.

Thanks,

Miguel
 
G

Guest

Howdy,

You could use ConvertAll if you didn't put culture parameter to the convert
function. I modified the concept:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load


Dim levels As New System.Collections.Generic.List(Of Level)

levels.Add(New Level(1))
levels.Add(New Level(2))
levels.Add(New Level(3))
levels.Add(New Level(4))


Dim converter As New LevelConverter("en-GB")
Dim strings As System.Collections.Generic.List(Of String) = _
levels.ConvertAll(Of String)(New System.Converter(Of Level,
String)(AddressOf converter.Level2String))

End Sub

End Class

Public Class Level

Public Sub New(ByVal n As Integer)
Number = n
End Sub

Public Shared Function Levels2String(ByVal myLevel As Level) As String

Return myLevel.Number.ToString()

End Function

Public Number As Integer

End Class

Public Class LevelConverter

Private defaultCulture As String = "en-GB"

Public Sub New()
End Sub

Public Sub New(ByVal defaultCulture As String)
Me.defaultCulture = defaultCulture
End Sub

Public Function Level2String(ByVal myLevel As Level) As String
Return myLevel.ToString() + defaultCulture
End Function

Public Function Level2StringWithCulture(ByVal myLevel As Level, ByVal
culture As String) As String
Return myLevel.ToString() + culture
End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top