“Use The New Keyword To Create An Object Instance” - vb.net

I am not sure what is wrong with this part of the program. Every time I run it there is an error message saying:
Object reference not set to an instance of an object
On the line cc.RawDataString.IndexOf("B").
Code:
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim cc As New CreditCard
Dim posB, posC1, posC2, posBlackSlash As Integer
posB = cc.RawDataString.IndexOf("B")
posC1 = cc.RawDataString.IndexOf("^")
posC2 = cc.RawDataString.IndexOf("^", posC1 + 1)
posBlackSlash = cc.RawDataString.IndexOf("/")
cc.RawDataString = txtRawData.Text
lblCCNumber.Text = cc.Number
lblCCNumber.Text = cc.RawDataString.Substring(posB + 1, posC1 - posB - 1)
lblLname.Text = cc.RawDataString.Substring(posC1 + 1, posBlackSlash - (posC1 + 1))
lblFName.Text = cc.RawDataString.Substring(posBlackSlash + 1, posC2 - (posBlackSlash + 1))
lblYear.Text = cc.RawDataString.Substring(posC2 + 1, 2)
lblMonth.Text = cc.RawDataString.Substring(posC2 + 3, 2)
End Sub

It looks like you reference cc.RawDataString before you set it to something.

Related

Exception Thrown - Receive data from Arduino to Visual Basic

I'm trying to send serial data from Arduino and read it on Visual Basic. When I execute the code sometimes works and sometimes doesn't: throwing exception, System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Can you help me?
I'm new to Visual Basic, thanks.
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim TWSL, TWAL, THL, AoAL, WAL, PeL, RoilL, RyL, RydL As Integer
Dim TWS, TWA, TH, AoA, WA, Pe, Roil, Ry, Ryd, TWSResult, TWAResult, THResult, AoAResult, WAResult, PeResult, RoilResult, RyResult, RydResult As String
Dim StrSerialIn, StrSerialInRam As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToParent()
SerialPort1.PortName = "COM4"
SerialPort1.BaudRate = 9600
SerialPort1.Open()
Timer1.Start()
SerialPort1.Write(TrackBarAWA.Value & Chr(10))
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
StrSerialIn = SerialPort1.ReadExisting
Dim TB As New TextBox
TB.Multiline = True
TB.Text = StrSerialIn
If TB.Lines.Count > 0 Then
If TB.Lines(0) = "Failed to read" Then
Timer1.Stop()
SerialPort1.Close()
Return
End If
StrSerialInRam = TB.Lines(0).Substring(0, 2)
If StrSerialInRam = "A" Then
TWS = TB.Lines(0)
TWSL = TWS.Length
Else
TWS = TWS
End If
StrSerialInRam = TB.Lines(1).Substring(0, 3)
If StrSerialInRam = "B" Then
TWA = TB.Lines(1)
TWAL = TWA.Length
Else
TWA = TWA
End If
StrSerialInRam = TB.Lines(2).Substring(0, 3)
If StrSerialInRam = "C" Then
TH = TB.Lines(2)
THL = TH.Length
Else
TH = TH
End If
StrSerialInRam = TB.Lines(3).Substring(0, 2)
If StrSerialInRam = "D" Then
AoA = TB.Lines(3)
AoAL = AoA.Length
Else
AoA = AoA
End If
StrSerialInRam = TB.Lines(4).Substring(0, 1)
If StrSerialInRam = "E" Then
WA = TB.Lines(4)
WAL = WA.Length
Else
WA = WA
End If
StrSerialInRam = TB.Lines(5).Substring(0, 3)
If StrSerialInRam = "F" Then
Pe = TB.Lines(5)
PeL = Pe.Length
Else
Pe = Pe
End If
StrSerialInRam = TB.Lines(6).Substring(0, 3)
If StrSerialInRam = "G" Then
Roil = TB.Lines(6)
RoilL = Roil.Length
Else
Roil = Roil
End If
StrSerialInRam = TB.Lines(7).Substring(0, 3)
If StrSerialInRam = "H" Then
Ry = TB.Lines(7)
RyL = Ry.Length
Else
Ry = Ry
End If
StrSerialInRam = TB.Lines(8).Substring(0, 3)
If StrSerialInRam = "I" Then
Ryd = TB.Lines(8)
RydL = Ryd.Length
Else
Ryd = Ryd
End If
TWSResult = Mid(TWS, 2, TWSL)
TWAResult = Mid(TWA, 2, TWAL)
THResult = Mid(TH, 2, THL)
AoAResult = Mid(AoA, 2, AoAL)
WAResult = Mid(WA, 2, WAL)
PeResult = Mid(Pe, 2, PeL)
RoilResult = Mid(Roil, 2, RoilL)
RyResult = Mid(Ry, 2, RyL)
RydResult = Mid(Ryd, 2, RydL)
TWSvalue.Text = TWSResult
TWAvalue.Text = TWAResult
THvalue.Text = THResult
AoAvalue.Text = AoAResult
WAvalue.Text = WAResult
PeValue.Text = PeResult
RoilValue.Text = RoilResult
RyValue.Text = RyResult
RydValue.Text = RydResult
From what I gather, you are trying to get characters from specific places in specific lines on a text box. Based on the error message you included in your question, I assume the error occurs on a line of code containing the "String. Substring" method. If the string where you are getting the substring from is too short to cover the range you have specified in the substring method, you will get this error. For instance, if you are getting a substring that's 3 characters long from line 2 starting at character 0 and it has less than 3 characters, you will get this error.
See the documentation on the String.Substring method here

