Wrox Programmer Forums
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2007, 10:10 AM
Registered User
 
Join Date: Jul 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with Enumerations

Will somebody please be able to go over this piece of code in detail because im strugling, any help i will fully appreciate.

Public Class Form1
    Inherits System.Windows.Forms.Form
    'enum...
    Public Enum dayaction As Integer
        Asleep = 0
        gettingreadyforwork = 1
        travellingtowork = 2
        atwork = 3
        atlunch = 4
        travellingfromwork = 5
        relaxingwithfriends = 6
        gettingreadyforbed = 7
    End Enum
    'members
    Public currentstate As dayaction
    'hour property
    Public Property hour() As Integer
        Get
            Return trkhour.Value
        End Get
        Set(ByVal value As Integer)
            'set the hour
            trkhour.Value = value
            'determin the state
            Dim hour As Integer = value
            If hour >= 6 And hour < 7 Then
                currentstate = dayaction.travellingtowork
            ElseIf hour >= 7 And hour < 8 Then
                currentstate = dayaction.travellingtowork
            ElseIf hour >= 8 And hour < 13 Then
                currentstate = dayaction.atwork
            ElseIf hour >= 13 And hour < 14 Then
                currentstate = dayaction.atlunch
            ElseIf hour >= 14 And hour < 17 Then
                currentstate = dayaction.atwork
            ElseIf hour >= 17 And hour < 18 Then
                currentstate = dayaction.travellingfromwork
            ElseIf hour >= 18 And hour < 22 Then
                currentstate = dayaction.relaxingwithfriends
            ElseIf hour >= 22 And hour < 23 Then
                currentstate = dayaction.gettingreadyforbed
            Else
                currentstate = dayaction.Asleep
            End If
            'set the text
            Dim statustext As String
            statustext = "At " & value & ":00, Len is " & currentstate.ToString()
            'update the display
            txtstate.Text = statustext

        End Set
    End Property

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set the hour to the current hour
        Me.hour = Date.Now.Hour
    End Sub

    Private Sub trkhour_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkhour.Scroll
        'update the hour
        Me.hour = trkhour.Value
    End Sub
End Class

 
Old July 9th, 2007, 12:12 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you tell us what you are struggling with?

-Peter
 
Old July 9th, 2007, 04:43 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Seems OK. Some suggestions:
Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Enum...
    Public Enum dayaction As Integer
        Asleep = 0
        gettingreadyforwork = 1
        travellingtowork = 2
        atwork = 3
        atlunch = 4
        travellingfromwork = 5
        relaxingwithfriends = 6
        gettingreadyforbed = 7
    End Enum

    ' Members
    Public currentstate As dayaction
    ' Hour property
    Public Property hour() As Integer
        Get
            Return trkhour.Value
        End Get

        Set(ByVal value As Integer)

            trkhour.Value = value   ' Set the hour

            ' You already have an integer named value.  Why 
            ' transfer it to another integer?
          ' Dim hour As Integer = value     ' Determine the state

            Select Case value               ' Since hour is an integer, . . .
                Case 6                      ' 6 is the only int >= 6 and < 7
                    currentstate = dayaction.travellingtowork
                Case 7                      ' 7 is the only int >= 7 and < 8
                     currentstate = dayaction.travellingtowork
                Case 8, 9, 10, 11, 12       ' This is how incluse Between is done in a Select Case.
                     currentstate = dayaction.atwork
                Case 13
                    currentstate = dayaction.atlunch
                Case 14, 15, 16
                     currentstate = dayaction.atwork
                Case 17
                    currentstate = dayaction.travellingfromwork
                Case 18, 19, 20, 21
                    currentstate = dayaction.relaxingwithfriends
                Case 22
                    currentstate = dayaction.gettingreadyforbed
                Case Else
                    currentstate = dayaction.Asleep
            End Select

            ' Set the text
            ' Update the display
            txtstate.Text = "At " & value & ":00, Len is " & currentstate.ToString()

        End Set

    End Property

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the hour to the current hour
        Me.hour = Date.Now.Hour
    End Sub

    Private Sub trkhour_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trkhour.Scroll
        ' Update the hour
        Me.hour = trkhour.Value
    End Sub

End Class
 
Old July 9th, 2007, 05:01 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I ran the app (created the controls to make it fly), and I had to change the signature of the Scroll event. The event argument is a specific type for scroll control, and the signature must match. Once I made the change, ran fine:
Code:
Private Sub trkhour_Scroll(ByVal sender As System.Object, _
                           ByVal e As System.Windows.Forms.ScrollEventArgs _
                          ) Handles trkhour.Scroll





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5: Enumerations Richard Stringfellow BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 3 December 5th, 2006 10:46 PM
Chapter 5: Enumerations Richard Stringfellow Visual Basic 2005 Basics 0 December 2nd, 2006 01:28 PM
Enumerations JAL C# 2 November 20th, 2005 10:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.