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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Hello All; Newbie programmer and a problem straight away

  • 10-03-2009 12:08pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You're welcome.


  • Advertisement
Advertisement