190 likes | 333 Views
Continue. Variable-Naming Conventions. 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)
E N D
Variable-Naming Conventions 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
Variable-Naming Rules : • 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)
Scope 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.
Access Specifiers Access specifierslet'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.
Access Specifiers .. 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
Numeric Variables • 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.
Boolean Variables 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”)
String Variables The String data type stores only text, and string variables are declared with the String type: 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.
The Empty String 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 = ""
Using Text Boxes for Input and Output • The contents of a text box is always a string • Input example strVar = txtBox.Text • Output example txtBox.Text = strVar
Data Conversion • 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.
Example: • numVar = CDbl(txtBox.Text)-Converts a String to a Double • txtBox.Text = CStr(numVar)-Converts a number to a string
Character Variables • 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.
Date Variables 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:
Date Variables … 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()