DataType Convertion In C#.net


To Convert the Base Data Type To an other Base Data Type,we use “Convert” class and its functions the target Data Type,Check the below example


int i;
 for (i = 0; i < 5; i++)
 {
 MessageBox.Show(Convert.ToString(i), "Integer To String", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }

Description

we use one integer type variable “i”.and use “for” loop to increment the value of “i” and display the message box.

Convert.ToString(i),is responsible to convert the integer value to string, and message box is only show the string value so i use the Convert.ToString(i) inside the message box.

The above Example is convert the base data type Integer to other base data type String.

It is not only convert the integer data type to string, but its able to convert any base data type to other base data type.

Advertisements

Check Message Box Result


To check the message box result use the Key word “DialogResult” and its option after the giving dot “.”  in if condition The sample code is:


if (DialogResult.Yes == MessageBox.Show("Hello word", "hello", MessageBoxButtons.YesNo, MessageBoxIcon.Question))

{

MessageBox.Show("you press 'yes' button.", "hello", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

else

{

MessageBox.Show("you press 'no' button.", "hello", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

Description

Above code first message box will display with the message of “hello word”, and the title of the message box is “hello” it contain two button yes and no.

We using the if condition to check the result which key user press if it is yes or no,it was check by DialogResult.yes if it was  yes the condition is true and display another message box with the message “you press yes button.” Title is “hello”.
If the IF condition is false control go to else part and display the other message box with the message “you press no button.”

MessageBox.show() function is use to display the message box. the first field of messagebox is use to display the message and the second field is use to display the title of message box and third field is use to set the buttons,which type of button you want you can select it from list and fourth field is icon field of message box by using it you can give icon to your message box and last field is default button by using this you give the default selection to your button.