Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Visual Basic .NET/Variables

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Variables In programming a variable is simply a place to store data. A variable has a name and a data type. In Visual Basic .NET, a variable is declared using the Dim (short for Dimension) statement. Here is the syntax: Dim varName As varType varName is the name of your variable. varType is the data type of the variable. Types include string, integer, double, boolean, etc. For example, to declare an integer named MyInt use: Dim MyInt As Integer By default, the variables case doesn't allow to distinguish them, so myint will automatically be converted in MyInt by the IDE if it's declared like that. On the other hand, to make a program ignore the case in the string values, Option Compare Text should be added. Option Compare Text ' By commenting this line the result becomes False Module Module1 Sub Main() Dim string1 As String = "a" Dim string2 As String = "A" MsgBox(string1 = string2) End Sub End Module Data Types Data Types define the type of data that a variable can store. Some variables store numbers, others store names. The built-in VB.NET type aliases and their equivalent .NET Framework types follow: Integers VB Alias.NET TypeSizeRangeSByteSystem.SByte8 bits (1 byte)-128 to 127ByteSystem.Byte8 bits (1 byte)0 to 255ShortSystem.Int1616 bits (2 bytes)-32,768 to 32,767UShortSystem.UInt1616 bits (2 bytes)0 to 65,535IntegerSystem.Int3232 bits (4 bytes)-2,147,483,648 to 2,147,483,647UIntegerSystem.UInt3232 bits (4 bytes)0 to 4,294,967,295LongSystem.Int6464 bits (8 bytes)-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807ULongSystem.UInt6464 bits (8 bytes)0 to 18,446,744,073,709,551,615 Floating-point VB Alias.NET TypeSizePrecisionRangeSingleSystem.Single32 bits (4 bytes)7 digits1.5 x 10-45 to 3.4 x 1038DoubleSystem.Double64 bits (8 bytes)15-16 digits5.0 x 10-324 to 1.7 x 10308DecimalSystem.Decimal128 bits (16 bytes)28-29 decimal places1.0 x 10-28 to 7.9 x 1028 Other pre-defined types VB Alias.NET TypeSize (bits)RangeCharSystem.Char16 bits (2 bytes)One Unicode symbol in the range of 0 to 65,535.BooleanSystem.Boolean32 bits (4 bytes)True or FalseObjectSystem.Object32/64 bits (4/8 bytes)Platform dependent (a reference to an object).DateSystem.DateTime64 bits (8 bytes)January 1, 0001 12:00:00 AM to December 31, 9999 11:59:59 PMStringSystem.String80 + [16 * Length] bits (10 + [2 * Length] bytes)A Unicode string with a maximum length of 2,147,483,647 characters. Using Variables Assigning Values A value is the data contained in a variable. To assign a value to a variable that is already declared, use an equal sign. Suffix for Literals Integral literals, such as 42 and 1000, are of type Integer by default. String and character literals, such as "Hello World" and "À", are of type String by default. To specify the type for a literal, suffixes are used. The suffixes are appended immediately after the literals, in the manner <literal><suffix>, without any whitespace between. Examples For string and char variables, use double quotes around value: strMyVariable = "The String" chrMyVariable = "À" For date variables, use hashes/pounds around the value, in the format #<month>/<day>/<year> <hour>:<minute>:<second> <AM|PM>#: dtMyVariable = #7/4/1776 12:01:50 PM# For all others, remove the quotes and hashes/pounds: bytMyVariable = 1 sbytMyVariable = -2 shrtMyVariable = 10S ushrtMyVariable = 10US intMyVariable = 100 uintMyVariable = 100UI lngMyVariable = 1000L ulngMyVariable = 1000UL sngMyVariable = 1.234F dblMyVariable = 1.567R decMyVariable = 1234567.89D boolMyVariable = True objMyObject = New Object Initial Value To assign a variable the value of another variable, simply replace the value on the right side of the equal sign with the name of the variable that holds the desired data. You can also assign a value to a variable in the declaration itself. Dim myVariable As String = "StringValue" Important: Visual Basic always assigns the value of the right variable to the left variable. The variable on the left takes the value of the right variable. The variable on the right does not change. Constants Constants are like variables that don't change. They take the place of values that you would not like to type over and over. Constants are declared using the keyword "Const". Their values are defined in their declaration - they also use data types. Here is the syntax: Const cnstMyConstant As String = "The very long string" Here is an example: Const cnstPi As Single = 3.14159265F Constants are very useful when you need to type the same numbers/strings/etc many times. For example, to convert from radians to degrees you can type 180/Pi constant: Const cnstRadToDeg As Single = 57,29579143 And use it like this: Degrees = Radians / cnstRadToDeg This constant is useful in functions like Sin, Cos, Tan, Arctan, etc. de:Visual Basic .NET: Variablen fr:Visual Basic .NET/Variables