Graphing from a table VB

I am trying to graph from a table called totals(). The program itself is working okay, but i am having some issues. First, for each button press, it is adding the values and regraphing them. I do not want them to add to the previous values, i want them to overwrite the values.
Dim d1, d2, d3 As Single
Dim dieSumInt As Integer
Dim totals(17) as integer
For ptr = 1 To 10000
d1 = (Int((Rnd() * 6) + 1))
d2 = (Int((Rnd() * 6) + 1))
d3 = (Int((Rnd() * 6) + 1))
dieSumInt = CInt((d1 + d2 + d3))
totals(dieSumInt) += 1
Next
The whole code i have here, is supposed to take the sum of 3 dice and graph the totals into a picture box. I have all the graphing down, but each time i hit the "roll" button it keeps adding to the previous values. Im not sure how to clear out the values, to get a consistent value being graphed.
Dim totals(18) As Integer
Dim dashPen As New Pen(Color.Black, 1)
Public Function Max(ByVal arry() As Integer) As Integer
Max = arry(0)
For ptr As Integer = 0 To UBound(arry)
If arry(ptr) > Max Then Max = arry(ptr)
Next
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Randomize(DateAndTime.Timer)
End Sub
Private Sub rollButton_Click(sender As Object, e As EventArgs) Handles RollButton.Click
Dim d1, d2, d3 As Single
Dim dieSumInt As Integer
For ptr = 1 To 10000
d1 = (Int((Rnd() * 6) + 1))
d2 = (Int((Rnd() * 6) + 1))
d3 = (Int((Rnd() * 6) + 1))
dieSumInt = CInt((d1 + d2 + d3))
totals(dieSumInt) += 1
Next
Call Plot(totals)
Call LabelGraph(totals)
End Sub
Private Sub Plot(ByVal arry() As Integer)
Dim plotColor As Color
plotColor = Color.SkyBlue
Dim plotPen As New Pen(plotColor, 5)
Dim graph As Graphics = displayBox.CreateGraphics
Dim ymax, ptr As Integer
Dim xscale, yscale, x1, y1, Dwidth, Dheight As Single
Dim myfont As New Font("courier new", 5 + (displayBox.Width \ 100))
Dim percentArry(4) As Integer
Dwidth = displayBox.Width - 50
Dheight = displayBox.Height - 100
ymax = Max(arry)
displayBox.Refresh()
yscale = CSng(Dheight / ymax)
xscale = CSng(Dwidth / 17)
Dwidth = displayBox.Width - 50
Dheight = displayBox.Height - 70
For ptr = 0 To 4
percentArry(ptr) = CInt(arry.Max * (ptr + 1) * 0.2)
y1 = Dheight - CInt((arry.Max * (ptr + 1) / 5) * yscale)
graph.DrawLine(dashPen, displayBox.Left - 25, y1 + 4, displayBox.Right - 25, y1 + 4)
dashPen.DashStyle = Drawing2D.DashStyle.DashDotDot
graph.DrawString(CStr(percentArry(ptr)), myfont, Brushes.LightSlateGray, 'marking the sides
CSng(displayBox.Left - 10),
y1 + 3)
Next
For ptr = 3 To 18
x1 = (ptr - 2) * xscale
y1 = Dheight - (arry(ptr) * yscale)
graph.DrawRectangle(plotPen, x1 + 30, y1 + 4, xscale - 10, Dheight - y1) 'adjusting the size of the graph rectangles
Next
End Sub
Private Sub LabelGraph(ByVal arry() As Integer)
Dim graph As Graphics = displayBox.CreateGraphics
Dim myfont As New Font("courier new", 5 + (displayBox.Width \ 100))
For ptr As Integer = 1 To 16
graph.DrawString(CStr(ptr + 2), myfont, Brushes.LightSlateGray,
CSng(((displayBox.Width / 17) * ptr + 5) - (ptr * 3) + displayBox.Width / 35),
displayBox.Height - 60)
graph.DrawString(CStr(arry(ptr + 2)), myfont, Brushes.LightSlateGray,
CSng(((displayBox.Width / 17) * ptr) - (ptr * 3) + displayBox.Width / 28),
displayBox.Height - 30)
Next
End Sub
Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
Plot(totals)
LabelGraph(totals)
End Sub

