Results 1 to 27 of 27

Thread: [RESOLVED] Array Questions

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Array Questions

    Heyy... So, I have this .txt file with 5 lines, let's say:

    I
    Have
    An
    Easy
    Question

    If I wanted to add those lines to an array list position, how would I do that?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Array Questions

    Instead of an array, use a List(of string) instead. It'll be much more flexible and user-friendly than an array will ever be (but you still have to learn arrays, which shows the basics of collecting things.)

    All you really need to do is ADD them to the list, and viola, you're golden.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    How could i add them? Using the streamreader.readline? or read to end?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Array Questions

    As long as the function returns a string, you can add it to the list (as long as the list is set for strings.)

    Here's an example:

    Code:
                Dim s As New StreamReader
                Dim L As New List(Of String)
    
                L.Add(s.ReadLine)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    I tried your code, but it only seems to add the first line ONLY...
    If I chage it to: line.AddRange(reader.ReadToEnd), it adds all the lines in the first position of the list...
    Last edited by tassa; May 12th, 2009 at 02:23 AM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    I fixed the code... This is it:

    vb Code:
    1. Using reader As New IO.StreamReader("C:\Array.txt")
    2.             line.AddRange(Split(reader.ReadToEnd, vbNewLine))
    3.         End Using

    Any recommendations or anything.. Please do
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Array Questions

    Try this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim fileContents As String = File.ReadAllText("C:\temp\test.txt")
    3.     Dim splitter() As Char = {Chr(13)}
    4.     Dim myArray() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
    5.  
    6.     'we have everything in the myArray array now
    7.     'lets fill it in a listbox for example
    8.     ListBox1.DataSource = myArray
    9. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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

    Re: Array Questions

    vb.net Code:
    1. Dim lines As String() = IO.File.ReadAllLines("file path here")
    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

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Array Questions

    Hi Pradeep..

    Wont this work ?

    vb Code:
    1. Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt")
    2.         'we have everything in the myArray array now
    3.         'lets fill it in a listbox for example
    4.         ListBox1.DataSource = myArray
    Please mark you thread resolved using the Thread Tools as shown

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. Dim lines As String() = IO.File.ReadAllLines("file path here")
    Where should I put that line of code? What's the difference between an Array and a List(Of T)? Doesn't Padreep's code and mine do the same?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Array Questions

    That's much better and easier.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    Which one?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Array Questions

    vb Code:
    1. Dim myArray() As String = System.IO.File.ReadAllLines("C:\temp\test.txt")
    This. It is simple one line code that would get all the lines in the file in an array.

    My code in post #7 was reading the file, then splitting it into an array. The only additional thing it was doing is removing any blank lines, if any. So that depends on which one you want to use.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    Oh, I thought that what jmcilhinney wrote was a correction to your code. But the question remains... What's the basic difference between a list and an array?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Array Questions

    An array is a basic structure (usually found in all languages), which provides a mechanism to address a (consecutive) sequence of memory locations by its index value. All the elements in an array are of same data type i.e. they occupy same memory size. Usually accessing an element is very fast since the memory blocks are of equal size. So we just multiply the index value with memory size and we get the address of the element. However, adding/inserting/removing elements is a cumbersome operation.

    A List is a specialized .NET class that can be used to store elements more or less like arrays. But it has many advanced features. You can access elements by their index values just like arrays. And adding/inserting/deleting elements is just a matter of a simple function call to the user, besides many more advanced features.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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

    Re: Array Questions

    Think of an array as an egg carton. When you make an egg carton it is a specific size, i.e. it can hold a specific number of eggs, and that size doesn't change. Initially each cup in the egg carton is empty. You can put an egg in each cup and you can remove eggs from those cups but the carton never changes size.

    A List is like a magic egg carton. When you create it it exists but it has no cups. When you add an egg to magic carton it grows automatically so that it has just one cup for that one egg. If you add another egg the magic carton grows again to have two cups. You can then insert another egg in between those two and the carton grows again. You can then remove one of the eggs and the carton shrinks automatically so it's just got two cups for the two remaining eggs. If clear out all the remaining eggs the magic carton will shrink down again so it has no cups at all.

    Arrays are very efficient so they should be used where you don't need this dynamic resizing behaviour. Lists are more convenient than arrays when dynamic resizing is required and, if a lot of resizing is required, then a List becomes more efficient than an array. That's because to "resize" an array you actually have to create a new array and copy all the elements from the original.

    Here's something that not everyone knows: internally, a List actually stores its items in an array. The List provides fancy logic to hide that fact from the user but it's not really magic. Each time you add an item to a List it first checks whether the array is large enough to hold it. If it's not it will resize the array as mentioned before. The thing is, the array doesn't get resized every time you add an item. When the array is resized its size is doubled. That keeps the number of copying operations to a balanced level and, the bigger the array gets, the less often it gets resized.
    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

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    Oh, thanks a lot for that explanation. I liked the (magic) egg carton example.

    But, just one last question:

    Where would I use the dynamic resizing option(?) of the Lists?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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

    Re: Array Questions

    Quote Originally Posted by tassa View Post
    Where would I use the dynamic resizing option(?) of the Lists?
    Run this code:
    Code:
    Dim eggCarton As New List(Of String)
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    
    eggCarton.Add("Egg 1")
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    
    eggCarton.Add("Egg 2")
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    
    eggCarton.Insert(1, "Egg 3")
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    
    eggCarton.RemoveAt(0)
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    
    eggCarton.Clear()
    
    MessageBox.Show(String.Join(ControlChars.NewLine, _
                                eggCarton.ToArray()), _
                    "Count = " & eggCarton.Count)
    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

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    Quote Originally Posted by Pradeep1210 View Post
    Try this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim fileContents As String = File.ReadAllText("C:\temp\test.txt")
    3.     Dim splitter() As Char = {Chr(13)}
    4.     Dim myArray() As String = fileContents.Split(splitter, StringSplitOptions.RemoveEmptyEntries)
    5.  
    6.     'we have everything in the myArray array now
    7.     'lets fill it in a listbox for example
    8.     ListBox1.DataSource = myArray
    9. End Sub

    Pradeep
    Hey Pradeep, I was looking into your code (again), and I didn't know why is that you used:

    vb.net Code:
    1. Dim splitter() As Char = {Chr(13)}

    What does the "13" stand for exactly?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  20. #20

    Re: Array Questions

    Chr(13) I assume is for the enter button.

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

    Re: Array Questions

    Splitting on Chr(13), which is a carriage return, is not such a good idea because, by default, a Windows text file should use a carriage return and line feed pair as a line break. If you split on just the carriage return then your substrings will all have leading line feeds.
    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

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    In other words....?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  23. #23

    Re: Array Questions

    This:
    Code:
    1
    2
    3
    4
    5
    would become this:
    Code:
    12345
    Understand now?
    EDIT: wait....that's not right..thats for replacing..oops.
    EDIT2: If I understand right, wouldn't it just add an extra carriage return between each line?

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

    Re: Array Questions

    Quote Originally Posted by tassa View Post
    In other words....?
    If your file uses carriage returns only as line breaks then you should split on ControlChars.Cr. If your file uses carriage return and line feed pairs as line breaks then you should split on ControlChars.NewLine or Environment.NewLine. I've never tried it but you may be able to handle both with the one line of code by using a String array as delimiters and specifying both, with NewLine first so that it gets preference. It might be worth testing that to see if it works, if you need it.
    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

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Array Questions

    What exactly is a carriage? What's it function?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  26. #26

    Re: Array Questions

    Quote Originally Posted by tassa View Post
    What exactly is a carriage? What's it function?
    http://upload.wikimedia.org/wikipedi...a/a5/Enter.png

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

    Re: Array Questions

    Quote Originally Posted by tassa View Post
    What exactly is a carriage? What's it function?
    The names hearken back to the days of typewriters. The carriage was the apparatus that held the paper and it would physically move to the left as you typed so that the characters would appear left-to-right. At the end of a line you would want the carriage to return to its original position. The line feed occurred by turning the roller that held the paper within the carriage so that the paper was feed up the height of one line. Generally speaking, when you finished typing a line you would hit the handle on the left hand side and it would return the carriage to its original position and feed the paper one line, hence the names carriage return and line feed.

    In Windows a line break in a text file is represent by the ASCII characters for a carriage return (13) and a line feed (10). In C# and other C-based languages these are represented with "\r" and "\n". In VB they can be represent individually with ControlChars.Cr and ControlChars.Lf or they can be represented together with ControlChars.CrLf or ControlChars.NewLine. Environment.NewLine will return a string containing a carriage return and a line feed on Windows systems, but on other systems it will return only a line feed because non-Windows systems use only a line feed to represent a line break.

    Note that, these days, Windows will interpret a line feed on its own as a line break. This is one reason that it's bad to split on just carriage returns. In that case it will leave behind line feeds and those will become line breaks preceding each substring.
    Last edited by jmcilhinney; Jun 4th, 2009 at 09:41 PM.
    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

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