0

I am reading data from an INI file. INI file look like this

[config]
TotalElements=16

#Special Case only elements
ElementNo1=9
ElementNo2=10
ElementNo3=11
ElementNo4=12
#
ElementNo5=2,17
ElementNo6=2,18
ElementNo7=2,19
ElementNo8=2,20
#
ElementNo9=3,17
ElementNo10=3,18
ElementNo11=3,19
ElementNo12=3,20
#
# Special Case only Elements
ElementNo13=9
ElementNo14=10
ElementNo15=11
ElementNo16=12

I am reading the Data from file like this

Dim inifile As New IniFile()
inifile.Load(filePath)
ElementValue = inifile.Sections("config").Keys("TotalElements").Value

For i = 1 To totalElement 
  elementValue = iniSetting.Sections("config").Keys("ElementNo" & i)Value
  ListOfElementNumbers.Add(New ElementMapping With {.ElementNumber = i, .ElementValue = 
  ElementValue, .specialElement = SpecialElement.SpecialElementValue})

Next

Data Structure Look like this

Public Class ElementMapping

Public ElementNumber As Integer
Public ElementValue As Integer
Public specialElement As SpecialElement
End Class

Public Enum SpecialElement
SpecialElementValue
End Enum

Now What i want to do is when i am reading the INI file when the Comment "#Special Case only elements" Comes from next line until the "#" Comes again i want to assign this to Enum in my data structure. And if this comment somewhere in the file again i want to do the same for that special data as well. I am using "MadMilkman.Ini" Library to read the data from Ini file.

Zubair
  • 27
  • 1
  • 7
  • Enums cannot be created dynamically, they have to be available at compile time. You can pre-build an assembly, but I am not sure that does what you want. See [here](https://stackoverflow.com/questions/725043/automatically-create-an-enum-based-on-values-in-a-database-lookup-table). This link is c# and getting the values from a database, but the principles are the same. – Jonathan Willcock Apr 08 '22 at 12:23
  • Does this answer your question? [Automatically create an Enum based on values in a database lookup table?](https://stackoverflow.com/questions/725043/automatically-create-an-enum-based-on-values-in-a-database-lookup-table) – Jonathan Willcock Apr 08 '22 at 12:24
  • @JonathanWillcock no it is not what i am looking for. i already have values in my file and there is certain condition on which i want to assign Enum – Zubair Apr 08 '22 at 12:57
  • @JonathanWillcock actually what i want is i have some special elements inside my INI file when these special elements comes in the file i want to assign it to the Enum and when these special elements are not in the file everything should be normal – Zubair Apr 08 '22 at 13:12
  • Maybe create a new INI `Section` for those special elements? – HardCode Apr 08 '22 at 13:52
  • @HardCode can you post your solution how you are thinking of it? – Zubair Apr 08 '22 at 14:30
  • @EricTuco you already have a section named `[config]`. Maybe create a section named `[SpecialElements]`? – HardCode Apr 08 '22 at 14:33
  • @HardCode i don't want to do it hard coded is there anyway like while reading the INI file i read that specific elements and combined them in one section and later read it and assign it to enum. – Zubair Apr 08 '22 at 14:45

1 Answers1

1

Try this:

Public Class ElementMapping
    Public ElementNumber As Integer
    Public ElementValue As Integer
    Public SpecialElement As Nullable(Of SpecialElement)
End Class
Dim iniOptions As New IniOptions() With {.CommentStarter = IniCommentStarter.Hash}
Dim inifile As New IniFile(iniOptions)

' ...

Dim isSpecialCase = False
For i = 1 To totalElement

    Dim iniKey = iniSetting.Sections("config").Keys("ElementNo" & i)
    Dim iniValue = iniKey.Value

    If (String.Equals(iniKey.TrailingComment.Text, String.Empty)) Then
        isSpecialCase = False
    ElseIf (iniKey.TrailingComment.Text?.IndexOf("Special Case only elements", StringComparison.OrdinalIgnoreCase) >= 0) Then
        isSpecialCase = True
    End If

    Dim element As New ElementMapping With {.ElementNumber = i, .ElementValue = iniValue}
    If (isSpecialCase) Then element.SpecialElement = SpecialElement.SpecialElementValue
    ListOfElementNumbers.Add(element)

Next

In short, I've changed the ElementMapping.SpecialElement to a nullable type so that I can differentiate when the element has a special value and when it doesn't.

I'm using isSpecialCase to detect if the key is inside the required range. If it is then I'm setting the ElementMapping.SpecialElement property to SpecialElementValue, otherwise it will be Nothing.

For the INI sample you provided, this will result in having your first 4 and last 4 elements with SpecialElementValue and the rest will have Nothing.

Last, I would suggest that instead of using this nullable type, you consider using something like this:

Public Enum SpecialElement
    None,
    SpecialElementValue
End Enum

Then instead of Nothing you would use SpecialElement.None.

Mario Z
  • 4,328
  • 2
  • 24
  • 38
  • Thanks for the answer. There is some problem in all the iteration i am getting the Enum value as Nothing :( – Zubair Apr 20 '22 at 08:22
  • The problem is it not going inside the if statement where it is setting isSpecialCase = True. That is why all the values are coming as Nothing – Zubair Apr 20 '22 at 08:35
  • @EricTuco oh I forgot to mention, you need to use this: `Dim inifile As New IniFile(New IniOptions() With {.CommentStarter = IniCommentStarter.Hash})` – Mario Z Apr 21 '22 at 02:13
  • Great!!! It Works :) Thanks – Zubair Apr 21 '22 at 05:54