shorther the code if a text equal with something

I'm a beginner programmer and I'm trying to create a game with Questions (Want to be a millionaire). Unfortunately, however, I've been stuck a bit, because what I want to do requires rewriting the code too often. Can I make this a little easier?
The first code is with Questions.
If LbNumber.Text increase +1, all code my increase +1
Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
Dim Rand As New Random() 'Pentru extragerea unui ` Număr ` aleatoriu)
Dim Number = Rand.Next(1, CountQListBox.Text) 'Exemplu, Un Număr intre (1,26)
LBNumber.Text = Number 'In Label-ul nostru denumit, LBNumber, va apărea Numarul extras.
If LBNumber.Text = ("1") Then 'Dacă Numărul extras va fi 1
QuestionBox.Text = QListBoxLoad.Items(1) 'In TextBox-ul denumit, QuestionBox va apărea primul Item din ListBox 1.
QListBoxLoad.SelectedItem = QListBoxLoad.Items(1) 'Vom selecta astfel primul Item, din ListBox 1.
ElseIf LBNumber.Text = ("2") Then
QuestionBox.Text = QListBoxLoad.Items(2)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(2)
ElseIf LBNumber.Text = ("3") Then
QuestionBox.Text = QListBoxLoad.Items(3)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(3)
ElseIf LBNumber.Text = ("4") Then
QuestionBox.Text = QListBoxLoad.Items(4)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(4)
ElseIf LBNumber.Text = ("5") Then
QuestionBox.Text = QListBoxLoad.Items(5)
QListBoxLoad.SelectedItem = QListBoxLoad.Items(5)
End If
End Sub
The 2nd with the answers.
If QListBox.Items increase +1, AnswerLoadFile.Lines increase +1. but the others not changed value.
Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
If QuestionBox.Text = QListBoxLoad.Items(1) Then
Dim a As String : a = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(1)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(2) Then
Dim a As String : a = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(2)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(3) Then
Dim a As String : a = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(3)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(4) Then
Dim a As String : a = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(4)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
ElseIf QuestionBox.Text = QListBoxLoad.Items(5) Then
Dim a As String : a = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(5)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
End If
try this:
Private Sub BttStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BttStart.Click
Dim Rand As New Random()
Dim Number = Rand.Next(1, CountQListBox.Text)
LBNumber.Text = Number
QListBoxLoad.SelectedItem = QListBoxLoad.Items(Number)
QuestionBox.Text = QListBoxLoad.Items(Number)
End Sub
and then this:
Private Sub QuestionBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuestionBox.TextChanged
Dim Number = QListBoxLoad.SelectedIndex
Dim a As String : a = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(a, 2))
Dim b As String : b = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(b, 3))
Dim c As String : c = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(c, 4))
Dim d As String : d = AnswerLoadFile.Lines(Number)
Debug.Print(indexThroughString(d, 5))
AnswerA.Text = (indexThroughString(a, 2))
AnswerB.Text = (indexThroughString(b, 3))
AnswerC.Text = (indexThroughString(c, 4))
AnswerD.Text = (indexThroughString(d, 5))
End Sub

VB.Net Drawing Binary Trees

