Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Hello All; Newbie programmer and a problem straight away

Options
  • 10-03-2009 1:08pm
    #1
    Registered Users Posts: 1,428 ✭✭✭


    We've just started programming (VB) in college and I have an assignment due up.

    The program is created to allow us to input numbers as the co-ordinates of vertices of a triangle, then calculate the area of the triangle, the angles between the various sides and if a point lies outside or within the bounds of the triangle.

    My problem lies in the fact the program has to be as robust as possible and if I enter a character (eg ~) or a letter it crashes the programe. Here's what I've written so far:

    Sub CalculateCommonValues()
    ' This is the procedure to read in the x,y values of the vertices. I've called Vertice number one's co-ords ax and bx

    Dim ax, ay, bx, by, cx, cy, dx, dy As Double

    ' Reading in the first value and trying to prevent the programme crashing if it doesn't see a number. TxtBxX1.Text is the name I assigned to the textbox where the user enters the X1/ax co-ordinate

    ax = TxtBxX1.Text
    If TxtBxX1.Text = "" Then
    MessageBox.Show("Enter a value for X1")
    ElseIf TxtBxX1.Text Like ("[A-Z]") Or _
    TxtBxX1.Text Like ("[a-z]") Or _
    TxtBxX1.Text Like ("#") Then
    MessageBox.Show("Only enter NUMERICAL values")
    End If

    I realise for professional programmers this is so easy you've probably forgotten it years ago but for me its brand new and bloody hard. Any and all help would be greatly appreciated.

    Shane


Comments

  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    It's been a while since I used VB, but in general:

    - read in the user value as a string
    - check to see is it numeric (there will be a function for this)
    -- if the value is not numeric, display an error message
    -- if the value is numeric, "cast" it to an integer or float. This means convert the string to a number.


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Two things you should do first you should restrict the inputs allowed into your text box the below shows you a basic example.
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii >= 58 Or KeyAscii <= 47 And KeyAscii <> 8 And KeyAscii <> 46 Then
            KeyAscii = 0
        End If
    End Sub
    

    Then when the user clicks the button you should check again just in case. Once you have passed the check you can then convert the string value into the correct data type (double in the example) and work away with that. The below would be an example
    Private Sub Command1_Click()
    Dim sInput As String
    Dim dInput As Double
    
        sInput = Text1.Text
        
        If Not IsNumeric(sInput) Then
            Text1.Text = ""
            MsgBox "Only valid numeric values allowed", vbExclamation
            Exit Sub
        End If
        
        dInput = cdbl(sInput)
        'Do your magic 
    End Sub
    

    Hope that helps


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    You converting a double to a string. ax is declared as double but the text property of a textbox returns a string.


    Also look at the char.IsNumber function.


  • Registered Users Posts: 1,428 ✭✭✭quietsailor


    Well the assignment is gone in amid much panic so I just wanted to say thank you to everyone for helping me. The programme is a lot more robust now that ye explained a few things to me.

    Shane


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    You're welcome.


  • Advertisement
Advertisement