Presentation on theme: "C ONTINUE... V ARIABLE -N AMING C ONVENTIONS When declaring variables, you should be aware of few naming conventions. A variable’s name: Must begin with."— Presentation transcript:
V ARIABLE -N AMING C ONVENTIONS When declaring variables, you should be aware of few naming conventions. A variable’s name: Must begin with a letter Mustn’t exceed 255 characters. Must be unique within its scope Cannot include any special character except “_” (underscore) Cannot include a space
V ARIABLE -N AMING R ULES : A variable names in VB.NET are NOT case sensitive. Variables can be prefixed to indicate their data type (prefixes indicate a variable's data type)
S COPE AND LIFETIME OF VARIABLES Scope = visibility of a variable is the section of the application that can see and manipulate the variable. If a variable is declared within a procedure, only the code in the specific procedure has access to that variable. This variable doesn’t exist for the rest of the application. When the variable’s scope is limited to a procedure, it’s called local.
A CCESS S PECIFIERS Access specifiers let's us specify how a variable, method or a class can be used. The following are the most commonly used one's: Public: Gives variable public access which means that there is no restriction on their accessibility. Private: Gives variable private access which means that they are accessible only within their declaration content.
A CCESS S PECIFIERS.. CONT Protected: Protected access gives a variable accessibility within their own class or a class derived from that class. Static: Makes a variable static which means that the variable will hold the value even the procedure in which they are declared ends. Shared: Declares a variable that can be shared across many instances and which is not associated with a specific instance of a class or structure.
TYPES OF VARIABLES Visual Basic.NET recognizes different categories of variables as shown in the previews Table & the most commonly used include: Numeric Boolean String Character Date
N UMERIC V ARIABLES Note: Note: The data type of your variable can make a difference in the results of the calculations. In our previous discussion of data types, we lumped all numeric data together; however, Visual Basic has several numeric data types.
B OOLEAN V ARIABLES The Boolean data type stores True/False values. Boolean variables are, integers that take the value 1 (for True) and 0 (for False). Actually, any non-zero value is considered True. Boolean variables are declared as: Dim failure As Boolean Boolean variables are used in testing conditions, such as the following: If failure Then MessageBox.Show(“Couldn’t complete the operation”)
S TRING V ARIABLES The String data type stores only text, and string variables are declared with the String type: Dim someText As String Dim someText As String You can assign any text to the variable someTxt. You can store nearly 2 GB of text in a string variable (that’s 2 billion characters and is much more text than you care to read on a computer screen). The following assignments are all valid:
Dim aString As String aString = “MIS is a cool course” aString = “” aString = “111, 000” Assignment number 2 creates an empty string, and the last one creates a string that just happens to contain numeric digits, which are also characters.
T HE E MPTY S TRING The string "", which contains no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement: txtBox.Clear() or the statement txtBox.Text = ""
U SING T EXT B OXES FOR I NPUT AND O UTPUT The contents of a text box is always a string Input example strVar = txtBox.Text Output example txtBox.Text = strVar
D ATA C ONVERSION string Because the content of a text box or a label is always a string, sometimes you must convert the input or output value into another type. Table 2 shows the VB functions that perform data-type conversions.
FunctionConverts Its Argument To: CBoolBoolean CByteByte CCharUnicode character CDateDate CDblDouble CDecDecimal CIntInteger (4-byte integer, int32) CLngLong (8-byte integer, int64) CObjObject CShortShort (2-byte integer, int16) CSngSingle CStrString
E XAMPLE : numVar = CDbl(txtBox.Text) -Converts a String to a Double txtBox.Text = CStr(numVar) -Converts a number to a string
C HARACTER V ARIABLES Character variables store a single Unicode character in two bytes (16 bit). you can use the CChart() function to convert integers to characters and the CInt() function to convert to their equivalent integer values.
D ATE V ARIABLES Date and time values are stored internally in a special format, but you don’t need to know the exact format. They are double-precision numbers: the integer part represents the date and the fractional part represents the time. A variable declared as Date can store both date and time values with a statement like the following:
D ATE V ARIABLES … CONT Dim expiration As Date The following are all valid assignments: expiration = #01/01/2004? expiration = # 1/2/2004 10;26:11 pm# expiration = “July 2, 2004” expiration = Now()