I've created a Form that allows a user to enter two numbers, one in each textbox. There is an add/subtract/multiply/divide button. The user then presses Save to save the equation, and Display to display the entire equation in the listbox, which can store up to ten equations. I am to use a class for add/subtract and an inherent class for multiply/divide. I am having problems setting up the class. Class1 contains addition and subtraction.
So, a few questions. I've seen tons of examples where values are assigned to variables from within the class, but if the variables come from user input (first number, second number, and arithmetic operation) how do I make this work. I can't refer to textboxes in the main form from Class1 can I?
Similarly, how can I populate the listbox in the main form with the equations that I assume would come from the class? I know listbox.items.add(but what string am I providing here?) Any guidance in the right direction would be greatly appreciated. I know, noob stuff, but we've all got to start somewhere.
The instances of your class don't know anything about your TextBoxes, but the form knows about the instances of your class. The class would have properties and your form would set those properties based on the user input.
Properties are one of the basics of .NET programming. If you don't know how to work with properties then you should brush up on the basics by working through a beginners tutorial from start to finish. This is a good one:
This is what I have for the class. I know I'm on the right track, just unsure of what I'm missing.
Public Class MathOP
'Define fields
Private FirstNum As Single
Private SecondNum As Single
Private Result As Single
'Define Constructor
Public Sub New(ByVal FirstNum As Single,
ByVal SecondNum As Single,
ByVal Result As Single)
Me.FirstNum = FirstNum
Me.SecondNum = SecondNum
Me.Result = Result
End Sub
'Add method
Public Sub Add(ByVal Total As Single)
Me.Result = FirstNum + SecondNum
End Sub
'Subtract method
Public Sub Minus(ByVal Total As Single)
Me.Result = FirstNum - SecondNum
End Sub
'Property for first and second number
Public Property GetFirstNum() As Single
Get
Return Me.FirstNum
End Get
Set(value As Single)
End Set
End Property
Public Property GetSecondNum() As Single
Get
Return Me.SecondNum
End Get
Set(value As Single)
End Set
End Property
End Class
I started on the Main Form with the Add button, but I do know I'm missing something. Arguments? I'm just unsure of what goes into the argument if it is a user-defined value. I wanted to see what I was missing before finishing the rest of the buttons. Is this the right direction?
Also, the save button is supposed to save the current equation, and the display button adds it to a list box. Any help on this would be appreciated. Thank you for your responses by the way jmcilhinney. The link to the tutorial you offered was very helpful, but I'm still a little fuzzy on some things when actually translating what I learned into a real problem.
Public Class Form1
Dim total As Single
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
Dim AddMath As New MathOP()
AddMath.GetFirstNum = txtFirstNum.Text
AddMath.GetSecondNum = txtSecondNum.Text
lblResult.Text = total
End Sub
Private Sub btnSubtract_Click(sender As System.Object, e As System.EventArgs) Handles btnSubtract.Click
End Sub
Private Sub btnDivide_Click(sender As System.Object, e As System.EventArgs) Handles btnDivide.Click
End Sub
Private Sub btnMultiply_Click(sender As System.Object, e As System.EventArgs) Handles btnMultiply.Click
End Sub
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
End Sub
Private Sub btnDisplay_Click(sender As Object, e As System.EventArgs) Handles btnDisplay.Click
I started on the Main Form with the Add button, but I do know I'm missing something. Arguments? I'm just unsure of what goes into the argument if it is a user-defined value. I wanted to see what I was missing before finishing the rest of the buttons. Is this the right direction?
Not a bad effort for a first time but you do seem to have made a few mistakes based on what you are trying to do. Let's start with the MathOP class:-
1) You define 3 private variables and then create a Constructor to populate those 3 variables when a New instance of your class is declared. This is fine when you NEED to have a class declared with incoming values but in your case I would suggest that you do not need this constructor since you cannot get any results out of the class until you call additional methods later on in your code. Even if you do decide to leave it like this then you should NOT be expecting an incoming argument called Result since what you will be returning from the class, via your methods, will be the result of the various calculations.
2) So, considering point 1 above, and looking at your Public Sub Add routine. The first thing to remember is the difference between a Sub and a Function. That being that a Function can return a value to the routine that called it whereas a Sub Cannot. So another way to declare your Add routine is to declare it as a Function, which accepts two arguments being FirstNum and SecondNum and then returns a Value being the Result of the Functions work.
Now if you followed these two points you could now have a class that looks like this:-
VB.NET:
Public Class MathOP
'Add method
Public Function Add(ByVal FirstNum As Single, ByVal SecondNum As Single) As Single
Return FirstNum + SecondNum
End Function
End Class
You could then use this class in your Form like this:-
VB.NET:
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myMathClass As New MathOP
lblResult.Text = myMathClass.Add(CSng(txtFirstNum.Text), CSng(txtSecondNum.Text)).ToString
End Sub
Note: You should also always do Data Validation when obtaining information from the user interface by using TryParse of the Numeric Type that you are expecting, but I have skipped that part here since I do not know whether you have touched on that as yet?
3) Now that you have got that far you can then expand the class to add as many calculation methods as you need returning the correct result of the calculation to the calling routine.
Also, the save button is supposed to save the current equation, and the display button adds it to a list box.
One way to do this is to again expand your class to have a ReadOnly Property that will return the Equation that has just been used in any given calculation. So when you call the Add Function, or any other function, you can build the equation and save that equation to this property before you Return the result of your calculation. So, your class could now look like this:-
VB.NET:
Public Class MathOP
Private _EquationUsed As String
Public ReadOnly Property EquationUsed As String
Get
Return _EquationUsed
End Get
End Property
'Add method
Public Function Add(ByVal FirstNum As Single, ByVal SecondNum As Single) As Single
_EquationUsed = FirstNum.ToString & " + " & SecondNum.ToString & " = " & (FirstNum + SecondNum).ToString
Return FirstNum + SecondNum
End Function
End Class
And your Form1 may look like this:-
VB.NET:
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myMathClass As New MathOP
lblResult.Text = myMathClass.Add(CSng(txtFirstNum.Text), CSng(txtSecondNum.Text)).ToString
ListBox1.Items.Add(myMathClass.EquationUsed)
End Sub
Hope that helps.
Cheers,
Ian
BTW, Turn Option Strict On in your projects to help you avoid bad habits and also please enclose you Code in Code Tags when posting to the Forum.
You are awesome. The description you provided along with the code examples helped me to understand what the code is actually doing. Thank you for the advice: I turned Option Strict on and will use code tags in future posts.
The code works perfectly, but if I wanted items to be added to the list box when the Display button is pressed, and not when the operand is pressed, do I have to declare a new instance of the class in the Display buttons click event procedure? I'm having difficulty making this work.
Also, another question regarding string.split. When a user selects an item in the list box, the text boxes should be re-populated
with the chosen strings FirstNum and SecondNum values. Do you see what is wrong with this:
VB.NET:
'Declare variables
Dim strSelected As String = lstResultsBox.SelectedItem.ToString
Dim strSplitSelected() As String
'Use Split method to turn the selected result into an array and used the
'Array index to place the user selected number into the proper textbox.
strSplitSelected = strSelected.Split(" ")
txtFirstNumber.Text = strSplitSelected(0)
txtSecondNumber.Text = strSplitSelected(3)
lblResult.Text = String.Empty
The code works perfectly, but if I wanted items to be added to the list box when the Display button is pressed, and not when the operand is pressed, do I have to declare a new instance of the class in the Display buttons click event procedure? I'm having difficulty making this work.
Due to the way that I have demonstrated the code so far the answer to this would be "Yes". However, that answer would seem to duplicate the work that you would need to do so this is NOT the answer.
The next thing for you to learn then is the "Scope" of a variable. The scope of a variable means "Where can a declared variable be used". Here are some examples:-
1) A variable that is declared at the start of a Sub or Function can only be used within that Sub or Function and is NOT visible outside of the Sub or Function. Similarly, a variable declared within a For Loop, for example, within a Sub or Function can only be seen and used within that For loop within that Sub or Function.
2) A variable that is declared in a Class and outside of any Sub or Function within that Class can be seen and used by every Sub or Function within that Class. This is typically know as declaring a variable at the Class Level. This variable cannot however been seen by any other code in the project that is outside of that Class.
3) For a variable to be deemed to be a Truly Global variable that variable would have to be declared as Public in a separate Module within the project. This variable can then be seen by anything in the project.
Knowing this then, the simple answer is to declare your variable at the Class Level so that the Calculation can be done in one button and the assignment of the equation to the ListBox can then be done in another Button. In this case you would NOT declare another variable of the MathOP class in each Sub or Function but instead use the SAME variable declared at the Class Level in each Sub or Function. i.e:-
VB.NET:
Public Class Form1
Private myMathClass As New MathOP
Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Do Stuff
End Sub
Private Sub btnSubtract_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'Do Stuff
End Sub
'etc etc
End Class
Also, another question regarding string.split. When a user selects an item in the list box, the text boxes should be re-populated
with the chosen strings FirstNum and SecondNum values. Do you see what is wrong with this:
Firstly and for the future, If you have another question then please make a new post to keep things separate within the Forum.
However, there are two minor issues that I can see here being that the Split method expects a value of Type Char so you should be doing a conversion here and the txtSecondNumber TextBox is getting the wrong element of the array. Have a look here:-
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.