Hi,

I am really, really new to VB .NET and cant get my head around loops, can someone suggest something to add to the end of my code.

Description of Program/Events.

I would like to come up with a number generator that takes the Total number (for this example I'll say 20), divides it by 2, and does alternating additions so the end result will generate a list like this:

1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20.

I can understand the arithmetic, just can't work out how to express this in code.

The end result would be save in a .Txt file (I have added a SaveFileDialog1 control, but figure I need to get the result before I work on the export)

Any pointers would be greatly appreciated.

Rick

Dim StartValue As Integer
        Dim EndValue As Integer
        Dim TotalValue As Integer

        
        ' Convert input to numeric variables.

        StartValue = Val(StartNumTextBox.Text)
        EndValue = Val(EndNumTextBox.Text)

        ' Set focus to field if you want  End If

        If Me.EndNumTextBox.Text.Trim = "" Then MessageBox.Show("Please Enter Numerals Only", "Non Numeric Character", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Me.EndNumTextBox.Focus()

        If Me.StartNumTextBox.Text.Trim = "" Then MessageBox.Show("Please Enter Numerals Only", "Non Numeric Character", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Me.StartNumTextBox.Focus()


        ' Calculate total number of pages.

        TotalValue = EndValue

        ' Display total pages in a message box for user to check.

        Dim TotalValueDisplay As Integer

        If StartValue <> 1 Then

            MessageBox.Show("The first page in a book SHOULD be 1")


        End If


        TotalValueDisplay = TotalValue.ToString("N")

        MessageBox.Show("Total Number of Pages = " & EndValue & vbNewLine & vbNewLine & "(Both Documents Merged)", "Total Combined Amount", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Dim HalfTotal = TotalValue / 2
        Dim FirstPage = StartValue
        Dim SecondPage As Integer

I would like to come up with a number generator that takes the Total number (for this example I'll say 20), divides it by 2, and does alternating additions so the end result will generate a list like this:

1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20.

I can understand the arithmetic, just can't work out how to express this in code.

I don't understand the arithmetic, but if I did, I would know how to express it in code ;) I got the first part (20 -> 10), but the rest of the sequence??? What if the Total number were, let's say, 12. What would the sequence be? Could you show it please?

But I can show few winks for your code already. First, use Integer classes TryParse method to validate input

Dim IntValue As Integer

' Use TryParse method to validate input as an integer
If Integer.TryParse(TextBox1.Text, IntValue) Then
  ' Now you have a valid integer (IntValue)
Else
  ' Tell the user to input an integer value. You may exit sub here
End If

Second. Since you convert an integer to a string, you don't need formatting

Dim TotalValue As Integer
Dim TotalValueDisplay As String

'TotalValueDisplay = TotalValue.ToString("N")
' Simply ToString
TotalValueDisplay = TotalValue.ToString

This was a bit irrelevant point, but what the heck :)

Hi Teme,

Thanks for your quick reply.

OK, if the total amount of pages were 12 (Start Page =1, End Page =12) then the sequence and format I would need is:

1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 6, 12

So this is 1 - 12 split in half (1 2 3 4 5 6 | 7 8 9 10 11 12)

1st Number is from left half
2nd Number is from right half.

And the tricky part is getting this into a .TXT file in this format:

1 7 2 8 3 9 4 10 5 11 6 12


I hope I explained it OK (my brain hurts :))

I hope I explained it OK (my brain hurts :))

You did, thanks.

There's a small problem with "odd" sequences, like 1, 2, 3, 4, 5. How that should be divided, (1 2 | 3 4 5) or (1 2 3 | 4 5)? I used the first one, which results 1, 3, 2, 4, 5. The latter would be 1, 4, 2, 5, 3. With an "even" sequence, there's no problems.

And here's the code

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim LastValue As Integer

  ' Use TryParse method to validate input as an integer
  If Integer.TryParse(TextBox1.Text, LastValue) Then
    ' Now you have a valid integer (LastValue)
    ' Check that > 0 (you need to check this?)
    If LastValue < 1 Then
      ' Tell the user to input an integer value > 0
      Exit Sub
    End If
  Else
    ' Tell the user to input an integer value
    Exit Sub
  End If

  ' Now calc the sequence, store sequence in an array. Array size is LastValue
  Dim Sequence(LastValue - 1) As Integer
  Dim i As Integer ' Array index
  Dim MidPoint As Integer ' Half of the sequence
  Dim SeqValue As Integer ' A value in the sequence
  Dim IsOdd As Boolean ' We need to know if we have an "odd" sequence (or do we?)

  ' Get the "middlepoint"
  MidPoint = LastValue \ 2

  ' Quick and dirty odd/even test
  If MidPoint * 2 = LastValue Then
    ' LastValue is even
    IsOdd = False
  Else
    ' LastValue is even
    IsOdd = True
  End If

  ' First, 1 to MidPoint
  i = 0 ' Indexing starts from zero
  SeqValue = 1 ' First sequence number ("low" numbers)
  Do Until SeqValue > MidPoint
    Sequence(i) = SeqValue
    SeqValue += 1 ' Next sequence number
    i += 2 ' Step two
  Loop
  ' Second, MidPoint to LastValue
  i = 1 ' Indexing starts from one because we already have "low" values
  SeqValue = MidPoint + 1 ' First sequence number ("high" numbers)
  Do Until SeqValue > LastValue
    Sequence(i) = SeqValue
    SeqValue += 1 ' Next sequence number
    i += 2 ' Step two
    If IsOdd AndAlso i > Sequence.GetUpperBound(0) Then
      ' Odd sequence and the indexing has to be fixed
      ' This is actually the last sequence number in an odd sequence
      i = Sequence.GetUpperBound(0)
    End If
  Loop

  Dim SequenceStr As String ' Array to string

  SequenceStr = ""
  For i = 0 To Sequence.GetUpperBound(0)
    SequenceStr = SequenceStr & Sequence(i)
    ' Add a space if not last sequence number
    If i < Sequence.GetUpperBound(0) Then
      SequenceStr = SequenceStr & " "
    End If
  Next i

  ' Save it
  Dim FileName As String ' Prompt from the user

  FileName = "C:\seq.txt" ' Debug

  TextBox2.Text = SequenceStr ' Debug

  My.Computer.FileSystem.WriteAllText(FileName, SequenceStr, False) ' Overwrite existing file

End Sub

I used a sub to test it, and added much comments.

If I got it wrong with the "odd" sequence, you may fix it with IsOdd variable (didn't exactly test it). That is, if I got this right in the first place :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.