Results 1 to 10 of 10

Thread: [RESOLVED] Line to array help?

  1. #1

    Thread Starter
    Lively Member crazy1993's Avatar
    Join Date
    Sep 2010
    Location
    Rochester, NY
    Posts
    85

    Resolved [RESOLVED] Line to array help?

    Hello Everyone!

    I am trying to take a line from a text file and get the second half of the line "my.settings.useqs = true" is my example and i want to get true out of the line and then make a textbox contain the text aftter the = sign basicly.

    http://www.vbforums.com/showthread.php?t=626903
    I posted something similar but i am having a hard time trying to figure this out. I also don't really understand how I used it in the first place.

    If anyone can help me that would be great!

    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Line to array help?

    You've already been shown in that other thread how to split a string on an '=' and get the second half. That's all you need to do, once you have the string. To get that, you can either open a StreamReader and call ReadLine until you get the desired line, or you can call File.ReadAllLines (which is also mentioned in that other thread) to get a String array and then get the desired element from that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member crazy1993's Avatar
    Join Date
    Sep 2010
    Location
    Rochester, NY
    Posts
    85

    Re: Line to array help?

    Yes i am aware of that but i don't understand the part about using a textbox this time. I used a listbox last time and used display member and such then. Textboxs, don't have display member so that is were my problem comes in.

    Code:
            Dim items = (From line In IO.File.ReadAllLines(FILE_NAME) Where line.StartsWith("my.settings") Let parts = line.Split(New String() {" = "}, StringSplitOptions.None)
                                                       Select New With {.setting = parts(0).Split("="c)(1), .value = parts(1).Split("="c)(1)}).ToArray()
    I think i made a mistake - the bolded code is in yellow and tells me:

    "Index was outside the bounds of the array."

    My file contains this

    Code:
    2.1.0.0
    my.settings.font = "Arial"
    my.settings.indent = "10"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Line to array help?

    When you call Split it returns a String array. Wen you get an element from a string array it returns a String. The Text property of a TextBox is type String. You assign the String to the property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member crazy1993's Avatar
    Join Date
    Sep 2010
    Location
    Rochester, NY
    Posts
    85

    Re: Line to array help?

    i somewhat understand but not fully. Would you be able to explain more please?

    Appreciate the help

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Line to array help?

    This is doing almost exactly what you want:
    Code:
    .setting = parts(0).Split("="c)(1)
    You just have to assign the String to the Text of the TextBox instead of the 'setting' property of a dynamically created object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Line to array help?

    The file that you are showing is much different than the file that you were showing in that other thread. The code certainly isn't going to work the same for one as for the other. For example, in the first thread, there were two = signs, but in the current file, as you have shown it, there is only one per line. Therefore, when you split on =, there will be a parts(0) and a parts(1), but neither of them will have = signs in them, so if you do split them on = (which is what you are doing), then the resulting arrays will only have one element. Therefore, when you attempt to look at the second element with (1), you are out of bounds.
    My usual boring signature: Nothing

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Line to array help?

    Ah, JMC has not gone to bed yet.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Line to array help?

    Any moment now. It's 2:20 AM. Yawwwwnn! Tehe. I just yawned when I typed "yawn". Maybe I should type "get rich".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member crazy1993's Avatar
    Join Date
    Sep 2010
    Location
    Rochester, NY
    Posts
    85

    Re: Line to array help?

    I apologize i completely forgot about this thread over Thanksgiving break! - Anyways, I have, with the help of a friend, figured out a way to do what was inteneded, we took a completly different aproach to the problem and foujnd some way to do it with an array and loops - I will look at what you guys have psoted though still and (due to harddrive failure that night! ARG) May end up rebuilding doing a new method since that friend has returned back to school - Recovery of hds is expensive Stupid logic file whatever hd! lol At least its under warrenty still!

    Thank you all! You guys are always a great help! Peace

Tags for this Thread

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