9 Replies - 1510 Views - Last Post: 26 November 2012 - 11:42 PM Rate Topic: -----

#1 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Errors with enhanced time project

Posted 18 November 2012 - 09:03 PM

I went by the book. I have come to realize my book contains many errors. I'm wondering if I have too much code?
My first error is showing a green line under NEW at aproximately line 11 and 18. Not sure how to get line numbers to show up?

My next problem comes in at the Private Sub addMinuteButton there's a blue line under the incrementsMinutes.Click and it says: Handle clause requires a WithEvents variable defined in the containing type or one of it's base types.

Lastly the " & time.ToUniversalString towards the end of the code created a private function on it's own? Is the throw new exception going to work?


Public Class Time



    'declare integer instance variables for the hour, minute and second
    Private hourValue As Integer         ' 0-23
    Private minuteValue As Integer      ' 0-59
    Private secondValue As Integer      '0-59

    'Time constuctor with hour, minute and second as optional parameters
    Public Sub New(Optional ByVal h As Integer = 12, Optional ByVal m As Integer = 0, Optional ByVal s As Integer = 0)

        SetTime(h, m, s)                'call SetTime with three arguments

    End Sub

    ' Time constructor: another Time object supplied
    Public Sub New(ByVal t As Time)

        SetTime(t.hourValue, t.minuteValue, t.secondValue)

    End Sub   'New

    ' set a new time value using universal time, check validity of the data
    Public Sub SetTime(ByVal h As Integer, ByVal m As Integer, ByVal s As Integer)

        Hour = h                    ' Set accessor validates the hour
        Minute = m                  ' Set accessor validates the minute
        Second = s                  ' Set accessor validates the second

    End Sub   'Set Time

    ' property Hour
    Public Property Hour() As Integer
        Get                         ' return hourValue
            Return hourValue

        End Get

        Set(ByVal value As Integer)             ' set HourValue
            If (value >= 0 AndAlso value < 24) Then
                hourValue = value
            Else
                Throw New ArgumentOutOfRangeException
            End If
        End Set
    End Property  'Hour

    'property Minute
    Public Property Minute() As Integer
        Get                     ' return minuteValue
            Return minuteValue

        End Get

        Set(ByVal value As Integer)             ' set minuteValue
            If (value >= 0 AndAlso value < 60) Then
                minuteValue = value
            Else
                Throw New ArgumentOutOfRangeException
            End If
        End Set
    End Property

    'property Second
    Public Property Second() As Integer
        Get
            Return secondValue
        End Get

        Set(ByVal value As Integer)             ' set secondValue
            If (value > -0 AndAlso value < 60) Then
                secondValue = value
            Else
                Throw New ArgumentOutOfRangeException
            End If
        End Set
    End Property  'Hour

    Dim time As New Time() ' construct Time with zero arguments  

    ' invoked when user clicks the Add 1 to Second button  
    Private Sub addSecondButton_Click(ByVal sender As System.Object,
       ByVal e As System.EventArgs) Handles incrementSecondButton.Click
        time.Second = (time.Second + 1) Mod 60 ' add 1 to Second  
        If time.Second = 0 Then
            time.Minute = (time.Minute + 1) Mod 60 ' add 1 to Minute    
        End If

        UpdateDisplay() ' update the TextBoxes and output Labels  
    End Sub ' addSecondButton_Click  

    Private Sub addHourButton_Click(ByVal sender As System.Object,
         ByVal e As System.EventArgs) Handles incrementHourButton.Click
        time.Hour = (time.Hour + 1) Mod 24


        UpdateDisplay()
    End Sub

    Private Sub addMinuteButton_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles incrementMinuteButton.Click
        time.Minute = (time.Minute + 1) Mod 60
        If time.Minute = 0 Then
            time.Hour = (time.Hour + 1) Mod 24

        End If
        UpdateDisplay()
    End Sub

    ' set time based on TextBox values  
    Private Sub setTimeButton_Click(ByVal sender As System.Object,
       ByVal e As System.EventArgs) Handles setTimeButton.Click

        ' ensure that hour, minute and second are in range  
        Try
            If setHourTextBox.Text <> String.Empty Then
                time.Hour = Convert.ToInt32(setHourTextBox.Text)
            End If

            If setMinuteTextBox.Text <> String.Empty Then
                time.Minute = Convert.ToInt32(setMinuteTextBox.Text)
            End If


            If setSecondTextBox.Text <> String.Empty Then
                time.Second = Convert.ToInt32(setSecondTextBox.Text)
            End If
        Catch ex As ArgumentOutOfRangeException
            MessageBox.Show("The hour, minute or second was out of range",
               "Out of Range", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        UpdateDisplay() ' update the TextBoxes and output Labels  
    End Sub ' setTimeButton_Click  


    ' update time display  
    Private Sub UpdateDisplay()

        setHourTextBox.Text = Convert.ToString(time.Hour)
        setMinuteTextBox.Text = Convert.ToString(time.Minute)
        setSecondTextBox.Text = Convert.ToString(time.Second)
        output1Label.Text = ("Hour: " & time.Hour & "; Minute: " &
        time.Minute & "; Second: " & time.Second)
        output2Label.Text = ("Standard time is: " & time.ToString() &
        "; Universal Time is: " & time.ToUniversalString())

    End Sub ' UpdateDisplay  

    Private Function ToUniversalString() As String
        Throw New NotImplementedException
    End Function

End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Errors with enhanced time project

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Errors with enhanced time project

Posted 18 November 2012 - 09:32 PM

View Postcharyl, on 19 November 2012 - 09:33 AM, said:

My next problem comes in at the Private Sub addMinuteButton there's a blue line under the incrementsMinutes.Click and it says: Handle clause requires a WithEvents variable defined in the containing type or one of it's base types.


I can help you with this.

You need to supply the necessary WithEvents variable.

For example,your method

Private Sub addSecondButton_Click

should be declared in the base class like this.

Private WithEvents addSecondButton As LinkButton

Similarly for your other methods.

Hope this helps :)

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#3 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Errors with enhanced time project

