Jump to content

Oh wow its me again... 

This time looking for some help in VB.

Just not sure how to have the code read every other line in vb.

I'm not allowed to modify the .txt file so I would do that but can't do that in this case. 

 

Here is what I have so far. I will also be having an array in here, so not sure if that will affect anything 

The file has ID's and then the next line after that is the salary. The list box needs to only contain the salary but the array will have both of them. 

 

I'm sure I could do this by my self but actually not sure what to start next. Should I start the array then get the values from array to display in the list?

 

Private Sub frmMain_load(sender As Object, e As EventArgs) Handles Me.Load
        Dim File As IO.StreamReader
        Dim IDS As String

        If IO.File.Exists("Employees.txt") Then
            File = IO.File.OpenText("Employees.txt")
            Do Until File.Peek = -1
                IDS = File.ReadLine
                lstIds.Items.Add(IDS)
            Loop
            File.Close()
        End If
    End Sub

 

Link to comment
https://linustechtips.com/topic/749557-read-every-other-line-visual-basic/
Share on other sites

Link to post
Share on other sites

15 minutes ago, CookieMaster said:

 - SNIP -

Private Sub frmMain_load(sender As Object, e As EventArgs) Handles Me.Load
	Dim File As IO.StreamReader
	Dim IDS As String
	Dim Alternator As Boolean

	If IO.File.Exists("Employees.txt") Then
		File = IO.File.OpenText("Employees.txt")
		Alternator = false
		Do Until File.Peek = -1
			IDS = File.ReadLine
			If Alternator Then
				lstIds.Items.Add(IDS)
			End If
			Alternator = !Alternator
		Loop
		File.Close()
	End If
End Sub

First of all, I haven't used VB in a few years, so please excuse any syntax errors. I don't know how you plan to size the array before you know how many lines are in the file, but if I understand what you're looking for with the listbox (only add the salary lines to the listbox), then this code should do what you want.

Link to post
Share on other sites

10 minutes ago, JFischer00 said:

Private Sub frmMain_load(sender As Object, e As EventArgs) Handles Me.Load
	Dim File As IO.StreamReader
	Dim IDS As String
	Dim Alternator As Boolean

	If IO.File.Exists("Employees.txt") Then
		File = IO.File.OpenText("Employees.txt")
		Alternator = false
		Do Until File.Peek = -1
			IDS = File.ReadLine
			If Alternator Then
				lstIds.Items.Add(IDS)
			End If
			Alternator = !Alternator
		Loop
		File.Close()
	End If
End Sub

First of all, I haven't used VB in a few years, so please excuse any syntax errors. I don't know how you plan to size the array before you know how many lines are in the file, but if I understand what you're looking for with the listbox (only add the salary lines to the listbox), then this code should do what you want.

why don't you just SQL this 

Link to post
Share on other sites

16 minutes ago, JFischer00 said:

Private Sub frmMain_load(sender As Object, e As EventArgs) Handles Me.Load
	Dim File As IO.StreamReader
	Dim IDS As String
	Dim Alternator As Boolean

	If IO.File.Exists("Employees.txt") Then
		File = IO.File.OpenText("Employees.txt")
		Alternator = false
		Do Until File.Peek = -1
			IDS = File.ReadLine
			If Alternator Then
				lstIds.Items.Add(IDS)
			End If
			Alternator = !Alternator
		Loop
		File.Close()
	End If
End Sub

First of all, I haven't used VB in a few years, so please excuse any syntax errors. I don't know how you plan to size the array before you know how many lines are in the file, but if I understand what you're looking for with the listbox (only add the salary lines to the listbox), then this code should do what you want.

All good but cant have a ! unless its a with statement. (so not working). Maybe I should get it into an array then load them into the lst later, the array is only 10  

Link to post
Share on other sites

So I found a solution after finding a guide, but I just don't understand how it displayed the employee Id only and not salary as well. Maybe someone can let me know how? This one really has me confused.

Dim Reader As IO.StreamReader
        Dim IDS As String
        Dim Subs As Integer = 0
        If IO.File.Exists(File) Then
            Reader = IO.File.OpenText(File)
            Do Until Reader.Peek = -1
                IDS = Reader.ReadLine
                lstIds.Items.Add(IDS)
                Emp(Subs).ID = IDS
                Emp(Subs).Salary = Convert.ToDouble(Reader.ReadLine)
                Subs += 1
            Loop
            Reader.Close()
            lstIds.SelectedIndex = 0
        End If

 

Link to post
Share on other sites

11 hours ago, CookieMaster said:

All good but cant have a ! unless its a with statement. (so not working). Maybe I should get it into an array then load them into the lst later, the array is only 10  

Sorry, I had to sleep. Anyway, I looked it up and the equivalent of "!Alternator" in VB is "Not(Alternator)".

9 hours ago, CookieMaster said:

 - SNIP -

This code reads a line (the ID), adds the ID to the listbox (which I guess is what you meant to say instead of salary in your original question), adds the ID to the array, then adds the salary to the array by reading the next line. This code is fine for what you want.

Link to post
Share on other sites

6 hours ago, JFischer00 said:

Sorry, I had to sleep. Anyway, I looked it up and the equivalent of "!Alternator" in VB is "Not(Alternator)".

This code reads a line (the ID), adds the ID to the listbox (which I guess is what you meant to say instead of salary in your original question), adds the ID to the array, then adds the salary to the array by reading the next line. This code is fine for what you want.

Thanks, and do you know what line or part makes it read just those certain lines? And yes I'm sorry I did mean IDs not salary before. Is it the +=1 part that makes it skip a line? And I know this could have been done easier but it was required for hw that it was done a certain way, make a structure and array. 

Link to post
Share on other sites

8 minutes ago, CookieMaster said:

Thanks, and do you know what line or part makes it read just those certain lines? And yes I'm sorry I did mean IDs not salary before. Is it the +=1 part that makes it skip a line? And I know this could have been done easier but it was required for hw that it was done a certain way, make a structure and array. 

If you look at the code, there are two lines that say "Reader.ReadLine" so it's not skipping a line, it's just only putting every other line into the listbox. It reads two lines every iteration. The first line gets put into the array and the listbox. The second line only gets put into the array.

Link to post
Share on other sites

11 minutes ago, JFischer00 said:

If you look at the code, there are two lines that say "Reader.ReadLine" so it's not skipping a line, it's just only putting every other line into the listbox. It reads two lines every iteration. The first line gets put into the array and the listbox. The second line only gets put into the array.

Thanks, so because I have it going into the list box it makes is skip a line? What if I just wanted it to go straight to the array, would I have reader.read line once or twice? I'm sorry for being confused, the textbook does not explain this really and I've searched everywhere 

Link to post
Share on other sites

1 hour ago, bcguru9384 said:

try watching the qbasic vids by schoolfreeware

it covers what i think you want and more

yes it is basic code but it still works to this day

the vids are on youtube

hope it helps

Thanks 

 

1 hour ago, JFischer00 said:

If you look at the code, there are two lines that say "Reader.ReadLine" so it's not skipping a line, it's just only putting every other line into the listbox. It reads two lines every iteration. The first line gets put into the array and the listbox. The second line only gets put into the array.

Double post, ignore my last post I believe I get it and if I wanted to remove the lst I would just remove that one line. I'm gonna guess that having the reader.read line for IDs before salary made it go "skip" a line because it was going back and forth between ID and salary but only showing ID

 

Thanks again, I went from doing C++ to VB in that same night so yeah. 

Edited by CookieMaster
Add even more
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×