r/visualbasic • • Nov 27 '18

Decomposing and reassembling Integers (1000 to 1025) Visual Basic

This is my code for a program that takes integers (1000 to 1025) decomposes them, and reassembling them, I imagined 6 listboxs, One for the list of 1000 to 1025, four for the individual digits, and the final one for the reasembled integers. The code works using a textbox and a label to seperate the digits individualy, but how can I transfer it so it works with all the listboxs. As you will see, some of the sections of my code is already planed out for the listbox code, but i cant seem to get it all working. Any well or tips would be great. BTW, my code was in french, and i translated into english to if theres any gramatical mistake, my bad.

Kinda like this: [ ] = Listbox

[1234] [1] [2] [3] [4] [1234]

[1311] [1] [3] [1] [1] [1311]

Public Class frmSepRea

'****************************************************************

Dim NombreInscrit As Integer

Dim Units As Integer

Dim Tens As Integer

Dim Hundreds As Integer

Dim Thousand As Integer

'*****************************************************************

Private Sub btnSepRea_Click(sender As Object, e As EventArgs) Handles btnSepRea.Click

'*************************************************************

'Makes sur that everytime the button is pressed it dosent re-insert the list of
'numbers (1000 to 1025)

While lstInitial.Items.Count > 0

lstInitial.Items.RemoveAt(lstInitial.Items.Count - 1)

End While

'************************************************************

'Ajoute des valeurs a la ListBox Initial.

For i As Integer = 1000 + 1 To 1025

lstInitial.Items.Add(i)

Next

****************************************************

'Decomposition of numbers in the thousands (Working indivudually in a textbox,
'need it in a listbox)

'*************************************************************

NumberInserted = Val(txtNumber.Text)

NumberInserted = Math.Abs(Val(txtNumber.Text))

If (NumberInserted < 10) Then

Me.lblDigit.Text = "The first digit is : " & NumberInserted

ElseIf (NumberInserted < 100) Then

Call SeperateTwoDigits(NumberInserted , Tens, Units)

Me.lblDigit.Text = "The first digit is : " & Dizaines & vbCrLf &

"The first digit is :" & Units

ElseIf (NumberInserted < 1000) Then

Call SeperateThreeDigits(NumberInserted , Hundreds, Tens, Units)

Me.lblDigit.Text = "The first digit is : " & Hundred & vbCrLf &

"The Second number is : " & Tens & vbCrLf &

"The third number is : " & Units

ElseIf (NumberInserted > 1000) And (NumberInserted < 9999) Then

Call SeperateFourDigit(NumberInserted , Thousands, Hundreds, Tens, Units)

Me.lblDigit.Text = "The first number is :" & Thousands & vbCrLf &

"The second number is : " & Hundreds & vbCrLf &

"The third number is : " & Tens & vbCrLf &

"The fourth number is : " & Units

Else

'Limites all other numbers

MessageBox.Show("Nombre non valide")

End If

End Sub

'************************************************************

'Seperate two digits Ex, 10 = 1, 0

'*************************************************************

Sub SeperateTwoDigit(ByVal number As Integer, ByRef Digit1 As Integer, ByRef Digit2 As Integer)

Digit1 = number \ 10

Digit2 = number Mod 10

End Sub

'************************************************************

'Seperate three digits Ex, 123 = 1, 2, 3

'*************************************************************

Sub SeperateThreeDigit(ByVal number As Integer, ByRef Digit1 As Integer, ByRef Digit2 As Integer, ByRef Digit3 As Integer)

Digit1 = number \ 100

number = number Mod 100

Call SeperateTwoDigit(number, Digit2, Digit3)

End Sub

'************************************************************

'Seperate Four digits Ex, 1234 = 1, 2, 3, 4

'*************************************************************

Sub SeperteFourDigit(ByVal number As Integer, ByRef Digit1 As Integer, ByRef Digit2 As Integer, ByRef Digit3 As Integer, ByRef Digit4 As Integer)

Digit1 = number \ 1000

number = number Mod 1000

Digit2 = number \ 100

number = number Mod 100

Digit3 = number \ 10

number = number Mod 10

Digit4 = number

End Sub

Sub ForLoop(ByVal Chiffre1, ByVal Chiffre2, ByVal Chiffre3, ByVal Chiffre4)

'Gonna need a For Loop that is able to use these equasion for each individual
'part

'*****All bellow is theoretical and dont work yet*****

For Each Item In lstInitial.Items

'Do stuff

'Possibiliy ---->

'lstDigit1.Text = Chiffre1

'lstDigit2.Text = Chiffre2

'lstDigit3.Text = Chiffre3

'lstDigit4.Text = Chiffre4

Next

'Or

For i = 0 To lstInitial.Items.Count - 1

'Do stuff

'Need a sepration after for four columbs

'MsgBox(lstInitial.Items(i).ToString) (*example*)

Next

End Sub

Sub Reassembler()

'Reassembling the integers

For Each Item In lstInitial.Items

'Do stuff

lstFinal.Text = Val(lst1digit.Text) & Val(lstDigit2.Text) & Val(lstDigit3.Text) & Val(lstDigit4.Text)

Next

'Or (*Other way*)

For i = 0 To lstInitial.Items.Count - 1

'Do stuff

Next

End Sub

End Class

5 Upvotes

2 comments sorted by

2

u/chrwei Nov 27 '18

four spaces in front makes for a code block. format properly please.

it seems to me you're doing this the hard way. just case the int to a string and pull out the digits.

1

u/Jayden933 Nov 27 '18

I'm still pretty new to VB, but it seems like Regex may work better for this application