VB6 code works like a treat but need it in vb.net grrr
-
Just an update here is what i have done in vb.net
dim objReader as new system.IO.StreamReader(FILE_NAME)
Do while not EOF(1)
list1.text = names(a) & objReader.readline()
a=a+1
loopobjReader.close
call highscoresFor some reason when i try to run the code it doesn't like the dow while loop and says bad file name or number? If anybody can help me that would be really cool Dan
If you are using StreamReader then you can do this (taken straight from the documentation for StreamReader):
' Create an instance of StreamReader to read from a file. Dim sr As StreamReader = New StreamReader("TestFile.txt") Dim line As String ' Read and display the lines from the file until the end ' of the file is reached. Do line = sr.ReadLine() Console.WriteLine(Line) Loop Until line Is Nothing sr.Close()
You don't need to worry about file numbers in VB.NET.
-
From what I can gather, basically what you are trying to do is to read a list of players and their high scores from a text file, and populate arrays named names and scores. In .Net you need to import System.IO and use a StreamReader to read from the file. An important consideration obviously, is the set up of the original text file. i.e - is it a comma delimted file ?, is each record on a new line ? etc. I threw this together, working on a few assumptions. 1. The text file is stored in the same folder as the application executable. 2. The text file consists of a new line for each player, and each player property is separated by a comma.
names = New List(Of String) scores = New List(Of Integer) Dim strFilename As String = Application.StartupPath & "\\highscores.txt" Dim srScores As New StreamReader(strFilename) While Not srScores.EndOfStream Dim strRead As String = srSave.ReadLine ListBox1.Items.Add(strRead) Dim strSplit() As String = Split(strRead, ",") names.Add(strSplit(0)) scores.Add(strSplit(1)) End While srScores.Close()
I also assumed names and scores were lists rather than arrays. You would obviously need to consider error handling as a major issue which I have completely ignored. Hope this helps.
Cheers Zimvbcoder. This is indeed very helpful :D However i have a few questions
names = New List(Of String) ' vb doesn't like the new list of string?
scores = New List(Of Integer)
Dim strFilename As String = Application.StartupPath & "\highscores.txt"
Dim srScores As New StreamReader(strFilename)
While Not srScores.EndOfStream
Dim strRead As String = srSave.ReadLine
ListBox1.Items.Add(strRead)
Dim strSplit() As String = Split(strRead, ",")
names.Add(strSplit(0)) ' doesnt like names.add?
scores.Add(strSplit(1))
End While
srScores.Close()Also srSave isn't doing anything? I am very grateful for you replying to my post. Many thanks Dan
-
Cheers Zimvbcoder. This is indeed very helpful :D However i have a few questions
names = New List(Of String) ' vb doesn't like the new list of string?
scores = New List(Of Integer)
Dim strFilename As String = Application.StartupPath & "\highscores.txt"
Dim srScores As New StreamReader(strFilename)
While Not srScores.EndOfStream
Dim strRead As String = srSave.ReadLine
ListBox1.Items.Add(strRead)
Dim strSplit() As String = Split(strRead, ",")
names.Add(strSplit(0)) ' doesnt like names.add?
scores.Add(strSplit(1))
End While
srScores.Close()Also srSave isn't doing anything? I am very grateful for you replying to my post. Many thanks Dan
For
List(Of )
you need to importSystem.Collections.Generic
. As forsrSave
, I think you made a typo. It should be.Dim strRead As String = srScores.ReadLine()
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
Cheers Zimvbcoder. This is indeed very helpful :D However i have a few questions
names = New List(Of String) ' vb doesn't like the new list of string?
scores = New List(Of Integer)
Dim strFilename As String = Application.StartupPath & "\highscores.txt"
Dim srScores As New StreamReader(strFilename)
While Not srScores.EndOfStream
Dim strRead As String = srSave.ReadLine
ListBox1.Items.Add(strRead)
Dim strSplit() As String = Split(strRead, ",")
names.Add(strSplit(0)) ' doesnt like names.add?
scores.Add(strSplit(1))
End While
srScores.Close()Also srSave isn't doing anything? I am very grateful for you replying to my post. Many thanks Dan
I assumed that the list of names and the list of scores would be previously declared. To be honest with you I would create a class Player in my app, and in that class you could have a function GetAllPlayers which would look like this :
Imports System.IO
Public Class Player Implements IComparable Private strName As String Private intHighScore As Integer Public Property Name() As String Get Return strName End Get Set(ByVal value As String) strName = value End Set End Property Public Property HighScore() As Integer Get Return intHighScore End Get Set(ByVal value As Integer) intHighScore = value End Set End Property Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim OtherPlayer As Player = CType(obj, Player) Return Me.HighScore - OtherPlayer.HighScore End Function Shared Function GetAllPlayers(ByVal FileName As String) As List(Of Player) Dim lstPlayers As New List(Of Player) Dim strFilename As String = FileName Using srScores As New StreamReader(strFilename) While Not srScores.EndOfStream Dim newPlayer As New Player Dim strRead As String = srScores.ReadLine Dim strPlayerProperties() As String = Split(strRead, ",") newPlayer.Name = strPlayerProperties(0) newPlayer.HighScore = strPlayerProperties(1) lstPlayers.Add(newPlayer) End While End Using Return lstPlayers End Function
End Class
Then you can declare a list(Of Player) in your main form and populate it like this :
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try lstPlayers = Player.GetAllPlayers(My.Settings.HighScoresFile) lstPlayers.Sort() For i As Integer = 0 To lstPlayers.Count - 1 ListBox1.Items.Add(lstPlayers(i).Name & ", " & lstPlayers(i).HighScore) Next Catch ex As System.IO.IOException MsgBox("There was an error retrieving High Scores.") End Try End Sub
This is very simple but a good way to start thinking in a more structured way.
-
For
List(Of )
you need to importSystem.Collections.Generic
. As forsrSave
, I think you made a typo. It should be.Dim strRead As String = srScores.ReadLine()
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
Thanks the man from U.N.C.L.E so are you saying that the following code would become name = New list(of system.collection.generic) ? This comes up saying do i want to change what i have written to system.collection.IEnumerable system.collection.IEnumerator system.collection.Generic.list(ofT) system.collection.queue system.collection.generic.IList(ofT) Many thanks Dan
-
I assumed that the list of names and the list of scores would be previously declared. To be honest with you I would create a class Player in my app, and in that class you could have a function GetAllPlayers which would look like this :
Imports System.IO
Public Class Player Implements IComparable Private strName As String Private intHighScore As Integer Public Property Name() As String Get Return strName End Get Set(ByVal value As String) strName = value End Set End Property Public Property HighScore() As Integer Get Return intHighScore End Get Set(ByVal value As Integer) intHighScore = value End Set End Property Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo Dim OtherPlayer As Player = CType(obj, Player) Return Me.HighScore - OtherPlayer.HighScore End Function Shared Function GetAllPlayers(ByVal FileName As String) As List(Of Player) Dim lstPlayers As New List(Of Player) Dim strFilename As String = FileName Using srScores As New StreamReader(strFilename) While Not srScores.EndOfStream Dim newPlayer As New Player Dim strRead As String = srScores.ReadLine Dim strPlayerProperties() As String = Split(strRead, ",") newPlayer.Name = strPlayerProperties(0) newPlayer.HighScore = strPlayerProperties(1) lstPlayers.Add(newPlayer) End While End Using Return lstPlayers End Function
End Class
Then you can declare a list(Of Player) in your main form and populate it like this :
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try lstPlayers = Player.GetAllPlayers(My.Settings.HighScoresFile) lstPlayers.Sort() For i As Integer = 0 To lstPlayers.Count - 1 ListBox1.Items.Add(lstPlayers(i).Name & ", " & lstPlayers(i).HighScore) Next Catch ex As System.IO.IOException MsgBox("There was an error retrieving High Scores.") End Try End Sub
This is very simple but a good way to start thinking in a more structured way.
This LOOKS pretty awsome :D I have one error and i don't actually know why.
lstPlayers = Player.GetAllPlayers(My.Settings.HighScoresFile)
VB doesn't like .High Scores File. This is probably something stupid whch i've done but i can't work out why that doesn't like it? Thanks again Dan
-
Thanks the man from U.N.C.L.E so are you saying that the following code would become name = New list(of system.collection.generic) ? This comes up saying do i want to change what i have written to system.collection.IEnumerable system.collection.IEnumerator system.collection.Generic.list(ofT) system.collection.queue system.collection.generic.IList(ofT) Many thanks Dan
Other way round:
New System.Collections.Generic.List(Of String)
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
Other way round:
New System.Collections.Generic.List(Of String)
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
Sorry, I just tried that, so the code is
names = New System.Collections.Generic.List(Of String)
but it comes up with an error as well as a blue squigly underneath saying it cannot be converted to '1 dimensional array of string?? I thank you for your patience Dan
-
Sorry, I just tried that, so the code is
names = New System.Collections.Generic.List(Of String)
but it comes up with an error as well as a blue squigly underneath saying it cannot be converted to '1 dimensional array of string?? I thank you for your patience Dan
No worries. Sounds like you have left the declaration of your variable as
Dim names as string()
. You need to change that to aList(Of
as well. E.g.Dim names = System.Collections.Generic.List(Of String)
names = New System.Collections.Generic.List(Of String)()Or
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
[My Articles] [My Website] -
No worries. Sounds like you have left the declaration of your variable as
Dim names as string()
. You need to change that to aList(Of
as well. E.g.Dim names = System.Collections.Generic.List(Of String)
names = New System.Collections.Generic.List(Of String)()Or
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
[My Articles] [My Website]Thanks a lot I have decided to show the code that i am using from start to fininsh. I am using 1 button called comtest 2 list boxes called listBox1 and ListBox2 2 textboxes called text1 and text2
Imports System.IO
Public Class Form1
Dim scores = New System.Collections.Generic.List(Of String)
Dim names = New System.Collections.Generic.List(Of String)
Dim test1(0 To 10) As String
Dim test2(0 To 10) As Integer
Dim userscore As Integer
Dim username As String
Dim a As Integer
Dim b As Integer
Dim i As Integer
Dim strFilename As String = ("High Scores.txt")Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call highscore() names = New System.Collections.Generic.List(Of String) scores = New System.Collections.Generic.List(Of Integer) Dim srScores As New IO.StreamReader(strFilename) While Not srScores.EndOfStream Dim strRead As String = srScores.ReadLine ListBox1.Items.Add(strRead) Dim strSplit() As String = Split(strRead, ",") names.Add(strSplit(0)) scores.Add(strSplit(1)) End While srScores.Close() End Sub
Private Sub comtest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comtest.Click
username = Text1.Text
userscore = Val(Text2.Text)
For i = 1 To 10
a = scores(i)
If userscore >= a Then
test1(i) = username
test2(i) = userscore
For b = i + 1 To 10
test1(b) = names(b - 1)
test2(b) = scores(b - 1)
Next
Call newscore()
Exit Sub
End If
test1(i) = names(i)
test2(i) = scores(i)
Next
End Sub
Private Sub highscore()
For i = 1 To 10
ListBox1.Items.Add(names(i))
List2.Items.Add(scores(i))
Next
End Sub
Private Sub newscore() ' write
ListBox1.ClearSelected()
List2.ClearSelected()
For i = 1 To 10
ListBox1.Items.Add(test1(i))
List2.Items.Add(test2(i))
names(i) = test1(i)
scores(i) = test2(i)
Next
Dim objWriter As New System.IO.StreamWriter(highScoresFile)For i = 1 To 10 objWriter.Write(names(i) & "," & scores(i)) Next objWriter.Close()
End Sub
This is the VB6 version with the same properties
Dim scores(1 To 10) As
-
This LOOKS pretty awsome :D I have one error and i don't actually know why.
lstPlayers = Player.GetAllPlayers(My.Settings.HighScoresFile)
VB doesn't like .High Scores File. This is probably something stupid whch i've done but i can't work out why that doesn't like it? Thanks again Dan
Hi there. Sorry it is my fault, I forgot to mention that where ever possible it is not a good idea to hardcode things like Filenames, Connectionstrings, etc into your app. The best way to do this is to use your Application.Settings file which is automatically generated for you by VS. To access the settings file, right click on your project in Solution Explorer, select properties and a form should open up with various tabs to set up options for your app. Select Settings tab and there you can store application specific settings such as file names and connection strings. To set up HighScoresFile, type the name HighScoresFile under name, under type select string, under scope select application, and under value put the full address of the file. To access any of your settings then from your app you call
My.Settings.'insert setting name here'
. When considering where to put the file, you need to take into account where you will deploy the application and where the High Scores file will actually be kept when the app has been deployed on other machines. Hope this helps.