Essentially, the purpose of this program is for revision. The program will generate a random mathematical expression, convert this into a visual representation of a binary tree and the user will have to traverse the binary tree. However, when I run this code, the initial node is far off centre. How would I go about re-positioning the binary tree to be in the middle of the PictureBox? Here is my code:
Public Class BTT
'VARAIBLES DECLARED CANNOT BE A FAULT
Dim nodes(7) As Object
'maybe try to alter the form so that the user can only get two incorrect answers'
Dim operators(6) As String
Dim actualAnswer As String = ""
Dim ogEquation(11) As String
Dim newLabel As String = "" 'used to store the equation to be stored in the label'
Dim userAnswer As String
Dim myTime As Double
Dim traversal(3) As String
Dim selectedTraversal As String
Dim treeCounter As Integer = 0
Dim draw As Boolean = False
Structure tree
Dim name As String
Dim left As Integer
Dim right As Integer
End Structure
Dim TreeNode(7) As tree
Dim scoreValue As Integer = 0 'stores the user's score for the game just completed'
Dim updating As Boolean = False 'if there are already 10 scores, the first one will need to be removed, so updating = true'
Class node
Public lineColour As Color
Public lineWidth As Integer
Public posX As Integer
Public posY As Integer
Public radius As Integer
Public Sub draw(e As PaintEventArgs)
Dim myPen As New Pen(Me.lineColour, Me.lineWidth)
e.Graphics.DrawEllipse(myPen, Me.posX, Me.posY, Me.radius, Me.radius)
End Sub
End Class
Sub DrawTree()
'these are the coordinates of the top left of the PictureBox
Dim leftX As Integer = 171
Dim rightX As Integer = 171 + PictureBox1.Width 'will be set to the edge of the picturebox
Dim topY As Integer = 138
Dim bottomY As Integer = 138 + PictureBox1.Height 'will be that number of pixels down, WILL NEVER CHANGE
Dim currentNode As Integer = 1 'will initially be the root node
For i = 1 To treeCounter 'loops based on the number of nodes in the array'
'assigns the basic information common to all of the nodes
nodes(i) = New node
nodes(i).radius = 70
nodes(i).lineWidth = 2
nodes(i).lineColour = Color.Black
Next
'need to go through the binary tree and determine x & y positions, with labels inside the ellipses
ConstructTree(currentNode, leftX, rightX, topY, bottomY)
draw = True
PictureBox1.Refresh()
End Sub
Sub ConstructTree(ByRef currentNode As Integer, ByRef leftX As Integer, ByRef rightX As Integer, ByRef topY As Integer, ByRef bottomY As Integer)
'ASK ISABEL ABOUT DYNAMICALLY GENERATING A LABEL'
'e.g. Dim test As New Label
nodes(currentNode).posX = (leftX + rightX) / 2 'gets average of x coordinates'
nodes(currentNode).posY = topY + ((bottomY - topY) * (1 / 3)) 'gets number of pixels down between bottom of form & last node, goes a third of the way down
If TreeNode(currentNode).left <> 0 Then 'if there is a node to the left
ConstructTree(TreeNode(currentNode).left, leftX, (leftX + rightX) / 2, nodes(currentNode).posY, bottomY)
End If
If TreeNode(currentNode).right <> 0 Then 'if there is a node to the right
ConstructTree(TreeNode(currentNode).right, (leftX + rightX) / 2, rightX, nodes(currentNode).posY, bottomY) 'swaps the left and right x-coords which have been changed
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If draw = True Then
For i = 1 To treeCounter
nodes(i).draw(e)
Next
'ALSO need to draw lines between the nodes, but IGNORE FOR NOW
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = myTime - (0.1)
myTime = TextBox1.Text
If myTime = 0 Then
Timer1.Enabled = False
MsgBox("Time is up!")
checkupdate()
resetForm()
End If
'add another if statement checking for two wrong answers, will stop the timer and tell the user that they have got too man questions wrong'
End Sub
Sub resetForm()
Score.Text = "Score:"
Label1.Text = ""
scoreValue = 0
End Sub
Sub writefile()
FileOpen(1, "BTTscores.txt", OpenMode.Output)
Select Case updating
Case True
For i = 2 To 11
WriteLine(1, scores(i))
Next
Case False
For i = 1 To numberOfScores + 1
WriteLine(1, scores(i))
Next
End Select
FileClose()
End Sub
Sub checkupdate()
'need to check whether there are already ten elements in the array. If so, then delete the first score, move all the indices of the other scores 1 to the left and add the new scores on the end'
numberOfScores = 0 'will need to be reset if the user carries on using the program'
FileOpen(1, "BTTscores.txt", OpenMode.Input) 'need to bubble sort values'
Dim line As String
Do Until EOF(1)
line = LineInput(1)
If line <> "" Then
numberOfScores = numberOfScores + 1
scores(numberOfScores) = line 'copies the line to the array'
End If
Loop
If numberOfScores = 10 Then 'if one needs to be updated, need to read all but the first line into the array'
updating = True
scores(11) = scoreValue
Else 'if there are less than 10 scores, the user's current score just needs to be added on the end'
updating = False
scores(numberOfScores + 1) = scoreValue
End If
FileClose(1)
writefile()
End Sub
Private Sub EnterButton_Click(sender As Object, e As EventArgs) Handles EnterButton.Click
userAnswer = Answer.Text
If actualAnswer.Replace(" ", "") = userAnswer.Replace(" ", "") Then
UpdateScore()
End If
Score.Text = ("Score: " & scoreValue)
Answer.Text = ""
InitialSetup()
End Sub
Sub UpdateScore()
Select Case difficulty
Case "Easy"
scoreValue = scoreValue + 10
Case "Medium"
scoreValue = scoreValue + 15
Case "Hard"
scoreValue = scoreValue + 20
End Select
End Sub
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
scoreValue = 0
Initialisation()
InitialSetup()
myTime = 60
Timer1.Enabled = True
End Sub
Sub InitialSetup()
Dim currentNode As Integer = 1 'will be root node'
actualAnswer = ""
GetEquation()
newLabel = ""
selectedTraversal = traversal(CInt(Math.Floor((3 - 1 + 1) * Rnd())) + 1) 'will choose a random traversal'
newLabel = "Traversal: " + selectedTraversal
Label1.Text = newLabel
If selectedTraversal = "Prefix" Then
PrefixConversion(currentNode)
ElseIf selectedTraversal = "Infix" Then
InfixConversion()
Else
RPConversion()
End If
DrawTree()
End Sub
Sub Initialisation()
operators(1) = "("
operators(2) = "-"
operators(3) = "+"
operators(4) = "*"
operators(5) = "/"
operators(6) = ")"
traversal(1) = "Prefix"
traversal(2) = "Infix"
traversal(3) = "Postfix"
End Sub
Sub GetEquation()
Select Case difficulty
'RANDOM NUMBER FORMAT: CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound'
Case "Easy"
'FORMAT: 17 * 4'
treeCounter = 3
ogEquation(1) = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
ogEquation(2) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(3) = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(2) 'operator is the root'
TreeNode(1).left = 2
TreeNode(1).right = 3
TreeNode(2).name = ogEquation(1)
TreeNode(3).name = ogEquation(3)
'EG: * 17 4
Case "Medium"
treeCounter = 5
'FORMAT: 15 * (17 + 4)'
ogEquation(1) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(2) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(3) = operators(1)
ogEquation(4) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(5) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(6) = CInt(Math.Floor((50 - 1 + 1) * Rnd())) + 1
ogEquation(7) = operators(6)
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(2) 'root node'
TreeNode(1).left = 2
TreeNode(1).right = 3
TreeNode(2).name = ogEquation(1)
TreeNode(3).name = ogEquation(5)
TreeNode(3).left = 4
TreeNode(3).right = 5
TreeNode(4).name = ogEquation(4)
TreeNode(5).name = ogEquation(6)
'EG: * 15 + 17 4
Case "Hard"
'FORMAT: (17 + 4) * (20 / 10), random numbers are 1-150'
treeCounter = 7
ogEquation(1) = operators(1)
ogEquation(2) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(3) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(4) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(5) = operators(6)
ogEquation(6) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(7) = operators(1)
ogEquation(8) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(9) = operators(CInt(Math.Floor((5 - 2 + 1) * Rnd())) + 2)
ogEquation(10) = CInt(Math.Floor((150 - 1 + 1) * Rnd())) + 1
ogEquation(11) = operators(6)
'initialising the binary tree iteration'
TreeNode(1).name = ogEquation(6) 'root node'
TreeNode(1).left = 2
TreeNode(1).right = 5
TreeNode(2).name = ogEquation(3)
TreeNode(2).left = 3
TreeNode(2).right = 4
TreeNode(3).name = ogEquation(2)
TreeNode(4).name = ogEquation(4)
TreeNode(5).name = ogEquation(9)
TreeNode(5).left = 6
TreeNode(5).right = 7
TreeNode(6).name = ogEquation(8)
TreeNode(7).name = ogEquation(10)
'EG: * + 17 4 / 20 10
End Select
End Sub
'Traversal Solutions'
'Postfix Conversion'
Sub RPConversion()
Dim myStack As New Stack(15)
Dim empty As Boolean = True
Dim temp As String 'used to store the current part of the original equation'
Dim operatorNum As Integer
Dim peekNum As Integer
Dim stoploop As Boolean = True
For i = 1 To ogEquation.Count - 1 'will iterate through the total number of elements in the array ogEquation'
If myStack.Count = 0 Then empty = True
temp = ogEquation(i)
MatchTempOperation(myStack, temp, operatorNum)
If operatorNum > 1 And operatorNum < 6 Then 'if the value is an operator'
If myStack.Count <> 0 Then 'if the stack contains a value'
CheckPeek(myStack, peekNum)
If operatorNum > peekNum Then
myStack.Push(temp)
ElseIf operatorNum = peekNum Then
actualAnswer = actualAnswer + myStack.Pop()
myStack.Push(temp)
Else 'operatorNum < peekNum'
actualAnswer = actualAnswer + myStack.Pop()
Do
stoploop = True
CheckPeek(myStack, peekNum)
If operatorNum > peekNum Then
myStack.Push(temp)
ElseIf operatorNum = peekNum Then
actualAnswer = actualAnswer + myStack.Pop()
myStack.Push(temp)
Else
actualAnswer = actualAnswer + myStack.Pop()
stoploop = False
End If
Loop Until stoploop Or myStack.Count = 0
End If
Else
myStack.Push(temp)
End If
ElseIf temp = "(" Then
myStack.Push(temp)
ElseIf temp = ")" Then
Do
actualAnswer = actualAnswer + myStack.Pop()
Loop Until myStack.Peek() = "("
myStack.Pop()
Else
actualAnswer = actualAnswer + temp
End If
operatorNum = 0
Next
If myStack.Count > 0 Then
For i = 1 To myStack.Count
actualAnswer = actualAnswer + myStack.Pop()
Next
End If
End Sub
Sub CheckPeek(ByVal myStack As Stack, ByRef peekNum As Integer) 'does the same as MatchTempOperation but for the top of the stack'
For i = 2 To 5 'skip one and six because we know it isn't a left or right bracket'
If myStack.Peek() = operators(i) Then
peekNum = i
End If
Next
End Sub
Sub MatchTempOperation(ByVal myStack As Stack, ByVal temp As String, ByRef operatorNum As Integer) 'wants to look at the stack but not be able to change it'
For i = 1 To 6
If temp = operators(i) Then
operatorNum = i
End If
Next
End Sub
'Infix'
Sub InfixConversion()
For i = 1 To 11
'check each element for empty spaces / brackets'
If ogEquation(i) <> "" And ogEquation(i) <> "(" And ogEquation(i) <> ")" Then
actualAnswer = actualAnswer + ogEquation(i)
End If
Next
End Sub
'Prefix'
Sub PrefixConversion(ByRef currentNode As Integer)
actualAnswer = actualAnswer + TreeNode(currentNode).name
If TreeNode(currentNode).left <> 0 Then
PrefixConversion(TreeNode(currentNode).left)
End If
If TreeNode(currentNode).right <> 0 Then
PrefixConversion(TreeNode(currentNode).right)
End If
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Hide()
End Sub
End Class
Apologies for it's inefficiency, please also note that the "difficulty" variable is Public and stored outside of this form. Thanks :)
OUTPUT:
enter image description here
As you can see, the root node is far off centre in the bottom left.

