I'm also not impressed by the class you are taking either. I'm getting the impression they are sending you down the road without any kind of map.
A very important area you need to understand in object oriented programming is "scope". "Scope" is the visibility of members or variables from the point of perspective of the executing code.
The keywords Public, Private, Protected and Friend (possibly others - my
VB is rusty) modify the scope of class members. They can apply to every member (methods, fields/variables, properties, etc).
Variables defined in methods are always local scope - visible only within the context of a method.
When you create a shared member, the code of that member can not see ANY of the class' non-shared members without creating an instance of the class.
In your most recent code, you are trying to reference the textboxes that are defined as members of the class for the ASPX page. This doesn't work. The page controls are (by default) only visible from within the page class itself, as it should be. Also, you have changed the AddNumbers method signature to look like a button event handler. This is not necessary. Instead, you need to call the AddNumbers method from the button click handler, passing the values from the text boxes into it.
Based on what you have posted here I'll put together how I think this should be done. As I mentioned before, I usually try to avoid given explicit solutions to assignments, but I'd hate to see you spend more time on something that should be a trivial task. I just implore you to study it and understand how it works.
First we have the class' add method:
Code:
Public Class AddingMachine
...
Sub AddNumbers()
_FinalValue = _FirstNumber + _SecondNumber + _ThirdNumber
End Sub
End Class
See how the method uses the internal variables of the class?
Then we have the button:
Code:
<asp:button id="Button1" onclick="Button1_onClick" runat="server" text="Submit"></asp:button>
Next, the button handler which creates the class, assigns values and calls the method:
Code:
Sub Button1_onClick(sender As Object, e As EventArgs)
Dim MyNumbers As New AddingMachine()
MyNumbers.FirstNumber = CInt(txtFirst_Number.text)
MyNumbers.SecondNumber = CInt(txtSecond_Number.text)
MyNumbers.ThirdNumber = CInt(txtThird_Number.text)
MyNumbers.AddNumbers()
Response.Write ("Your total is: " & MyNumbers.FinalValue)
End Sub
Now, if you want to use a shared method the scenario is a bit different. You need to pass in everything you need and get back what you want because you don't have an instance of the class to work with. Here's what changes:
First the class method:
Code:
Public Class AddingMachine
...
Shared Function AddNumbers(firstNum As Int, secondNum as Int, thirdNum As Int) As Int
Dim MyNumbers As New AddingMachine()
MyNumbers.FirstNumber = firstNum
MyNumbers.SecondNumber = secondNum
MyNumbers.ThirdNumber = thirdNum
MyNumbers.FinalValue = MyNumbers.FirstNumber + MyNumbers.SecondNumber + MyNumbers.ThirdNumber
Return MyNumbers.FinalValue
End Function
End Class
Then the button click handler:
Code:
Sub Button1_onClick(sender As Object, e As EventArgs)
'using local vars for forum readability
'I'm intentionally using different var names to
'eliminate the confusion between the variables here
'and those in the AddNumbers method
Dim number1, number2, number3, answer As Int
number1 = CInt(txtFirst_Number.text)
number2 = CInt(txtSecond_Number.text)
number3 = CInt(txtThird_Number.text)
answer = MyNumbers.AddNumbers(number1, number2, number3)
'You need to call ToString() on the result or the string
'concatenation (&) will fail
Response.Write ("Your total is: " & answer.ToString())
End Sub
See the differences?
I would redo the add numbers method to greatly simplify it. The above is rather excessive:
Code:
Public Class AddingMachine
...
Shared Function AddNumbers(firstNum As Int, secondNum as Int, thirdNum As Int) As Int
Return firstNum + secondNum + thirdNum
End Function
End Class
Hopefully this will help you understand the different ways to implement class methods and get you on the right track.
-Peter
compiledthoughts.com