Tag Archives: Readability

Enumerable of String version 3 – Generics to simplify

I continue exploring readability and the framework by investigating the possibility of something similar to an enumerable but with strings. It really is quite different than an enumerable in several ways. I had two previous posts V1 and V2 . I’m going to repeat some information in this post. You can see I’m beginning to call this a Completion List Pattern.

Story

I was on a project where I am reading information from a mainframe travel agency system. I communicate back and forth using strings that represent screens that the travel agent used to see before the internet. These are similar to the 3270 displays but most are smaller. An example would be the inventory screen. The travel agent would type in a hotel and some dates and they would receive a screen indicating what days were available, closed or on request. On a month availability screen there are a lot of letters. under each day for a hotel there is one of three letters. If you have to call the hotel to request a date the letter R was there to indicate that it was on request. If the hotel was closed to arrivals there there was a letter C on that day. If the hotels availability was open and you could come and say then an O was under that day. These letters are called inventory codes.

For working with this data I wanted to put it in proper objects. Link makes it trivial to write queries to find what you want once you have it is objects. I really like enums (enums) because you get the intellisense but the only integer types were allowed for the values. I then had to have a separate object that mapped the integers to the strings found on the screen. That visually separated the string from the enums which complicated upkeep. I’ve been experimenting and here is what I have come up with. It’s not ideal but I learned a lot putting it together.

Goals:

  • Easily map string values to types for parsing
  • Provide intellisense with a statement completion list of values for easy coding
  • Minimize the amount of code needed to implement the pattern

Let’s begin by creating a type to store our inventory codes.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InventoryCode
    Inherits completionListItem
    Public Sub New(ByVal SetText As String)
        MyBase.New(SetText)
    End Sub
    Public Sub New()
        MyBase.New()
    End Sub
End Class

This is actually far more complex than I wanted but apparently you can’t inherit constructors (See my previous post about my question on stackoverflow.com). Any ideas on how to shrink this would be appreciated.

Next we need to list out all the codes in a completion list.

Public NotInheritable Class InventoryCodes
    Inherits completionList
    Public Shared ReadOnly Open As New InventoryCode("O")
    Public Shared ReadOnly Closed As New InventoryCode("C")
    Public Shared ReadOnly OnRequest As New InventoryCode("R")
End Class

That has a lot of words in it but it’s easy enough to follow. Let’s see how the intellisense works

image

Great. Now we can get to business. Let’s assume that I have a variable with data from the screen called ScreenText. I can check to see if it’s available by the following:

If ScreenText = InventoryCodes.Open.Text Then
    Console.WriteLine("Yay! We can stay at the hotel!")
End If

That’s Easy. Now something more difficult. Let’s say I want to convert a screen text directly into the proper Inventorycode type. You could use reflection to iterate through the items to find the right one. The problem with reflection is the speed loss. To mitigate that I created a generic class that created a dictionary once then you can use it several times. I also included a command to find the right item sing that is code you will need to do a lot. First you create a new completionListDictionairy object like below to generate the dictionary and give you the tools. Below I put the proper InventoryCode object in the variable “I”.

Dim InventoryDictionairy As New completionListDictionairy(Of InventoryCode, InventoryCodes)
Dim ScreenText As String = "O"
Dim I As InventoryCode
I = InventoryDictionairy.GetValueFromString(ScreenText)

You can now even loop through all the codes and print them out if you like:

Console.WriteLine(InventoryDictionairy.items.Count)
For Each item In InventoryDictionairy.items
    Console.WriteLine(item.Key.ToString & " : " & item.Value.Text)
Next

Let’s see how this meets our goals

  • Easily map string values to types for parsing : Seems pretty easy after some setup
  • Provide intellisense with a statement completion list of values for easy coding: Intellisense is there
  • Minimize the amount of code needed to implement the pattern: Umm… lots more code than I like so I didn’t really succeed here.

Let me know what you think in the comments but please be kind. I know that this is not production code and probably does not really justify it’s work. It’s an experiment.

Namespace CompletionListHelperNamespace
    Public Class completionListDictionairy(Of t As {completionListItem, New}, ts As {completionList, New})
        Public items As New Dictionary(Of String, t)

        'Get the code from the list created in the new
        Public Function GetValueFromString(ByVal Text As String) As t
            For Each code In items
                If code.Value.Text = Text Then Return code.Value
            Next
            Return Nothing
        End Function

        'Use reflection to put the codes in a list for later fast retrieval
        Sub New()
            Dim TempeStrClass As New ts
            Dim TempeStr As New t
            Dim TypeStrClass As Type = TempeStrClass.GetType
            Dim TypeStr As Type = TempeStr.GetType
            Dim f() As FieldInfo = TypeStrClass.GetFields
            For Each Field In f
                If Field.GetValue(Field).GetType Is TempeStr.GetType Then
                    items.Add(Field.Name, Field.GetValue(Field))
                End If
            Next
        End Sub
    End Class

    Public Class completionListItem
        Public ReadOnly Text As String
        Public Sub New(ByVal SetText As String)
            Text = SetText
        End Sub
        Public Sub New()
        End Sub
    End Class

    Public Class completionList
    End Class
