Zubair
Zubair

Reputation: 27

Reading Data from an Ini File Vb .Net

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.

Upvotes: 0

Views: 1154

Answers (1)

Mario Z
Mario Z

Reputation: 4381

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.

Upvotes: 1

Related Questions