none
[VB2010] - using a const and numbers in a variable\enum

    Question

  • i need 1 variable for use the Automatic const and numbers.

    Private Enum Time        
           Automatic = -1        
           1 to 5000  'i get 1 error here, but can i change these?  
    End Enum

    how i need to use it:

    dim s as Time

    s=Automatic

    or

    s=1000

    how can i do these?


    • Edited by Cambalinho Sunday, July 28, 2013 11:10 AM correct my english
    Sunday, July 28, 2013 11:09 AM

Answers

  • now the real thing: how can i associate the const to that property(for be showed in property window too)?

    I think that you are saying that the valid values for time are -1 (meaning 'automatic') or (for example) 1 to 1000, and you want to indicate that these options are available for your property in the property list.

    I don't know of any way to do that (apart from the impractical one of creating an enum for each valid integer).

    What you can do is create Automatic as a property (Boolean - True or False) and Time as a property (Integer) and you can put code in the Automatic property set that sets Time to -1 when Automatic is set to True, and code in the Time property set that sets Automatic to False when Time is set to anything other than -1.  So the two properties each indicate their own valid values, but they are always in sync.

    • Marked as answer by Cambalinho Sunday, July 28, 2013 7:24 PM
    Sunday, July 28, 2013 12:23 PM
  • today i learn 1 thing: when we do an enum, we can use that consts and numbers too;)

    Public Class Form1
        Enum timerIntervals
            auto = -1
            defaultval = 10
            onesec = 1000
            tensec = 10000
        End Enum
    
        Dim foo As timerIntervals = timerIntervals.auto
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            MsgBox(foo.ToString)
            foo = 125
            MsgBox(foo.ToString)
        End Sub
    End Class

    like you see, i use another numbers and the enum consts;)

    thanks to all

    • Marked as answer by Cambalinho Sunday, July 28, 2013 7:24 PM
    Sunday, July 28, 2013 7:23 PM

All replies

  • Hello,

    Please refer to Enum documentation, members are what you set them as and are unchanging. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Sunday, July 28, 2013 11:26 AM
    Moderator
  • Hi,

     I am not sure what you are trying to do with the (1 to 5000) part but, here is an example of what i think you are doing by assigning s an integer number or assigning s to your Automatic Enum value.

    Public Class Form1
    
        Private Enum MyTime As Integer
            Automatic = -1
        End Enum
    
        Dim s As MyTime
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            s = MyTime.Automatic 'Assign Automatic (-1) to the s variable
            MessageBox.Show(s.ToString)
    
            'Or
    
            s = CType(1000, MyTime) 'Assign 1000 to the s variable
            MessageBox.Show(s.ToString)
        End Sub
    End Class
    

    Sunday, July 28, 2013 11:35 AM
  • hello to all

    see these code that i did:

    Public Class Class1
    
        Private intTime As Integer
        Public Const Automatic As Integer = -1
    
        Public Property Time As Integer
    
            Get
                Return intTime
            End Get
            Set(value As Integer)
                intTime = value
            End Set
        End Property
    End Class

    Dim s As New Class1
    s.Time = Class1.Automatic

    now the real thing: how can i associate the const to that property(for be showed in property window too)?
    • Edited by Cambalinho Sunday, July 28, 2013 11:47 AM i forget the other code
    Sunday, July 28, 2013 11:45 AM
  • now the real thing: how can i associate the const to that property(for be showed in property window too)?

    I think that you are saying that the valid values for time are -1 (meaning 'automatic') or (for example) 1 to 1000, and you want to indicate that these options are available for your property in the property list.

    I don't know of any way to do that (apart from the impractical one of creating an enum for each valid integer).

    What you can do is create Automatic as a property (Boolean - True or False) and Time as a property (Integer) and you can put code in the Automatic property set that sets Time to -1 when Automatic is set to True, and code in the Time property set that sets Automatic to False when Time is set to anything other than -1.  So the two properties each indicate their own valid values, but they are always in sync.

    • Marked as answer by Cambalinho Sunday, July 28, 2013 7:24 PM
    Sunday, July 28, 2013 12:23 PM
  • now the real thing: how can i associate the const to that property(for be showed in property window too)?

    I think that you are saying that the valid values for time are -1 (meaning 'automatic') or (for example) 1 to 1000, and you want to indicate that these options are available for your property in the property list.

    I don't know of any way to do that (apart from the impractical one of creating an enum for each valid integer).

    What you can do is create Automatic as a property (Boolean - True or False) and Time as a property (Integer) and you can put code in the Automatic property set that sets Time to -1 when Automatic is set to True, and code in the Time property set that sets Automatic to False when Time is set to anything other than -1.  So the two properties each indicate their own valid values, but they are always in sync.

    thanks for the tip
    Sunday, July 28, 2013 4:34 PM
  • today i learn 1 thing: when we do an enum, we can use that consts and numbers too;)

    Public Class Form1
        Enum timerIntervals
            auto = -1
            defaultval = 10
            onesec = 1000
            tensec = 10000
        End Enum
    
        Dim foo As timerIntervals = timerIntervals.auto
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            MsgBox(foo.ToString)
            foo = 125
            MsgBox(foo.ToString)
        End Sub
    End Class

    like you see, i use another numbers and the enum consts;)

    thanks to all

    • Marked as answer by Cambalinho Sunday, July 28, 2013 7:24 PM
    Sunday, July 28, 2013 7:23 PM
  • I have seen this before ;)

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Sunday, July 28, 2013 7:49 PM
  • I have seen this before ;)

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    don't ask how i did... i just try it lol
    Sunday, July 28, 2013 8:04 PM