הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, March 30, 2010 10:57 AM
Hey, I'm sort of new to VB and have to use it for my Software Design & Development Major Project
I'm making a blackJack game, and i the names of all users in a file called "CurrentUserNames.txt"
I'm struggling to get VB to read this file (items are line by line not separated by a comma or something) and then save it into an array called CurrentUsers(). This array is then displayed in a listbox called "lstAllUsers"
I can get VB to read the file and put the FIRST item in the file into the FIRST line of the list box, or i can get VB to put ALL the items in the file into the listbox, separated by the little [] squares when the program runs e.g listbox, line 1 says - "Current [][] Names"
this is the line I have -
up top
Imports System.IO
Dim readNames As StreamReader
further down in the load form section
readNames = New StreamReader("C:\CC BlackJack Bandits\Resources\CurrentUserNames.txt")
lstExistingUsers.Items.Add(readNames.ReadLine)
so say my text file is
"Current
User
Names
Go
Here"
thats what i want the list box to display, line by line, any help?
(would be great if you coul also show me how to do the reverse, for savng changes to this array
thanks
(i wasnt sure if this was wherei asked this question or not)
All replies (17)
Tuesday, March 30, 2010 11:16 AM ✅Answered
There's no need to use a streamreader for this. The System.IO namesapce provides a ReadAllLines method for the File object that makes the job simple, for instance:
Dim path As String = "c:\CC BlackJack Bandits\Resources\CurrentUserNames.txt"
Dim readText() As String = File.ReadAllLines(path)
To save the array:
File.WriteAllLines(path, readText)
ReadAllLines will copy all the lines in the file into an array of string, and WriteAllLines writes them back. For more description, see:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
Tuesday, March 30, 2010 9:42 PM ✅Answered
Hopefully the following will simplify it rather than further confuse things:
Public Class Form1
Dim CurrentUsers() As String
Dim source As String = "C:\CC BlackJack Bandits\Resources\CurrentUserNames.txt"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CurrentUsers = System.IO.File.ReadAllLines(source)
For i As Integer = 0 To CurrentUsers.Length - 1
ListBox1.Items.Add(CurrentUsers(i))
Next
End Sub
End Class
The essence of it is what Acamar has explained. I hope it helps.
Good luck! :)
Wednesday, March 31, 2010 9:27 PM ✅Answered
The problem you have run into is one reason that arrays are not the best option when the processing starts to become more complex. They are very suitable for static data, but if things can change, a list is better. You could ReDim the array, but to change it to a list instead your code could become:
Dim CurrentUsers as List(Of String) = New List(Of String)
so you could add to it with:
CurrentUsers.Add(txtNewUser.Text)
There are some small differences between an array and a list, but they are mostly to do with additional features available in the list - apart from adding to a list (compared with inserting into an array) you can pretty much consider the list as an array with extra features.
See: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx
Tuesday, March 30, 2010 10:58 AM
Hey, I'm sort of new to VB and have to use it for my Software Design & Development Major Project
I'm making a blackJack game, and i the names of all users in a file called "CurrentUserNames.txt"
I'm struggling to get VB to read this file (items are line by line not separated by a comma or something) and then save it into an array called CurrentUsers(). This array is then displayed in a listbox called "lstAllUsers"
I can get VB to read the file and put the FIRST item in the file into the FIRST line of the list box, or i can get VB to put ALL the items in the file into the listbox, separated by the little [] squares when the program runs e.g listbox, line 1 says - "Current [][] Names"
this is the line I have -
up top
Imports System.IO
Dim readNames As StreamReader
further down in the load form section
readNames = New StreamReader("C:\CC BlackJack Bandits\Resources\CurrentUserNames.txt")
lstExistingUsers.Items.Add(readNames.ReadLine)
so say my text file is
"Current
User
Names
Go
Here"
thats what i want the list box to display, line by line, any help?
(would be great if you coul also show me how to do the reverse, for savng changes to this array
thanks
(i wasnt sure if this was wherei asked this question or not)
Tuesday, March 30, 2010 11:33 AM
There is nothing wrong with the way Acamar shows, but going in your way and then also adding some things which are more in the line of current Visual basic I would do it like this.
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using readNames As New StreamReader("C:\CC BlackJack Bandits\Resources\CurrentUserNames.txt")
lstExistingUsers.Items.Add(Split(readNames.ReadToEnd, vbCrLf))
End Using
End Sub
End Class
Success
Cor
Tuesday, March 30, 2010 11:40 AM
Hi,
We become not happy if a question is placed in more forums.
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/587b7cd2-4913-4423-a714-0d56a56849d6
Success
Cor
Tuesday, March 30, 2010 4:02 PM
How about soemthing like this to get you started:
ListBox1.Items.AddRange(System.IO.File.ReadAllLines("../../usernames.txt"))
jon.stromer.galley
Tuesday, March 30, 2010 8:02 PM
There's no need to use a streamreader for this. The System.IO namesapce provides a ReadAllLines method for the File object that makes the job simple, for instance:
Dim path As String = "c:\CC BlackJack Bandits\Resources\CurrentUserNames.txt"
Dim readText() As String = File.ReadAllLines(path)To save the array:
File.WriteAllLines(path, readText)
ReadAllLines will copy all the lines in the file into an array of string, and WriteAllLines writes them back. For more description, see:
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
Hey, I've been trying to do that method but when I try do the line
Dim readText() As String = File.ReadAllLines(path)
I get the line ' "File" is not declared' --> what do i have to dim file as?
And also if i try just do
Dim readText() As String
and then further down I put in the line
readText() = File.ReadAllLines(namespath)
I get "Value of type '1-dimensional array of String' cannot be converted to 'String'.
Can i fix these up?
Tuesday, March 30, 2010 9:23 PM
Hey, I've been trying to do that method but when I try do the line
Dim readText() As String = File.ReadAllLines(path)
I get the line ' "File" is not declared' --> what do i have to dim file as?
...
and then further down I put in the line
readText() = File.ReadAllLines(namespath)
I get "Value of type '1-dimensional array of String' cannot be converted to 'String'.
The File object is part of sthe System.IO namespace. You need either:
Dim readText() As String = System.IO.File.ReadAllLines(path)
or
Imports System.IO
at the top of the code.
The second ReadAllLines (readText() = File.ReadAllLines(namespath)) is not required and should not be used.
Wednesday, March 31, 2010 8:33 AM
Hopefully the following will simplify it rather than further confuse things:
Public Class Form1 Dim CurrentUsers() As String Dim source As String = "C:\CC BlackJack Bandits\Resources\CurrentUserNames.txt" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .Load CurrentUsers = System.IO.File.ReadAllLines(source) For i As Integer = 0 To CurrentUsers.Length - 1 ListBox1.Items.Add(CurrentUsers(i)) Next End Sub End Class
The essence of it is what Acamar has explained. I hope it helps.
Good luck! :)
hey ive got that to work, thankyou to you Frank and Acamar. I have another question,
Say I have a textbox that for a New User, the new user enters their name into and clicks create, this adds their name to the lstbox which contains the currently existing names, but how do i get it to add to the next position in the CurrentUsers() array AFTER the last item in the file?
I'm fairly sure saving it I can do, its just adding this new item to the end of the array i cant do, ive tried increasing the length of the array by 1, but i cant do that cause CurrentUsers.Length is read only,i tried
CurrentUserName = txtNewUser.txt
CurrentUsers(CurrentUsers.Length + 1) = CurrentUserName
but that doesn't work either, when i try run it and add the new name i get the error saying "Index was outside the bounds of the array"
how can i fix this?
thankyou to you both for your help
Wednesday, March 31, 2010 9:33 PM
There are some small differences between an array and a list, but they are mostly to do with additional features available in the list - apart from adding to a list (compared with inserting into an array) you can pretty much consider the list as an array with extra features.
See: http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx
The List(OfT) also has better memory management if I understand it correctly. Am I right Acamar?
Thanks
Wednesday, March 31, 2010 10:03 PM
There is no extra array needed for this problem.
The items can be in the Listbox Items which is simply a collection itself.
Success
Cor
Wednesday, March 31, 2010 10:12 PM
This is the MS description:
"In deciding whether to use the List(Of T) or ArrayList class, both of which have similar functionality, remember that the List(Of T) class performs better in most cases and is type safe. If a reference type is used for type T of the List(Of T) class, the behavior of the two classes is identical. However, if a value type is used for type T, you need to consider implementation and boxing issues.
"If a value type is used for type T, the compiler generates an implementation of the List(Of T) class specifically for that value type. That means a list element of a List(Of T) object does not have to be boxed before the element can be used, and after about 500 list elements are created the memory saved not boxing list elements is greater than the memory used to generate the class implementation. "
( http://msdn.microsoft.com/en-us/library/6sh4ey19.aspx )
I can't find a direct comparison between a list and an array, but I suspect that comments like those above will still apply. Any memory advantage in using an array would be lost if it had to be Dimmed to an arbitrary large size to allow for future growwth, and I would expect that ReDim Preserve for an array would be much more expensive than expanding a list. So my preference is for a List (Of or some other form of generic collection) in any circumstance where the size might change. But an array is still suitable for a collection of value variables where the size can be reliably predicted before it is created.
Wednesday, March 31, 2010 10:13 PM
As soon as there is noting needed to add to a collection while the Class type is known, then The array performs better than any other collection.
Why do you thing that a bitmap is an array?
But like I wrote in my previous message, why would it be used in this case, at least I see not any reason for this.
(With a listview it can be because that performs so extremely slow, but this is about a listbox)
Success
Cor
Wednesday, March 31, 2010 10:28 PM
There is no extra array needed for this problem.
Yes there is. The user's problem statement is "read this file (items are line by line not separated by a comma or something) and then save it into an array called CurrentUsers(). " That's the task they are trying to accomplish. You can provide a simpler solution using the listbox if you prefer, but it won't meet the user's stated requirements.
Wednesday, March 31, 2010 10:57 PM
jgalley provided the simplest correct answer why complicate things . This code will work for adding names . Removing names will reguire more code .
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If File.Exists("CurrentUserNames.txt") Then
lstAllUsers.Items.AddRange(System.IO.File.ReadAllLines("CurrentUserNames.txt"))
End If
End Sub
Private Sub CreateBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateBTN.Click
If Not TextBox1.Text = String.Empty Then
lstAllUsers.Items.Add(TextBox1.Text)
Using SR As New StreamWriter("CurrentUserNames.txt", True) 'true to append file
SR.WriteLine(TextBox1.Text)
SR.Flush()
SR.Close()
End Using
End If
End Sub
End Class
'
Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
Wednesday, March 31, 2010 11:48 PM
A lot of brilliant minds here!
@ Sordafish -> The forum rules are that you should mark the appropriate answer(s) as correct then start a new thread with a new question. It's not a matter of being rude, it's because others will later search and find this when they have problems. Once the thread starts taking a detour into a new area, it's easy to get lost trying to find the answer. I'm sure you understand.
You're in good company here! Good luck with your project. :)