Log in

No account? Create an account
entries friends calendar profile ABMann.net Previous Previous Next Next
Coding - Portrait of a Young Man as The Artist — LiveJournal

Our project for today was to design a program that calculates target heart rates for a user given their birthdate and heart rate. Now, we were supposed to learn to use SELECT CASE and If/Then statements are were not allowed use of any robust date functions (Like Cdate() or IsDate()). We had to break the user interface (UI) into three sections for Day, Month and Year rather than allow single entry in MM/DD/YYYY format.

Because we had three fields with which to deal, I had to write this nutty bit of code to validate for correct Day/Month combos, accept both YY and YYYY format and check for leap years.

'validate year data
 sTemp = Val(txtYear.Text)  'store year data in temporary string
    If sTemp <= 0 Then
      Call MsgBox("Please enter Year as a number.", , "In the zone - Year")
      txtYear.BackColor = &H8080FF
      Exit Sub
    ElseIf IsNumeric(sTemp) Then
      If Len(sTemp) = 4 Then
        iYear = sTemp
      ElseIf Len(sTemp) = 2 Then
          If Val(sTemp) < 30 Then
            iYear = 20 & sTemp
            iYear = 19 & sTemp 'the previous was a ridiculously involved validation of the date, so both YY and YYYY formats work
          End If
      End If
    End If

'validate month data
  iMonth = Val(txtMonth.Text)  'store Month as numeric data
    If iMonth < 1 Or iMonth > 12 Then  'validate Month value for numeric data between 1 and 12
      Call MsgBox("Please enter Month as a number between 1 and 12.", , "In the zone - Month")
      txtMonth.BackColor = &H8080FF 'highlight field to fix
      Exit Sub
    End If

'set var that will determine valid day/month combos
  Select Case iMonth
    Case 4, 6, 9, 11
      iValidDay = 30
    Case 1, 3, 5, 7, 8, 10, 12
      iValidDay = 31
    Case 2 And iYear Mod 4 = 0 And iYear Mod 100 <> 0
      iValidDay = 29
    Case 2 And iYear Mod 4 <> 0
      iValidDay = 28
    End Select
'Validate & store day
  iDay = Val(txtDay.Text)  'store day as numeric data
   If iDay < 1 Or iDay > iValidDay Then
    Call MsgBox("Please enter Day as a number between 1 and " & iValidDay & ".", , "In the zone - Day")
    txtDay.BackColor = &H8080FF 'highlight incorrect field
    txtDay.SetFocus 'change cursor focus
    Exit Sub
  End If

'Calculate user age
  If iDay <= Day(Date) And iMonth <= Month(Date) Then
    iAge = Year(Date) - iYear 'calculate age
    iAge = Year(Date) - iYear - 1
  End If ' display age (used to test age formula)

End Sub

Now, it works. I'm willing to bet it's really clunky given that this is my second day coding in VB6.

Were we allowed to use the nifty date functions, the entire code above would have been as follows.
If IsDate(txtBirthDate.Text) Then 'validate if data entered is a real date
  iBirthdate = txtBirthDate 'store Birthdate in iBirthDate
  Call MsgBox("Please enter birthdate in Month Day, YYYY format.",,"Target Heart Rate - Date error") 'pop an error if date is invalid
End If

iBirthDateConverted = CDate(iBirthDate) 'convert data to date expression.. to be safe...

Not sure if that's exactly right, suffice that the code would be 10% as long as it is.

Still having fun in class though.

Tags: , ,
Current Mood: cranky Code-y

11 comments or Leave a comment
alibee From: alibee Date: June 6th, 2006 09:11 pm (UTC) (Link)
As a Java programmer, I will now state the following:

Holy crap is VB ugly.
abmann From: abmann Date: June 6th, 2006 09:27 pm (UTC) (Link)
hehe. Yeah, prolly. but it's what I'm learning.

I should get back into PHP. Ha was weird.
alibee From: alibee Date: June 6th, 2006 09:29 pm (UTC) (Link)
Ha was weird? :)

I realize that the ugliness of VB is mainly a syntax thing and not something you can help :). As long as you learn good programming practices, the syntax is irrelevant, anyway.

We had a code review today in which we ripped one of our guy's code apart. Heh. THAT was fun.
abmann From: abmann Date: June 6th, 2006 09:59 pm (UTC) (Link)
Swore I hit more letter than that. "That" was weird, as in PHP.
kingfox From: kingfox Date: June 6th, 2006 09:32 pm (UTC) (Link)
As a person raised on C++ who finds himself doing Perl and PHP on a semi-regular basis, I'll second that emotion.
From: thegelf Date: June 6th, 2006 09:57 pm (UTC) (Link)
You wouldn't happen to know matlab at all by chance? I started learning VB to interface with matlab, got my first project working, then on my second project said screw it and started working entirely in matlab. I'm regretting that choice...
abmann From: abmann Date: June 6th, 2006 10:00 pm (UTC) (Link)
No idea. Sorry.
fiendishx From: fiendishx Date: June 10th, 2006 10:31 pm (UTC) (Link)
Dude, matlab is like...the awesome. As long as you don't try to interface with it with other languages.
Or expect it to not crash every few hours.
But other than that, I heart Matlab.
From: thegelf Date: June 11th, 2006 03:28 am (UTC) (Link)
Ahh, so the crashing every few hours is normal, is it?
And the Matlab gui seems a bit buggy...
fiendishx From: fiendishx Date: June 11th, 2006 03:34 am (UTC) (Link)
Well, it's normal in the sense that one wouldn't be surprised at and might even expect a retarded man to regularly hit himself or drool or go on murderous rampages.

The matlab gui is so crappy because it's done in Java and tacked on top of the execution framework which is not.
From: thegelf Date: June 11th, 2006 05:21 pm (UTC) (Link)
The execution framework is in C, right?

I had no idea you had an lj. ::friends::
11 comments or Leave a comment