CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: VB Programming Array Problem (with code)

  1. #1
    Join Date
    Sep 2002
    Location
    Baton Rouge
    Posts
    26

    Angry VB Programming Array Problem (with code)

    Can anyone help me with this code. I have run into a brick wall. I am suppose to have the do while loop that validates the course information. My program validates only the first set of boxes and no more (and I havea control array of 5 boxes). Any help would be greatly appreciated.

    Ryan


    Private Sub cmdRegister_Click()
    'Checks the Classes form for valid data. If the data is valid the Register
    'button will add up the total hours along with activating the CLear button

    Dim i As Integer
    Dim blnerror As Boolean
    Dim inttotal As Integer

    i = 0
    blnerror = False

    Do While i < txtCourse1.Count And blnerror = False
    'Call validation then increment i
    Call IsCourseHoursValid(txtCourse1(i), txtHours1(i))
    If IsCourseHoursValid = True Then
    i = i + 1
    Else
    blnerror = True
    End If
    Loop

    If blnerror = False Then
    'Total Hours
    For i = 0 To txtCourse1.Count - 1
    If txtHours1(i).Text <> "" Then
    inttotal = inttotal + txtHours1(i).Text
    End If
    Next i
    If inttotal = 0 Then
    MsgBox " You must enter at least one course."
    lblHours.Caption = 0
    Else
    lblHours.Caption = inttotal
    End If
    Else
    'Clear Label
    lblHours.Caption = ""
    End If

    'Enables Clear button
    If cmdClear.Enabled = False Then
    cmdClear.Enabled = True
    End If

    End Sub
    Private Function IsCourseHoursValid() As Boolean

    Dim blnerror As Boolean
    Dim i As Integer

    'Checks for valid data in the course ID and hours textboxes

    If txtCourse1(i) = "" And txtHours1(i) <> "" Then
    MsgBox "Please Enter a Course"
    txtCourse1(i).SetFocus
    IsCourseHoursValid = False
    blnerror = True

    ElseIf txtCourse1(i) <> "" And txtHours1(i) = "" Then
    MsgBox "Please Enter Course Credit Hours"
    txtHours1(i).SetFocus
    IsCourseHoursValid = False
    blnerror = True

    Else
    IsCourseHoursValid = True
    blnerror = False

    End If
    If blnerror = False Then
    If IsCourseValid = False Then

    IsCourseHoursValid = False
    ElseIf IsCreditValid = False Then
    MsgBox "The Course is not defined"
    txtCourse1(i).SetFocus
    IsCourseHoursValid = False
    ElseIf IsHoursValid = False Then
    MsgBox "Course Credit Hours are between 1 and 3 hours"
    txtHours1(i).SetFocus
    IsCourseHoursValid = False
    End If
    End If
    End Function

    Private Function IsCourseValid() As Boolean

    'Checks the Course name to make sure it is the appropriate length

    Dim intLocationofSpace As Integer
    Dim intlength As Integer
    Dim strname As String
    Dim i As Integer

    intLocationofSpace = InStr(txtCourse1(i).Text, " ")
    intlength = Len(txtCourse1(i).Text)

    If intLocationofSpace = 0 Or intLocationofSpace = intlength Then
    MsgBox "There must be a space between course name and number"
    txtCourse1(i).SetFocus
    IsCourseValid = False
    Else
    IsCourseValid = True
    End If

    If IsCourseValid = True Then
    strname = Left(txtCourse1(i), intLocationofSpace - 1)
    If Len(strname) < 2 Or Len(strname) > 4 Then
    MsgBox "Course name must be 2 to 4 characters"
    txtCourse1(i).SetFocus
    IsCourseValid = False
    Else
    IsCourseValid = True
    End If
    End If
    End Function

    Private Function IsCreditValid() As Boolean

    'Checks the Course name and ID to check if the course number is a valid number

    Dim intLocationofSpace As Integer
    Dim i As Integer
    Dim strnum As String
    Dim intlength As Integer

    intLocationofSpace = InStr(txtCourse1(i).Text, " ")
    intlength = Len(txtCourse1(i).Text)
    strnum = Right(txtCourse1(i), intlength - intLocationofSpace)
    If IsNumeric(Right(txtCourse1(i).Text, intlength - intLocationofSpace)) Then
    If strnum < 1000 Or strnum > 8999 Then
    IsCreditValid = False
    Else
    IsCreditValid = True
    End If
    End If

    End Function

  2. #2
    Join Date
    Aug 2001
    Posts
    1,447
    the two consecutive statements

    Call IsCourseHoursValid(txtCourse1(i), txtHours1(i))
    If IsCourseHoursValid = True Then

    appear to be nonsense. the second statement references a function return variable in a way that should give a compiler error

    try

    if IsCourseHoursValid(txtCourse1(i), txtHours1(i)) = True then

    I haven't looked at the rest of your code.
    phinds
    vs2008, 3.5SP1 Version 9.0.21022.8 RTM

  3. #3
    Join Date
    Sep 2002
    Location
    Baton Rouge
    Posts
    26
    Ok, thanks for the reply. When I try this is tells me that I have teh wrong number of arguments or property invalid for the IsCourseHoursValid. I do not refer to any string or anything for the ISCOurseHoursValid....do I need to put txtcourse1(i) and txthours1(i) into strings and test them in the function or can I leave it how I have it.

    Thanks,

    Ryan

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    You have not argument lists on your Isvalidxxx() function declarations either.... ie:

    Private Funciton IsValidCourse(course as string, whatever as string) as boolean

    .....


    end Function

  5. #5
    Join Date
    Sep 2002
    Location
    Baton Rouge
    Posts
    26
    After I do course as string and hours as string will I need to declare those variables in that function?

    Ryan

  6. #6
    Join Date
    Sep 2002
    Location
    Baton Rouge
    Posts
    26
    Another quick question, when I refer to the array in teh function do I need to specify txtcourse(i) or can I do txtcourse??

    Thanks,

    Ryan

  7. #7
    Join Date
    Sep 2002
    Location
    Baton Rouge
    Posts
    26

    Ok I am not getting an error but the logic is wrong

    Does anyone have any suggestions for this. The code is working properly but the program will only check the first set of boxes for the control array. It completely skips over the 2-5 boxes. Do I need to decalre i in each function and put it with each string or will that already be defined.

    Here is my code:

    Private Sub cmdRegister_Click()
    'Checks the Classes form for valid data. If the data is valid the Register
    'button will add up the total hours along with activating the CLear button

    Dim i As Integer
    Dim blnerror As Boolean
    Dim inttotal As Integer

    i = 0
    blnerror = False

    Do While i < txtCourse1.Count And blnerror = False
    'Call validation then increment i
    If IsCourseHoursValid(txtCourse1(i).Text, txtHours1(i).Text) = True Then
    i = i + 1
    blnerror = False
    Else
    blnerror = True
    End If
    Loop

    If blnerror = False Then
    'Total Hours
    For i = 0 To txtCourse1.Count - 1
    If txtHours1(i).Text <> "" Then
    inttotal = inttotal + txtHours1(i).Text
    End If
    Next i
    If inttotal = 0 Then
    MsgBox " You must enter at least one course."
    lblHours.Caption = 0
    Else
    lblHours.Caption = inttotal
    End If
    Else
    'Clear Label
    lblHours.Caption = ""
    End If

    'Enables Clear button
    If cmdClear.Enabled = False Then
    cmdClear.Enabled = True
    End If

    End Sub
    Private Function IsCourseHoursValid(strcourse1 As String, strhours1 As String) As Boolean

    Dim blnerror As Boolean
    Dim i As Integer

    'Checks for valid data in the course ID and hours textboxes

    If txtCourse1(i) = "" And txtHours1(i) <> "" Then
    MsgBox "Please Enter a Course"
    txtCourse1(i).SetFocus
    IsCourseHoursValid = False
    blnerror = True

    ElseIf txtCourse1(i) <> "" And txtHours1(i) = "" Then
    MsgBox "Please Enter Course Credit Hours"
    txtHours1(i).SetFocus
    IsCourseHoursValid = False
    blnerror = True

    Else
    IsCourseHoursValid = True
    blnerror = False

    End If
    If blnerror = False Then
    If IsCourseValid(txtCourse1(i).Text) = False Then
    IsCourseHoursValid = False
    ElseIf IsCreditValid(txtCourse1(i).Text) = False Then
    MsgBox "The Course is not defined"
    txtCourse1(i).SetFocus
    IsCourseHoursValid = False
    ElseIf IsHoursValid(txtHours1(i).Text) = False Then
    MsgBox "Course Credit Hours are between 1 and 3 hours"
    txtHours1(i).SetFocus
    IsCourseHoursValid = False
    End If
    End If
    End Function

    Private Function IsCourseValid(strcourse1 As String) As Boolean

    'Checks the Course name to make sure it is the appropriate length

    Dim intLocationofSpace As Integer
    Dim intlength As Integer
    Dim strname As String
    Dim i As Integer

    intLocationofSpace = InStr(strcourse1, " ")
    intlength = Len(strcourse1)

    If intLocationofSpace = 0 Or intLocationofSpace = intlength Then
    MsgBox "There must be a space between course name and number"
    txtCourse1(i).SetFocus
    IsCourseValid = False
    Else
    IsCourseValid = True
    End If

    If IsCourseValid = True Then
    strname = Left(strcourse1, intLocationofSpace - 1)
    If Len(strname) < 2 Or Len(strname) > 4 Then
    MsgBox "Course name must be 2 to 4 characters"
    txtCourse1(i).SetFocus
    IsCourseValid = False
    Else
    IsCourseValid = True
    End If
    End If
    End Function

    Private Function IsCreditValid(strcourse1 As String) As Boolean

    'Checks the Course name and ID to check if the course number is a valid number

    Dim intLocationofSpace As Integer
    Dim i As Integer
    Dim strnum As String
    Dim intlength As Integer

    intLocationofSpace = InStr(strcourse1, " ")
    intlength = Len(strcourse1)
    strnum = Right(strcourse1, intlength - intLocationofSpace)
    If IsNumeric(Right(strcourse1, intlength - intLocationofSpace)) Then
    If strnum < 1000 Or strnum > 8999 Then
    IsCreditValid = False
    Else
    IsCreditValid = True
    End If
    End If

    End Function

    Private Function IsHoursValid(strhours1 As String) As Boolean

    'Checks the course hours to see if it is a valid number
    Dim i As Integer

    If IsNumeric(strhours1) = True Then
    If txtHours1(i) < 1 Or txtHours1(i) > 3 Then
    IsHoursValid = False
    Else
    IsHoursValid = True
    End If
    Else
    MsgBox "Please enter a numeric number for course hours"
    IsHoursValid = False
    End If

    End Function

    I would be very thankful for any help.

    Ryan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)