VB.netenum and Constants

Embed Size (px)

Citation preview

  • 7/25/2019 VB.netenum and Constants

    1/6

    VB.Net - Constants and Enumerations

    The constantsrefer to fixed values that the program may not alter during

    its execution. These fixed values are also called literals.

    Constants can be of any of the basic data types like an integer constant afloating constant a character constant or a string literal. There are also

    enumeration constants as !ell.

    The constants are treated "ust like regular variables except that their values

    cannot be modified after their definition.

    #n enumerationis a set of named integer constants.

    $eclaring Constants%n VB.Net constants are declared using the Conststatement. The Const

    statement is used at module class structure procedure or block level for

    use in place of literal values.

    The syntax for the Const statement is&

    [][accessmodifer ][Shadows]

    Constconstantlist

    'here

    attributelist& specifies the list of attributes applied to the constants( you can provide

    multiple attributes separated by commas. )ptional.

    accessmodifier& specifies !hich code can access these constants. )ptional. Values can

    be either of the& *ublic *rotected +riend *rotected +riend or *rivate.

    Shadows& this makes the constant hide a programming element of identical name in a

    base class. )ptional.

    Constantlist& gives the list of names of constants declared. ,euired.

    'here each constant name has the follo!ing syntax and parts&

    constantname [Asdatatype ]=initializer

    constantname& specifies the name of the constant

  • 7/25/2019 VB.netenum and Constants

    2/6

    datatype& specifies the data type of the constant

    initializer& specifies the value assigned to the constant

    +or example

    'The ollowin! statements declare constants"'

    Constma#$al As%on!=&

    (ublicConstmessa!e AsStrin!=)*+%%,)

    (ri$ateConstpi-alue As.ouble=/"0&01

    ExampleThe follo!ing example demonstrates declaration and use of a constant

    value&

    2oduleconstants3enum

    Sub2ain45

    Const(6 =/"0&0&

    .imradius7area AsSin!le

    radius =8

    area =(6 9radius 9radius

    Console":rite%ine4)Area = );Str4area55

    Console"eadey45

    +ndSub

    +nd2odule

    'hen the above code is compiled and executed it produces the follo!ing

    result&

    Area=01/"//

    *rint and $isplay Constants in VB.NetVB.Net provides the follo!ing print and display constants&

    Constant Description

    vbCrf Carriage return/linefeed character combination.

    vbCr Carriage return character.

  • 7/25/2019 VB.netenum and Constants

    3/6

    vbf inefeed character.

    vbNe!ine Ne!line character.

    vbNullChar Null character.

    vbNull0tring Not the same as a 1ero-length string 2334( used for

    calling external procedures.

    vb)b"ectError Error number. 5ser-defined error numbers should

    be greater than this value. +or example&

    Err.,aise2Number4 6 vb)b"ectError 7 8999

    vbTab Tab character.

    vbBack Backspace character.

    $eclaring Enumerations#n enumerated type is declared using the Enumstatement. The Enum

    statement declares an enumeration and defines the values of its members.The Enum statement can be used at the module class structure

    procedure or block level.

    The syntax for the Enum statement is as follo!s&

    [][accessmodifer ] [Shadows]

    +numenumerationname [Asdatatype ]

    memberlist

    +nd+num

    'here

    attributelist& refers to the list of attributes applied to the variable. )ptional.

    asscessmodifier& specifies !hich code can access these enumerations. )ptional. Values

    can be either of the& *ublic *rotected +riend or *rivate.

  • 7/25/2019 VB.netenum and Constants

    4/6

    Shadows& this makes the enumeration hide a programming element of identical name in

    a base class. )ptional.

    enumerationname& name of the enumeration. ,euired

    datatype& specifies the data type of the enumeration and all its members.

    memberlist& specifies the list of member constants being declared in this statement.

    ,euired.

    Each member in the memberlist has the follo!ing syntax and parts&

    []member name [=initializer ]

    'here

    name& specifies the name of the member. ,euired.

    initializer& value assigned to the enumeration member. )ptional.

    +or example

    +numColors

    red =0

    oran!e =

    yellow =/

    !reen =&

    azure =1

    blue =?

    $iolet =8

    +nd+num

    ExampleThe follo!ing example demonstrates declaration and use of the Enum

    variable Colors&

    2oduleconstants3enum

    +numColors

    red =0

    oran!e =

    yellow =/

  • 7/25/2019 VB.netenum and Constants

    5/6

    !reen =&

    azure =1

    blue =?

    $iolet =8

    +nd+num

    Sub2ain45

    Console":rite%ine4)The Color ed is @ );Colors"red5

    Console":rite%ine4)The Color ellow is @ );Colors"yellow5

    Console":rite%ine4)The Color Blue is @ );Colors"blue5

    Console":rite%ine4)The Color reen is @ );Colors"!reen5

    Console"eadey45

    +ndSub

    +nd2odule

    'hen the above code is compiled and executed it produces the follo!ing

    result&

    TheColoredis@0

    TheColorellowis@/

    TheColorBlueis@?

    TheColorreenis@&

    An enumeration is a user-def ined data type consists o f integral constants and each integral constant

    is give a name. Keyword enumis used to defined enumerated data type.

    enum typeDnameE $alue07 $alue7"""7$alue3 FG

    Here, type_nameis the name of enumerated data type or tag. And value1, value2,....,valueNare

    values of type type_name.

    By default, value1will be equal to 0, value2will be and so on but, the programmer can change the

    default value.

    HH Chan!in! the deault $alue o enum elements

    enum suitE

    club=IG

    diamonds=0IG

  • 7/25/2019 VB.netenum and Constants

    6/6

    hearts=IG

    spades=/G

    FG

    !eclaration of enumerated variableAbove code defines the type of the data but, no any variab le is created. "ar iab le of type enumcan

    be created as#

    enum booleanE

    alseG

    trueG

    FG

    enum boolean checJG

    Here, a variable chec$ is declared which is of type enum boolean.

    %&ample of enumerated typeKinclude