Visual basic what is a variable




















Form Region " Windows Form Designer generated code " End Region End Class. In Visual Studio, you can use the Object Browser to see a list of all of the constants in an enumeration. If you are using Visual Basic. From the list of objects, select DataTypeConstants. Add MyVariable. Note that. The previous program was a bit boring, let's try to respond to user input somehow. Let's write a program named Parrot.

You can probably guess what it will do - it will repeat twice what the user has written. We haven't tried to read anything from the console yet, but it's very simple.

There is the ReadLine method that returns a string from the console. Let's write the following code:. It's a little more fun, right? The first two lines are self-explanatory. On the third line, we declare a string input. Input will contain the return value of the ReadLine method, i.

To make the code more understandable, we create another string variable for output. The interesting part of the code is when we assign the value to output , we do a string concatenation. As you can see, it doesn't matter whether it's a variable or it's an explicitly declared string in quotation marks in our source code. We assign the input to the variable, then a comma followed by a space, then input again and finally an exclamation mark.

Then the program will display this variable, and wait for the user to press any key before terminating. We could shorten the program in many other ways, but generally, it's better to use more variables and focus on clarity and readability than altering the code until we forget what the program was even supposed to do.

Since we haven't worked with real numbers yet, let's program a simple calculator. It'll be very easy. The input will consist of two numbers, and the program will display the results of addition, subtraction, multiplication, and division.

Note: The decimal separator depends on your regional settings, for the English-speaking world it would be a dot. At the end, let's at least mention type casting. NET performs that automatically, however, you can also force it manually.

Type casting means converting a value to some other data type. Consider we have an Integer - 10 and we want a real number from it Double - We can do it like this:.

As you can see, we perform type cast using functions which don't belong to any class and their name starts with C and is followed usually by the first 3 letters of the data type we want to cast to. They all access an Object as parameter meaning we can pass anything to them.

The most used type-cast functions are:. All of the programs are available for download in the attachment below. You should definitely try to create some programs similar to these, seeing as how you already have the knowledge necessary to do so! In the following exercise, Solved tasks for Visual Basic. Boolean values are used in conditional statements to decide whether particular parts of Visual Basic code should be executed or not see Visual Basic Flow Control for more details on conditional statements.

A Byte variable holds a positive number in the range 0 to Because of the limited range, Byte variables should be used with caution. An attempt to assign a negative value or a value greater than to a Visual Basic Byte variable will result in an error. The Visual Basic Date variable type holds a date and time value.

Visual Basic provides a number of mechanisms for working with Date variables such as calculating a date six months from today's date. The Decimal variable type can store either a whole number or a decimal number up to 29 decimal places. When working with whole numbers, however, it is recommended that the Integer variable type be used as this is more memory efficient than the Decimal variable type.

The Visual Basic Double variable is used to store either very large numbers, or small numbers requiring more than 28 decimal places. If you fail to do so, the program will run into an error. Variables are usually declared in the general section of the code windows using the Dim statement. If you want to declare more variables, you can declare them in separate lines or you may also combine them in one line, separating each variable with a comma, as follows:.

Now enter the following code:. In this example, we create a simple calculator where the user enter his name, two numbers and click the Calculate button to compute the addition of the two numbers.

It also display the date. There are three data types here, String, Integer and Date. We also use the Format function to display the current date of computation. Running the program and clicking on the Calculate button produces the following output as shown in Figure 9. After declaring various variables using the Dim statements, we can assign values to those variables.



0コメント

  • 1000 / 1000