Posted 18 November 2012 - 10:17 PM

Thanks Raghav! Now I have left is a green line under the NEW (s) there are 2
Says: Public Sub New....time should call initialize component method.
(confused on what that means? And for the Universal time, do I need to import something?

    'Time constuctor with hour, minute and second as optional parameters
    Public Sub New(Optional ByVal h As Integer = 12, Optional ByVal m As Integer = 0, Optional ByVal s As Integer = 0)

        SetTime(h, m, s)                'call SetTime with three arguments

    End Sub

    ' Time constructor: another Time object supplied
    Public Sub New(ByVal t As Time)

        SetTime(t.hourValue, t.minuteValue, t.secondValue)

    End Sub   'New

Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Errors with enhanced time project

Posted 18 November 2012 - 11:17 PM

Hi Charyl

Can you please post the exact error that you are getting? I am not able to understand what you are trying to say.

regards,
Raghav
Was This Post Helpful? 1
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Errors with enhanced time project

Posted 18 November 2012 - 11:39 PM

If you want to view line numbers in VS editor:
Tools->Options->Text editor->All languages->General in the Display section check Line numbers.

When you want to point to the line in code you are posting, look what line number it has here, not in your code editor (use preview, and you'll know what line number it is).

You want to use ToUniversalString method of Time class? What exactly represents Time class? Is it not your form perhaps?

Use DateTime structure.

This post has been edited by lucky3: 18 November 2012 - 11:40 PM

Was This Post Helpful? 2
  • +
  • -

#6 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Errors with enhanced time project

Posted 19 November 2012 - 04:54 AM

View Postraghav.naganathan, on 19 November 2012 - 01:17 AM, said:

Hi Charyl

Can you please post the exact error that you are getting? I am not able to understand what you are trying to say.

regards,
Raghav



There is a green line under New it says Public Sub New(t As Time)in designer generated type should call intitializerComponent method.?
Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Errors with enhanced time project

Posted 19 November 2012 - 05:26 AM

The error is pretty clear. In the New method(Sub) you should call the InitializeComponent() method. In objects (classes, forms) when you write:
Dim cls1 As New Class1



What it do is calls your New() method(sub) from your class and since your using form you should call the InitializeComponent() method in it so that the form can draw the controls etc. What you're doing is overloading the New method for the form but for that you need to call and the initialize method.

good luck
Was This Post Helpful? 1
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Errors with enhanced time project

Posted 19 November 2012 - 07:31 AM

Once again... Time is form and Time <> DateTime (read my previous post). Look how many times you use your form inside it:
  • Public Sub New(ByVal t As Time)
  • Dim time As New Time()

Then you use this local time variable in every button click handler, and UpdateDisplay(). Finally you want to have it use something like ToUniversalString.

I'm pretty sure you confused your Time form with DateTime, where you can find ToUniversalTime method.
Was This Post Helpful? 1
  • +
  • -

#9 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: Errors with enhanced time project

Posted 26 November 2012 - 05:43 PM

View Postraziel_, on 19 November 2012 - 07:26 AM, said:

The error is pretty clear. In the New method(Sub) you should call the InitializeComponent() method. In objects (classes, forms) when you write:
Dim cls1 As New Class1



What it do is calls your New() method(sub) from your class and since your using form you should call the InitializeComponent() method in it so that the form can draw the controls etc. What you're doing is overloading the New method for the form but for that you need to call and the initialize method.

good luck




I'm sorry. I've come back to this to perfect it. So I may understand better...I don't understand. I tried putty the above code in different places, but it didn't work...I'm a dork, but I will get it momentarily.
Was This Post Helpful? 0
  • +
  • -

#10 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: Errors with enhanced time project

Posted 26 November 2012 - 11:42 PM

You don't have Class1 declared anywhere, that's why you can't use it in your code. You have a Form named Time. On Time form, you have textboxes for hours, minutes, seconds, and labels for showing set time value, and universal time value. And there is a button for submitting entered time values.

Based on that, I assume you want to enter values in textboxes, click on button, and have times displayed in labels. Then change values, if you wish to, click button again, and see output/change displayed. Is that correct? Do you have any need to display changed values in new form?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1