Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
All programming languages support the concept of a variable. A place to hold data in memory during the execution of a bit of programming code. I'm going to describe how to declare variables in both C# and Visual Basic, and talk about data types. How to designate what type of data a particular variable can hold. The .NET Framework is strongly typed, meaning that when you declare a variable, you always declare what kind of data it holds. The syntax that you use for this is different between the two languages C# and Visual Basic, but the concept is the same.
I'll start with the file ProgrammingCSharp.aspx. I'll open the file, and then select File > Save ProgrammingCSharp.aspx as and I'll name the new file SimpleVarsCSharp.aspx. Make sure you have added the file extension .ASPX. Visual Web Developer won't do it for you. When you declare a variable, you can place it either inside a function or outside a function. When you place a variable inside the function, the variable expires, that is, it disappears from memory, when the function is finished executing.
In C#, if you want to declare a variable that's local to the function, you place the cursor inside the function. Then you start with the variable's data type. I'll start with String, meaning this is a string variable, and I'll name the variable localVar. When you type in the variable at first, you might see a squiggly line as shown on my screen. The squiggly line is an indication of a possible error. I'll move the cursor over the variable name and see the message, The variable "localVar" is declared but never used. This is just a warning from the Visual Web Developer Editor that it has detected the potential programming problem.
If you know you are going to use the variable, you can ignore the error. If you simply want to declare the variable, but not use it yet, put in a semicolon. On the other hand, if you want to set the variable's value, continue with an equals operator and then set its initial value. In this case, I'm setting it to a literal string of My local variable. So when I put in the semicolon at the end of the line that completes the statement. Now, I'm going to output the value of the variable using the output function and I'll pass in localVar.
Notice that Visual Web Developer already knows the name of the variable. So that's my code. I'll save the changes and run the page. I'll click Run Code and I'll see the value of my local variable displayed in the text box. I click the button and run the server-side code as many times as I want to, and then I'll clear the console. Now, I'll do the same thing in Visual Basic. I'll open the file ProgrammingVB.aspx, and I'll save it under a new name, selecting File > Save As, and I'll name this file SimpleVarVB.aspx.
The rules for scoping, that is for variable, visibility, and lifetime, are the same in both languages. When you declare the variable inside a function, the variable is local to the function and expires when the function is finished. In Visual Basic, you declare this sort of variable using a keyword Dim. Dim stands for dimension, and it declares the variable using what's called the default scope. Within a function, the variable is being declared as local to the function.
In Visual Basic you then follow with the variable name. I'll once again name the variable localVar, and then you set its data type using the As keyword. I'll put in the word As and then the data type of string. As with C#, you can now set the initial value of the variable in the same statement, or you can leave the variable for later use. I'll set it to initial value once again as My local variable. And then on the next line, I'll output the value of the variable by passing it into the output function. I'll save my changes and run the page, selecting Debug > Start Without Debugging. I'll click the button to run the code, and I'll see that I'm executing the function successfully. Then, I'll clear the console, and I'll close the browser.
So once again, the rules are the same for the two languages. When you declare the variable within a function, it's local to the function. You use the Dim for dimension keyword in Visual Basic, whereas in C#, you simply declare the variable by putting in its data type and then the variable name. You declare data types in either C# or Visual Basic, using distinct syntax styles. The underlying data type however is the same. The system data type is listed in the left column of this chart. For example, the string value is something called system.string and it actually represents a class definition in the .NET Framework.
In C#, you declare the data type using the word String all lowercase, and in Visual Basic the word String with an uppercase initial character. Boolean values, which are used to represent true or false values, again are represented by the class system.boolean. In C#, you declare that data type with bool, all lowercase, and in Visual Basic with the word Boolean with an initial uppercase character. There are a number of numeric data types available in the Framework. Here are the most commonly used three data types. An integer defined as a 32-bit integer is represented in the system as int 32. In C# as lowercase int and in Visual Basic as Integer with an uppercase I.
A floating number is represented as floating C#, and single in VB, and a double number which has greater precision is represented in all lowercase in C#, and with an initial uppercase character in Visual Basic. There are many other built-in data types in the .NET Framework, and each has their equivalent syntax in C# and in Visual Basic. Check the .NET documentation for more complete examples.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101478 Viewers
61 Video lessons · 88242 Viewers
71 Video lessons · 72114 Viewers
56 Video lessons · 103875 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.