End Namespace

Enumerable of String ver 2 (VB , enum , .net)

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

In an earlier post about making an enumerable of strings I proposed a method to have string enumerated for better intellisense. I suggest you read that post for the background. I have discovered another way to do something similar. This code also provides the great intellisence of an enumerable but does not use and enumerable. It only works in VB. The big change is a post I found on Stack overflow about the Hidden Features of VB.Net. It is the completionlist tag. Note this tag below. Here is good example is a post by Dustin Campbell – Lesser-Known VB: Customizing the Completion List showing how it is done. In my implementation I also needed a way to find out is a string was in the enumerated items. For that I had to do reflection (WARNING speed reduction). Unlike the previous example this uses full on reflection. I still am not sure if tags are as detrimental as regular reflection. Be sure to do performance testing on this example before using it.

Here is the Completion list XML decorating a new class that will hold the strings.

''' <completionlist cref="InventoryStatusCodes"/>
Public Class InevntoryStatusCode
    Private ReadOnly _Text As String
    Public Sub New(ByVal Text As String)
        _Text = Text
    End Sub
    Public ReadOnly Property GetScreenText() As String
        Get
            Return _Text
        End Get
    End Property
End Class

Here is how we apply the string to the names that will display like when you use an enumerable

Public NotInheritable Class InventoryStatusCodes
    Public Shared ReadOnly Open As New InevntoryStatusCode("O")
    Public Shared ReadOnly Closed As New InevntoryStatusCode("C")
    Public Shared ReadOnly OnRequest As New InevntoryStatusCode("R")
End Class

Here is the function I use when I have some screen text and I want to return the proper enumerated value. If it is supplied a “O” it would return and Open Inventory Status code object.

Public Function ScreenTextToStatusInventoryCode(ByVal ScreenText As String) As InevntoryStatusCode
        Dim i As New InventoryStatusCodes
        Dim t As Type = i.GetType
        Dim f() As FieldInfo = t.GetFields

        Dim isc As InevntoryStatusCode '= InventoryStatusCodes.Open

        For Each Field In f
            isc = Field.GetValue(Field)
            If ScreenText = isc.GetScreenText Then Return isc
        Next

        Return Nothing
    End Function

Here is an example of it being used

'We read in an O string text from the old mainfram screen.
Dim IncomingStringText As String = "O"
If IncomingStringText = InventoryStatusCodes.OnRequest.GetScreenText Then
    'This if statement can be used to test if it was Open 
End If

'We can also store it in an object like
Dim Codes As New List(Of InevntoryStatusCode)

Codes.Add(ScreenTextToStatusInventoryCode(IncomingStringText))

Here is a screen shot of the intellisense in action

image

Let me know what you think but please be kind.

Enum Intellisense

Enumerable of string (VB, enum, .net)

The Story

I was recently working on an interface to an old mainframe. I was screen scraping a terminal emulator and putting the data into objects for analysis. It was actually fun as I was using some technologies I hadn’t used yet. I wanted to use enums for my object so the intellisense would look real good. The problem is that each item translates to a character or short string on the screen. I wanted a way to keep track of the screen text that represented the enum. For an example lets use an old travel agency system for hotels. There is a screen that displays availability of a hotel. There is a screen that will display the availability of a hotel for a specific day. Each day will be either open, closed, or request. On the text screen from the mainframe these were represented as O=open, C=closed, and R=request. I created a quick enum and then I had to figure out how to associate the text from the screen to the enum in a format that is easy to read in case we need to make a change in the future (even though all these systems are going away.) Here was my first attempt

Public Enum InventoryStatusCode
    Open
    Closed
    OnRequest
End Enum

That was easy and clear enough. Sow to associate the text on the terminal screen to the proper enum

Function GetInventoryStatusCode(ByVal ScreenText As String) As InventoryStatusCode
    Select Case ScreenText
        Case "O"
            Return InventoryStatusCode.Open
        Case "C"
            Return InventoryStatusCode.Closed
        Case "R"
            Return InventoryStatusCode.OnRequest
        Case Else
            Throw New ArgumentException("Screen text " & ScreenText & " not valid for Inventory Status Code")
    End Select
End Function

Wow, is that a lot of code for so little. I am spoiled when I am using enums that can equate to meaningful numbers. If the system used 1=open, 2=closed, and 3=request then I could use the number but I couldn’t request a change to a system that is older than I am. I could use constants but I like how the enums were strongly types and grouped together so they just pop up after you hit the equal sign. Enum allows you to assign a type but it must be I searched for quite a while and found several posts about how to match a string to the enum name but I also discovered that the framework has that built in now with [Enum].Parse(GetType(InventoryStatusCode), “O”). The problem with this approach is that the enums would be InventoryStatusCode.O instead of InventoryStatusCode.Open. That tends to deflect the benefit of the readability of enums. Also, for this specific implementation some of the screen codes were special characters and blanks which would not be allowed. I finally ran across a post suggesting attributes and I like how concise it looked. There was a built in description attribute but I didn’t use that here because I might want descriptions also in the future. It also provided me the opportunity to try my hand at writing my own attribute. Hopefully it’s won’t cause confusion with the added code in this example.

My Solution

Here is what I came up with. There are probably other ways. Let me know what you would do in the comments. This example supposes that you have three enum items of Open, Closed and OnRequest and you with to associate the text O,C and R with them respectively.

Public Enum InventoryStatusCode
    <ScreenText("O")> Open
    <ScreenText("C")> Closed
    <ScreenText("R")> OnRequest
End Enum

Then I can use the helper function below. If I pass in “O” I will receive the InventoryStatusCode.Closed like below:

ParseScreenText(GetType(InventoryStatusCode), "O")

I can also accomplish the opposite and receive the “O” by doing the following:

InventoryStatusCode.Open.GetScreenText

That is how it looks when used. Looks pretty clear to me. We will need some more code make it all work

Here is the attribute definition:

''' <summary>
''' USed to identify the text on the terminal screen represented by an enum
''' </summary>
''' <remarks></remarks>
<AttributeUsage(AttributeTargets.All)> _
Public Class ScreenText
    Inherits System.Attribute

‘Private fields. Private _Text As String

‘This constructor defines two required parameters: name and level. Public Sub New(ByVal Text As String) Me._Text = Text End Sub

‘Define Name property. ‘This is a read-only attribute. Public Overridable ReadOnly Property Text() As String Get Return _Text End Get End Property End Class

Here is the function so you can pass in some screen text and receive the associated enum. I couldn’t get this to work as an extension of enum which would have been nice.

''' <summary>
    ''' Returns the proper enumerator that matches the provided screen text
    ''' </summary>
    ''' <param name="en">Enumerator type to search</param>
    ''' <param name="ScreenText">Screen text to be matched to an enumerator</param>
    ''' <returns>Proper enumerator that matched the screen text</returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function ParseScreenText(ByVal en As Type, ByVal ScreenText As String) As [Enum]
        'Dim enumitem As [Enum]
        For Each enumitem In [Enum].GetValues(en)
            If GetScreenText(enumitem) = ScreenText Then
                Return enumitem
            End If
        Next
        Return Nothing
    End Function
End Module

Here is the extension on the enum class. Module enumExtensions ”’ <summary> ”’ Provides the attribute value ScreenText on the enumerator. ”’ The screen text is the representation of the value on a terminal screen ”’ </summary> ”’ <param name=”en”>Enumerator for which the screen text will be returned</param> ”’ <returns>String of the text representation on a terminal screen for this enumerator</returns> ”’ <remarks></remarks> <Extension()> _ Public Function GetScreenText(ByVal en As [Enum]) As String Dim type As Type = en.[GetType]() Dim memInfo As MemberInfo() = type.GetMember(en.ToString()) If memInfo IsNot Nothing AndAlso memInfo.Length > 0 Then Dim attrs As Object() = memInfo(0).GetCustomAttributes(GetType(ScreenText), False) If attrs IsNot Nothing AndAlso attrs.Length > 0 Then Return DirectCast(attrs(0), ScreenText).Text End If End If Return en.ToString() End Function End Module

Isn’t that slower?

The main disadvantage of this is that it appears to use reflection which reduces performance. It seems to me that Dynamic data uses attributes and thus reflection a lot. They may have a way to perform it once up front which reduces the impact. In my application I had seconds of wait times between commands due to legacy systems so the performance should not be a factor.

Isn’t that bad Object Oriented Design?

It’s considered bad object oriented design because “we’re asking another class to retrieve information about our enum type”. Isn’t the enum.parse doing about the same thing though? Would it be good OO if I could get the extension method to work? I’m thinking it’s worth it for this case.

Why not just make a class?

I found a post suggesting that you strongly type your domain values which accomplishes much of the same goals and is probably more performant when looking up the codes. I like the syntactic goodness of enums when coding. You don’t get this great intellisense from a class. when you hit the equals key it just lists the applicable items. When using a class it lists all the objects.

Enum Intellisense

It makes it quick for coding. If someone knows how to get intellisense to automatically show you the options after hitting the equal sign then I would suggest a class. The syntax of the class is a little more verbose but is as legible in my opinion. I guess you could just remember that the type begins with “inven” and the the intellisense would pick up the rest.

Summary Justification

In this case I feel the less verbose and clear syntax makes up for the performance and an additional helper class. I could go the the class option if performance became an issue. Tell me what you think.

Other Sources

I’ve done more searching while writing this post and found a whole lot more.

Who thought this area was so intricate. I guess that is because enums are really syntactic sugar in a way. I think this needs more investigations. Please comment your ideas and suggestions.