Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB6 code works like a treat but need it in vb.net grrr

VB6 code works like a treat but need it in vb.net grrr

Scheduled Pinned Locked Moved Visual Basic
csharphelp
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O offroaderdan

    Experts, I have some vb6 code that does exactly what i want it to do (yippee) however the program i am developing in is vb.net. Now orignally i thought that most of the code would be fairly easy to convert however some of it isn't being liked. This is the main code that i need help with

    open App.path & "\files\high scores.txt" for input as #1
    a=1
    While not EOF(1)
    Input #1, names(a), scores(a)
    a=a+1
    end while
    close #1

    vb.net doesn't like open app path and for input as #1as well as input #1 and close #1 If someone could help me that would be great. Many thanks Dan

    W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #3

    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.

    O 1 Reply Last reply
    0
    • O offroaderdan

      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
      loop

      objReader.close
      call highscores

      For 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

      D Offline
      D Offline
      David Skelly
      wrote on last edited by
      #4

      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.

      1 Reply Last reply
      0
      • W Wayne Gaylard

        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.

        O Offline
        O Offline
        offroaderdan
        wrote on last edited by
        #5

        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

        T W 2 Replies Last reply
        0
        • O offroaderdan

          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

          T Offline
          T Offline
          The Man from U N C L E
          wrote on last edited by
          #6

          For List(Of ) you need to import System.Collections.Generic. As for srSave, 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]

          O 1 Reply Last reply
          0
          • O offroaderdan

            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

            W Offline
            W Offline
            Wayne Gaylard
            wrote on last edited by
            #7

            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.

            O 1 Reply Last reply
            0
            • T The Man from U N C L E

              For List(Of ) you need to import System.Collections.Generic. As for srSave, 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]

              O Offline
              O Offline
              offroaderdan
              wrote on last edited by
              #8

              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

              T 1 Reply Last reply
              0
              • W Wayne Gaylard

                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.

                O Offline
                O Offline
                offroaderdan
                wrote on last edited by
                #9

                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

                W 1 Reply Last reply
                0
                • O offroaderdan

                  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

                  T Offline
                  T Offline
                  The Man from U N C L E
                  wrote on last edited by
                  #10

                  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]

                  O 1 Reply Last reply
                  0
                  • T The Man from U N C L E

                    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]

                    O Offline
                    O Offline
                    offroaderdan
                    wrote on last edited by
                    #11

                    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

                    T 1 Reply Last reply
                    0
                    • O offroaderdan

                      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

                      T Offline
                      T Offline
                      The Man from U N C L E
                      wrote on last edited by
                      #12

                      No worries. Sounds like you have left the declaration of your variable as Dim names as string(). You need to change that to a List(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]

                      O 1 Reply Last reply
                      0
                      • T The Man from U N C L E

                        No worries. Sounds like you have left the declaration of your variable as Dim names as string(). You need to change that to a List(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]

                        O Offline
                        O Offline
                        offroaderdan
                        wrote on last edited by
                        #13

                        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

                        1 Reply Last reply
                        0
                        • O offroaderdan

                          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

                          W Offline
                          W Offline
                          Wayne Gaylard
                          wrote on last edited by
                          #14

                          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.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups