Results 1 to 6 of 6

Thread: While Loop Confusion

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    While Loop Confusion

    I am building some code to read a text file which will contain values for some variables which are subject to change. I'm reading these from a config file so that later when someone changes their mind about the MTA's address for example, I can simply change this text file and not have to recompile the .exe file(s).

    So I thought I'd use a format something like this for the configuration files:
    #########################################
    # Name of configuration file
    # Date of configuration file
    #
    # A bunch of information about what this config file is used
    # for and it's format
    #
    ########################################
    value #1
    value #2
    value #3
    value #4
    value #5
    Okay, that said, I'm using some code like this to read through this configuration file. I want to ignore all the lines which begin with a # as these are just comments.
    Code:
    Sub Main()
    
            Try
                Dim objReader As New StreamReader("C:\0\HatchWizardEmail.conf")
                Do Until Left(objReader.ReadLine, 1) <> "#"
                Loop
                Dim linkname As String = objReader.ReadLine
                Dim email_username As String = objReader.ReadLine
                Dim email_passwd As String = objReader.ReadLine
                Dim mta_port As String = objReader.ReadLine
                Dim mta_dns As String = objReader.ReadLine
                Dim email_from As String = objReader.ReadLine
                objReader.Close()
    This almost works. I apparently don't have the loop correct because it reads and ignores all the lines beginning with #, but it also ignores the next line and doesn't start assigning variables until it gets one line past where I want it to start. I have tried Do While, Do Until, etc... and I keep getting the same results.
    Last edited by Vladamir; Apr 28th, 2014 at 03:31 PM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: While Loop Confusion

    Without testing, try:
    Code:
    Do
                Loop  Until Left(objReader.ReadLine, 1) <> "#"
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: While Loop Confusion

    Thanks opus. That worked but I still had to add a blank line after the comment section in order for it to pickup reading variables where I want it to. One of the IT guys is telling me to make an entry in a new table in the MySQL databases to do this. I guess I could do that but this simple text file is really what I'm after.

    I'm finding that this solution is 6 of one and 1/2 a dozen of the other....!
    Last edited by Vladamir; Apr 28th, 2014 at 03:50 PM.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: While Loop Confusion

    Quote Originally Posted by Vladamir View Post
    That worked but I still had to add a blank line after the comment section
    Becasue when your loop exits you have already read the first line that you actually want!
    Just a way to think about the logic...

    Code:
    Dim line As String
    
    Do
        line = objReader.ReadLine
    Loop While line.StartsWith("#")
    
    Dim linkname As String = line ' 1st line we want
    Dim email_username As String = objReader.ReadLine ' 2nd line
    Dim email_passwd As String = objReader.ReadLine ' 3rd , etc, etc..
    Last edited by Edgemeal; Apr 28th, 2014 at 04:08 PM. Reason: simplified ?

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: While Loop Confusion

    Why not doing something like this:

    Code:
    Module Module1
    
        Sub Main()
    
            Dim dt As DataTable = Configuration_To_DataTable(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "config.txt"))
    
            For Each row As DataRow In dt.Rows
                Dim str As String = String.Empty
                For i As Integer = 0 To dt.Columns.Count - 1
                    str &= row(i).ToString & " | "
                Next
    
                str = str.Substring(0, str.Length - 3)
                Console.WriteLine(str)
            Next
    
            Console.ReadLine()
    
        End Sub
    
        Private Function Configuration_To_DataTable(ByVal path As String) As DataTable
            Dim dt As DataTable = New DataTable
    
            Dim lines = From line In IO.File.ReadAllLines(path)
                        Where line.StartsWith("#") = False
    
            For c As Integer = 0 To lines(0).Split({" "}, StringSplitOptions.None).Count - 1
                dt.Columns.Add(New DataColumn("Column" & c.ToString))
            Next
    
            For l As Integer = 0 To lines.Count - 1
                Dim r As DataRow = dt.NewRow
    
                For c As Integer = 0 To lines(l).Split({" "}, StringSplitOptions.None).Count - 1
                    r(c) = lines(l).Split({" "}, StringSplitOptions.None)(c)
                Next
    
                dt.Rows.Add(r)
            Next
    
            Return dt
        End Function
    
    End Module
    (I used MyDocuments/config.txt for my file)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: While Loop Confusion

    Ah, yes....I think I see your logic very clearly. Thanks everyone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width