New Posts  All Forums:Forum Nav:

Can I get a little VB help?

post #1 of 2
Thread Starter 
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
and here is the 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
Even if I completely delete any thing that defaults the _side variable to zero it will still come up as zero.

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)
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
My System
(16 items)
 
  
CPUMotherboardGraphicsGraphics
2600K @ 4.7GHz Asus P8P67 B3 GTX 580 EVGA Hydro Copper 2 GTX 580 EVGA  
RAMHard DriveOptical DriveOS
4x4GB G.Skill Ripjaws bunch of 'em Blu-Ray For movies Windows 8.1 
MonitorMonitorKeyboardPower
24.4" Hans G HH251 X2 Yamakasi DS270  Blah 1000watt Super Flower 
CaseMouseMouse PadAudio
Built into Desk Microsoft SideWinder X8 Comfy one... Creative Extreme Gamer 
  hide details  
Reply
post #2 of 2
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 * side

End 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.
Edited by Ledward - 6/16/08 at 1:10am
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2Q Q6600 @ 3200MHz Gigabyte EP45-UD3P Sapphire HD4870 4GB OCZ Platinum PC2-8500 
Hard DriveOSMonitorKeyboard
OCZ Core SSD 128GB Windows Vista Samsung 275T + 245B + V2021m Logitech G15 
PowerCaseMouse
Coolermaster Real Power 620W Modular TT Soprano Logitech G9 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2Q Q6600 @ 3200MHz Gigabyte EP45-UD3P Sapphire HD4870 4GB OCZ Platinum PC2-8500 
Hard DriveOSMonitorKeyboard
OCZ Core SSD 128GB Windows Vista Samsung 275T + 245B + V2021m Logitech G15 
PowerCaseMouse
Coolermaster Real Power 620W Modular TT Soprano Logitech G9 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming