1 / 19

Continue ..

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)

kuper
Download Presentation

Continue ..

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Continue ..

  2. 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

  3. 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)

  4. 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.

  5. 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.

  6. 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.

  7. 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

  8. 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.

  9. 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”)

  10. 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:

  11. 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.

  12. 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 = ""

  13. 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 

  14. 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. 

  15. Example: • numVar = CDbl(txtBox.Text)-Converts a String to a Double • txtBox.Text = CStr(numVar)-Converts a number to a string

  16. 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.

  17. 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:

  18. 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()  

More Related