Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

need error catch for small program i wrote

interpiminterpim Member Posts: 8
OK... simple little program here

takes 10 inputs and multiplies the users 10 inputs by 10 constants stored in an array.

Then each of these 10 inputs is multiplied by its constant and displayed, and then a sum of all those are displayed also.

My error occurs when there is no number in one of the input boxes. I have the program to default to 0, but if the user deletes the 0 then it is possible to create this error... any idea on how to prevent the user from not inputting a number, or if the input is blank that the program automattically measures it as a 0 value?

Comments

  • iwilld0itiwilld0it Member Posts: 1,134
    : OK... simple little program here
    :
    : takes 10 inputs and multiplies the users 10 inputs by 10 constants stored in an array.
    :
    : Then each of these 10 inputs is multiplied by its constant and displayed, and then a sum of all those are displayed also.
    :
    : My error occurs when there is no number in one of the input boxes. I have the program to default to 0, but if the user deletes the 0 then it is possible to create this error... any idea on how to prevent the user from not inputting a number, or if the input is blank that the program automattically measures it as a 0 value?
    :

    I dont know how you wrote the code but maybe this idea will help ...

    If the inputs are coming from a textbox ...

    [code]
    Dim total As Double
    Dim value As String

    value = someTextBox.Text.Trim

    If IsNumeric(value) Then
    total += CDbl(value)
    End If
    [/code]

    I'm sure you can incorpate this logic into your code some how.
  • interpiminterpim Member Posts: 8
    : : OK... simple little program here
    : :
    : : takes 10 inputs and multiplies the users 10 inputs by 10 constants stored in an array.
    : :
    : : Then each of these 10 inputs is multiplied by its constant and displayed, and then a sum of all those are displayed also.
    : :
    : : My error occurs when there is no number in one of the input boxes. I have the program to default to 0, but if the user deletes the 0 then it is possible to create this error... any idea on how to prevent the user from not inputting a number, or if the input is blank that the program automattically measures it as a 0 value?
    : :
    :
    : I dont know how you wrote the code but maybe this idea will help ...
    :
    : If the inputs are coming from a textbox ...
    :
    : [code]
    : Dim total As Double
    : Dim value As String
    :
    : value = someTextBox.Text.Trim
    :
    : If IsNumeric(value) Then
    : total += CDbl(value)
    : End If
    : [/code]
    :
    : I'm sure you can incorpate this logic into your code some how.
    :



    OK... here is my Code, please before you bash my code consider that I'm teaching myself VB, and have no formal training. Thanks. Just curious as to where to put that code in here.
    [code]
    Dim materials() As Decimal = {0.0008, 0.0138, 0.0552, 0.1656, 0.4968, 0.9936, 1.4904, 2.2356, 3.3534, 5.0301}
    Dim mat1 As Decimal
    Dim mat2 As Decimal
    Dim mat3 As Decimal
    Dim mat4 As Decimal
    Dim mat5 As Decimal
    Dim mat6 As Decimal
    Dim mat7 As Decimal
    Dim mat8 As Decimal
    Dim mat9 As Decimal
    Dim mat10 As Decimal
    Dim total As Decimal

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mat1 = TextBox1.Text * materials(0)
    Dim mat2 = TextBox2.Text * materials(1)
    Dim mat3 = TextBox4.Text * materials(2)
    Dim mat4 = TextBox3.Text * materials(3)
    Dim mat5 = TextBox8.Text * materials(4)
    Dim mat6 = TextBox7.Text * materials(5)
    Dim mat7 = TextBox6.Text * materials(6)
    Dim mat8 = TextBox5.Text * materials(7)
    Dim mat9 = TextBox10.Text * materials(8)
    Dim mat10 = TextBox9.Text * materials(9)

    Label20.Text = mat1
    Label19.Text = mat2
    Label18.Text = mat3
    Label17.Text = mat4
    Label16.Text = mat5
    Label15.Text = mat6
    Label14.Text = mat7
    Label13.Text = mat8
    Label12.Text = mat9
    Label11.Text = mat10
    Label25.Text = (mat1 + mat2 + mat3 + mat4 + mat5 + mat6 + mat7 + mat8 + mat9 + mat10)
    End Sub

    [/code]
  • iwilld0itiwilld0it Member Posts: 1,134
    : : : OK... simple little program here
    : : :
    : : : takes 10 inputs and multiplies the users 10 inputs by 10 constants stored in an array.
    : : :
    : : : Then each of these 10 inputs is multiplied by its constant and displayed, and then a sum of all those are displayed also.
    : : :
    : : : My error occurs when there is no number in one of the input boxes. I have the program to default to 0, but if the user deletes the 0 then it is possible to create this error... any idea on how to prevent the user from not inputting a number, or if the input is blank that the program automattically measures it as a 0 value?
    : : :
    : :
    : : I dont know how you wrote the code but maybe this idea will help ...
    : :
    : : If the inputs are coming from a textbox ...
    : :
    : : [code]
    : : Dim total As Double
    : : Dim value As String
    : :
    : : value = someTextBox.Text.Trim
    : :
    : : If IsNumeric(value) Then
    : : total += CDbl(value)
    : : End If
    : : [/code]
    : :
    : : I'm sure you can incorpate this logic into your code some how.
    : :
    :
    :
    :
    : OK... here is my Code, please before you bash my code consider that I'm teaching myself VB, and have no formal training. Thanks. Just curious as to where to put that code in here.
    : [code]
    : Dim materials() As Decimal = {0.0008, 0.0138, 0.0552, 0.1656, 0.4968, 0.9936, 1.4904, 2.2356, 3.3534, 5.0301}
    : Dim mat1 As Decimal
    : Dim mat2 As Decimal
    : Dim mat3 As Decimal
    : Dim mat4 As Decimal
    : Dim mat5 As Decimal
    : Dim mat6 As Decimal
    : Dim mat7 As Decimal
    : Dim mat8 As Decimal
    : Dim mat9 As Decimal
    : Dim mat10 As Decimal
    : Dim total As Decimal
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    : Dim mat1 = TextBox1.Text * materials(0)
    : Dim mat2 = TextBox2.Text * materials(1)
    : Dim mat3 = TextBox4.Text * materials(2)
    : Dim mat4 = TextBox3.Text * materials(3)
    : Dim mat5 = TextBox8.Text * materials(4)
    : Dim mat6 = TextBox7.Text * materials(5)
    : Dim mat7 = TextBox6.Text * materials(6)
    : Dim mat8 = TextBox5.Text * materials(7)
    : Dim mat9 = TextBox10.Text * materials(8)
    : Dim mat10 = TextBox9.Text * materials(9)
    :
    : Label20.Text = mat1
    : Label19.Text = mat2
    : Label18.Text = mat3
    : Label17.Text = mat4
    : Label16.Text = mat5
    : Label15.Text = mat6
    : Label14.Text = mat7
    : Label13.Text = mat8
    : Label12.Text = mat9
    : Label11.Text = mat10
    : Label25.Text = (mat1 + mat2 + mat3 + mat4 + mat5 + mat6 + mat7 + mat8 + mat9 + mat10)
    : End Sub
    :
    : [/code]
    :

    For the way you wrote your code, you could write a utility function ...

    [code]
    Function ZeroIf(ByVal value As String) As Double
    value = value.Trim

    If IsNumeric(value) Then
    Return CDbl(value)
    Else
    Return 0
    End If
    End Function
    [/code]

    Then you can do this ...

    [code]
    Dim materials() As Decimal = {0.0008, 0.0138, 0.0552, 0.1656, 0.4968, 0.9936, 1.4904, 2.2356, 3.3534, 5.0301}
    Dim mat1 As Decimal
    Dim mat2 As Decimal
    Dim mat3 As Decimal
    Dim mat4 As Decimal
    Dim mat5 As Decimal
    Dim mat6 As Decimal
    Dim mat7 As Decimal
    Dim mat8 As Decimal
    Dim mat9 As Decimal
    Dim mat10 As Decimal
    Dim total As Decimal

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mat1 = ZeroIf(TextBox1.Text) * materials(0)
    Dim mat2 = ZeroIf(TextBox2.Text) * materials(1)
    Dim mat3 = ZeroIf(TextBox4.Text) * materials(2)
    Dim mat4 = ZeroIf(TextBox3.Text) * materials(3)
    Dim mat5 = ZeroIf(TextBox8.Text) * materials(4)
    Dim mat6 = ZeroIf(TextBox7.Text) * materials(5)
    Dim mat7 = ZeroIf(TextBox6.Text) * materials(6)
    Dim mat8 = ZeroIf(TextBox5.Text) * materials(7)
    Dim mat9 = ZeroIf(TextBox10.Text) * materials(8)
    Dim mat10 = ZeroIf(TextBox9.Text) * materials(9)

    Label20.Text = mat1
    Label19.Text = mat2
    Label18.Text = mat3
    Label17.Text = mat4
    Label16.Text = mat5
    Label15.Text = mat6
    Label14.Text = mat7
    Label13.Text = mat8
    Label12.Text = mat9
    Label11.Text = mat10
    Label25.Text = (mat1 + mat2 + mat3 + mat4 + mat5 + mat6 + mat7 + mat8 + mat9 + mat10)
    End Sub
    [/code]

    This is how I would solve your code.
  • iwilld0itiwilld0it Member Posts: 1,134
    I thought this would be a fun exercise to show you more about the .NET framework and how to write reusable code. This code solves your same problem (assuming you rename all the label controls from Label1 to Label10 insead of Label11 to Label20.)

    First I will show you the main code. This is the very same code that reads each texbox, calculates the material, and stores the result in a label.

    [code]
    Dim materials() As Double = { _
    0.0008, 0.0138, 0.0552, 0.1656, 0.4968, 0.9936, 1.4904, 2.2356, 3.3534, 5.0301}

    Dim i As Integer
    Dim total As Double = 0
    For i = 1 To 10
    Dim value As String = GetTextByName("TextBox" & i)
    value = value.Trim

    Dim result As Double = 0

    If IsNumeric(value) Then
    result = CDbl(value) * materials(i - 1)
    total += result
    End If

    SetTextByName("Label" & i, result.ToString)
    Next
    [/code]

    For one, the code utilizes the VB "For Next" loop. This basically repeates one set of code for each calculation, rather than writting the same code 10 times. Inside the loop, you will see two custom functions (GetTextByName and SetTextByName), this demonstrates how to modularize your code. It demonstrates code reuse as well. The two functions are defined below:

    [code]
    Public Function GetTextByName(ByVal name As String) As String
    Dim flags As BindingFlags = _
    BindingFlags.NonPublic Or _
    BindingFlags.GetProperty Or _
    BindingFlags.Instance

    Dim frmType As Type = Me.GetType
    Dim tbProp As PropertyInfo = frmType.GetProperty(name, flags)

    If Not tbProp Is Nothing Then
    Dim value As Object = tbProp.GetValue(Me, Nothing)
    If value.GetType Is GetType(TextBox) Then
    Return CType(value, TextBox).Text
    ElseIf value.GetType Is GetType(Label) Then
    Return CType(value, Label).Text
    End If
    End If

    Return ""
    End Function

    Public Sub SetTextByName(ByVal name As String, ByVal value As String)
    Dim flags As BindingFlags = _
    BindingFlags.NonPublic Or _
    BindingFlags.GetProperty Or _
    BindingFlags.Instance

    Dim frmType As Type = Me.GetType
    Dim tbProp As PropertyInfo = frmType.GetProperty(name, flags)

    If Not tbProp Is Nothing Then
    If tbProp.PropertyType Is GetType(Label) Then
    With CType(tbProp.GetValue(Me, Nothing), Label)
    .Text = value
    End With
    ElseIf tbProp.PropertyType Is GetType(TextBox) Then
    With CType(tbProp.GetValue(Me, Nothing), TextBox)
    .Text = value
    End With
    End If
    End If
    End Sub
    [/code]

    Both functions assume you have this line of code at the very top of the code page:

    [code]
    Imports System.Reflection
    [/code]

    Basically the functions look for a textbox or label control on the current form by name and then dynamically grab or set the controls text property. As it stands now, both these functions must be defined in the Form class for which they are used. You could further refine these functions to be reused on any Form. However, that will be up to you. To get a better understanding, read up on Reflection in the .NET framework.

    Also, you may argue that more code was involved to write the same solution, but now you have two reusable functions that serve many purposes now. Some will argue that it's overkill and sometimes I might agree. However, this is for educational purposes for one and for two, the execution of code in .NET is so fast, you would never notice.

    By fully understanding the code above, you will have made great leaps.


  • interpiminterpim Member Posts: 8
    Wow, Thank you so much for your help, Like I had said before I have been teaching myself VB, and really only have begun learning. I have a copy of Sams Teach Yourself Visual Basic .NET 2003 book, and a Students version of Visual Studio .NET 2003 software. I have made quite a few progresses, and learn more every time I run into a "snag". I appreciate all your help and extra effort to show me something new. Thanks Again.
  • iwilld0itiwilld0it Member Posts: 1,134
    : Wow, Thank you so much for your help, Like I had said before I have been teaching myself VB, and really only have begun learning. I have a copy of Sams Teach Yourself Visual Basic .NET 2003 book, and a Students version of Visual Studio .NET 2003 software. I have made quite a few progresses, and learn more every time I run into a "snag". I appreciate all your help and extra effort to show me something new. Thanks Again.
    :

    Get the books Programming Visual Basic.NET by Francesco Balena. This is probably the best book you could by on learning Visual Basic.NET. The copy I bought had 1600 pages. I think newer editions are slightly less pages but cover more advanced topics.-

Sign In or Register to comment.