|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Can I get a little VB help?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
New to Overclock.net
|
Screwing arround here and I am at a loss why no matter what is entered in the text box it will end up being zero. In the default constructor I can set the "_side" variable to some thing other than zero and that value will be calculated and displayed just fine (_side = 5 will display 25 in the label)
This should be so simple...I don't understand Code:
Option Explicit On
Option Strict On
Public Class MainForm
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub xSideTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xSideTextBox.Enter
Me.xSideTextBox.SelectAll()
End Sub
Private Sub xSideTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles xSideTextBox.KeyPress
' accept only numbers, the period, and the Backspace
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> "." _
AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Private Sub xSideTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xSideTextBox.TextChanged
Me.xAreaLabel.Text = String.Empty
End Sub
Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
Dim mySquare As New Square
Dim area As New Square
Integer.TryParse(Me.xSideTextBox.Text, mySquare.Side)
Call area.CalculateArea()
Me.xAreaLabel.Text = Convert.ToString(area.Area)
Me.xSideTextBox.Focus()
End Sub
End Class
Code:
Option Explicit On
Option Strict On
Public Class Square
Private _area As Integer
Private _side As Integer
Public Property Side() As Integer
Get
Return _side
End Get
Set(ByVal value As Integer)
If value > 0 Then
_side = value
Else
_side = 0
End If
End Set
End Property
Public ReadOnly Property Area() As Integer
Get
Return _area
End Get
End Property
Public Sub New()
_side = 0
_area = 0
End Sub
Public Sub CalculateArea()
_area = _side * _side
End Sub
End Class
Any one have any ideas? (Oh yeah this is suppose to take a value for the side of square and find the area of it...but that part works...it is just the user input that is being a pain...which should be the easy part)
__________________
If it ain't broke...MAKE IT GO FASTER!!!
|
|||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||||
|
Intel Overclocker
|
I'm not a vb.net person but it doesn't look like you ever set _side in your square object, you seem to just call CalculateArea() which takes _side (which is equal to 0) and calculates _area (which of course will then be equal to 0)
__________________Edit: Try changing Public Sub CalculateArea() to: Public Sub CalculateArea(ByVal side as Integer) _area = side * sideEnd Sub and then when you call CalculateArea just pass the contents of the textbox like this Call area.CalculateArea(xSideTextBox.Text) Edit2: I noticed also that you have two square objects, mySquare and area. I'm not familiar with the vb.net API, what does Integer.TryParse do? I'm guessing TryParse takes (source,target) parameters... if that is the case then you need to do TryParse(xSideTextBox.Text, area.Side) instead.
Last edited by Ledward : 06-16-08 at 04:10 AM. |
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|