Have you just installed Visual Studio and are ready to begin your first program? Then this is the sample for you. This is a basic example of some basic functions in C# C++ VB or F#. These basic skills are necessary for complex C# VB C++ or F# programming.
Building the Sample
To run this you will need Visual Studio 2013 or Visual Studio 2013 Express installed. They can be downloaded here. To run it open the .sln file then hit f5.
Description
This sample solves the program by creating a small interface in which you can choose to compare two numbers, or solve a mathematical equation with them. This sample explains string variables and integer variables, if equal, greater than, less than, greater than or equal, less than or equal, and not equal statements, and the addition, subtraction, multiplication, and division of numbers. Then you can look at the code and see what has happened. The interface is a collection of radioboxes, asking for operation, textboxes, for the numbers, and a picturebox, to show the relationship between the two numbers.
This is a small example of what happens when you click the add radiobox.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
string x = this.textBox1.Text;
string y = this.textBox2.Text;
if (x == "") //See if x has no value
{
x = "0"; //Set x to 0
}
if (y == "") //See if x has no value
{
y = "0"; //Set y to 0
}
int x_number = Convert.ToInt32(x); //Converts the string x to a number then stores it in x_number
int y_number = Convert.ToInt32(y);
int random = x_number + y_number; //Sets the integer variable random to the value of x_number plus y_number
string random_string = Convert.ToString(random); //Converts the integer random to the string random_string
this.label3.Text = random_string;
}
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
x = Me.textBox1.Text()
y = Me.TextBox2.Text
If x = "" Then 'This sees if x has no value
x = "0" 'This sets x to 0
End If
If y = "" Then 'This sees if x has no value
y = "0" 'This sets y to 0
End If
x_number = Convert.ToInt32(x) 'This converts the string x to a integer then stores it in x_number
y_number = Convert.ToInt32(y)
random = x_number + y_number 'This sets the integer variable random to the value of x_number plus y_number
random_string = Convert.ToString(random) 'This converts the integer random to the string random_string
Me.Label3.Text = random_string
End Sub
case 3: //This is ran when the radiobutton with (HEMNU)3 is pressed
{
std::ostringstream ostr;
GetDlgItemTextA(hwnd, 1, x, 32767);
GetDlgItemTextA(hwnd, 2, y, 32767);
if (x == "") //This sees if x has no value
{
x[0] = '0'; //This sets x to 0
}
if (y == "") //This sees if x has no value
{
y[0] = '0'; //This sets y to 0
}
x_number = atof(x); //This converts the string x to a integer then stores it in x_number
y_number = atof(y);
random = x_number + y_number;
ostr << random;
random_string = ostr.str();
SetDlgItemTextA(hwnd, 13, random_string.c_str());
break;
}
member private this.radioButton1_CheckChanged() = //This method is called when the radiobutton (a button that when pressed makes every other radiobutton in its groupbox not pressed) is changed
let x =
if textBox1.Text = "" then "0" //This sees if x has no value and if it has none, then it sets x to 0
else textBox1.Text
let y =
if textBox2.Text = "" then "0"
else textBox2.Text
let x_number = Convert.ToInt32(x) //This converts the string x to a integer then stores it in x_number
let y_number = Convert.ToInt32(y)
let random = x_number + y_number //This sets the integer variable random to the value of x_number plus y_number
let random_string = Convert.ToString(random) //This converts the integer random to the string random_string
do label3.Text <- random_string
private void radioButton1_CheckedChanged(object sender, EventArgs e) { string x = this.textBox1.Text; string y = this.textBox2.Text; if (x == "") //See if x has no value { x = "0"; //Set x to 0 } if (y == "") //See if x has no value { y = "0"; //Set y to 0 } int x_number = Convert.ToInt32(x); //Converts the string x to a number then stores it in x_number int y_number = Convert.ToInt32(y); int random = x_number + y_number; //Sets the integer variable random to the value of x_number plus y_number string random_string = Convert.ToString(random); //Converts the integer random to the string random_string this.label3.Text = random_string; }
This is a picture of the C# interface
C#
C++
VB
F#
For more information on this sample, please feel free to ask questions on the Q&A. Any feedback will be appreciated.