Visual C#.NET: Windows Forms Programming with C# by mohmmed moaml & Beerbohm Max

Visual C#.NET: Windows Forms Programming with C# by mohmmed moaml & Beerbohm Max

Author:mohmmed, moaml & Beerbohm, Max [mohmmed, moaml]
Language: eng
Format: epub
Published: 2019-11-12T16:00:00+00:00


A simple program to add two integers

Let us now work on a more practical program. We will write a program that combines two integers and shows the result to the user. Follow the same steps that we performed in the previous program to create a new program named SumTwoNumbers, copy the following code contents to the Program.cs file:

using System;

namespace SumTwoNumbers

{

class Program

{

static void Main (string [] args)

{

int a;

int b;

int c;

a = 3;

b = 4;

c = a + b;

Console.WriteLine ("3 plus 4 equals:" + c.ToString ());

}

}

}

This simple program introduces the concept of declaring and dealing with variables. In lines 9 through 11 we have declared three variables a, b, and c of type int.

Each variable must be declared in C Sharp before using it in the program. Note that a variable is declared by its type and hence its name. The int variable can accommodate any integer (without a decimal point) between -2147,483,648 and 2,147,483,647. Note that we have assigned values ​​3 and 4 to variables a and b respectively, in lines 13 and 14. We are adding and assigning the variable c on line 16. On line 18, we display a message to the user.

A variable can be assigned a value directly when declared. For example, values ​​3 and 4 can be assigned to variables a and b respectively, when declared:

int a = 3;

int b = 4;

A single statement statement can also be used to declare several variables at the same time. For example, the previous variables a, b and c could be declared in one software statement as follows:

int a, b, c;

In fact, there are two types of variables supported by DotNet. Value types and reference types. We'll talk about them later.

The int type is a value type. At least one assignment to a value variable must be performed before reading it. Otherwise we will get an error. Try deleting the code statement on line 16 responsible for assigning the value of the sum to the variable c. Run the program and you will get the following error:

Use of unassigned local variable 'c'

This is because we tried to read variable c in line 18 without assigning any value to it.

The code statement on line 18 is responsible for displaying the message to the user as mentioned above. You'll notice that we've passed the following expression to the WriteLine function:

"3 plus 4 equals:" + c.ToString ()

A programmatic expression is a concept found in almost all programming languages, and is simply the result of a software operation using one or more operator operators, or a call to a function or a combination between them. We call expression processing by expression evaluation. The factor used here is the combining factor (+), and you may be surprised why I call it the combining factor, although it is similar to the normal addition factor that combines two numbers together (see line 16). This is due to the type of operands on both ends. You can easily notice that the left operand is the text:

"3 plus 4 equals:"

The right coefficient is:

c.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.