Stuggling to get arrows keys to work when passing through functions

So I'm trying to make a game for our final project (high school) and we've recently swaped over to the new Visual Basic 2010 from VB6. But my question today is, that whever I try to use the KeyDown event and call it from another procedure
KeyValue = _5x5_Board_KeyDown(KeyValue)
It is flagging it up as it doesn't contain the "sender" or "e" that is used for the event to activate. And if I enter those into the call for the program, It says they're not defined. But as far as I'm aware, they are system defined.
Public Class _5x5_Board
Dim xpos As Integer
Dim ypos As Integer
Dim Tile(6, 6) As PictureBox
Dim KeyValue As Integer
Private Sub _5x5_Board_Load(ByVal sender As System.Object)
'Test start position (center of the board)
xpos = 3
ypos = 3
'Assisgning all of the picture boxes to the array
'You can ignore this part...
Tile(0, 1) = Tile0_1
Tile(0, 2) = Tile0_2
Tile(0, 3) = Tile0_3
Tile(0, 4) = Tile0_4
Tile(0, 5) = Tile0_5
Tile(1, 0) = Tile1_0
Tile(1, 1) = Tile1_1
Tile(1, 2) = Tile1_2
Tile(1, 3) = Tile1_3
Tile(1, 4) = Tile1_4
Tile(1, 5) = Tile1_5
Tile(1, 6) = Tile1_6
Tile(2, 0) = Tile2_0
Tile(2, 1) = Tile2_1
Tile(2, 2) = Tile2_2
Tile(2, 3) = Tile2_3
Tile(2, 4) = Tile2_4
Tile(2, 5) = Tile2_5
Tile(2, 6) = Tile2_6
Tile(3, 0) = Tile3_0
Tile(3, 1) = Tile3_1
Tile(3, 2) = Tile3_2
Tile(3, 3) = Tile3_3
Tile(3, 4) = Tile3_4
Tile(3, 5) = Tile3_5
Tile(3, 6) = Tile3_6
Tile(4, 0) = Tile4_0
Tile(4, 1) = Tile4_1
Tile(4, 2) = Tile4_2
Tile(4, 3) = Tile4_3
Tile(4, 4) = Tile4_4
Tile(4, 5) = Tile4_5
Tile(4, 6) = Tile4_6
Tile(5, 0) = Tile5_0
Tile(5, 1) = Tile5_1
Tile(5, 2) = Tile5_2
Tile(5, 3) = Tile5_3
Tile(5, 4) = Tile5_4
Tile(5, 5) = Tile5_5
Tile(5, 6) = Tile5_6
Tile(6, 1) = Tile6_1
Tile(6, 2) = Tile6_2
Tile(6, 3) = Tile6_3
Tile(6, 4) = Tile6_4
Tile(6, 5) = Tile6_5
Call GamePlay(xpos, ypos, KeyValue)
End Sub
Private Sub GamePlay(ByRef xpos As Integer, ByRef ypos As Integer, ByRef KeyValue As Integer)
Do
'Sends for answer of the KeyDown event.
'This is the problem bellow
KeyValue = _5x5_Board_KeyDown(KeyValue)
'Decides the position the tile should move to pased on numeric order
Select Case KeyValue
'Up
Case Is = 1
xpos = xpos - 1
'Right
Case Is = 2
ypos = ypos + 1
'Down
Case Is = 3
xpos = xpos + 1
'Left
Case Is = 4
ypos = ypos - 1
End Select
'Colours in the new square on the board of the designated tile
Tile(xpos, ypos).Load("H:\Computing\Project\TileJump\TileJump\TileJump\Resources\Select1.bmp")
Loop Until Keys.Escape = True
End Sub
Private Function _5x5_Board_KeyDown(ByRef KeyValue As Integer, ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
'This is to declare the key pressed to a numeric value that is used to declare the new position
If e.KeyCode = Keys.Up Then
KeyValue = 1
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Right Then
KeyValue = 2
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Down Then
KeyValue = 3
'MsgBox("KeyValue is " & KeyValue)
ElseIf e.KeyCode = Keys.Left Then
KeyValue = 4
'MsgBox("KeyValue is " & KeyValue)
End If
Return KeyValue
End Function
End Class
The above is my code so far. I hope it helps. It may sound really stupid and bad of me, but i've been stuck on this for almost a month
The KeyDown function accepts three parameters : keyvalue (which you have), sender as object (type of object), e as eventargs (which key you pressed)
However, when you call the keydown function, you are only passing the first parameter: keyValue
Instead Call the GamePlay() from within the keydown function, instead of the other way around, this way it will return with an integer instead of getting complicated with the sender and eventargs