Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Conversion of different data types in Boolean, (Date Time to Boolean, string to Boolean, object to Boolean, byte to Boolean, etc…..) using “Convert Class” in C# (Conversion PART1).

Author: Zunnair
Download Source Code : 870_source.zip

In this article you will learn that how we can convert different data types in Boolean using “Convert Class” in C# and VB (.net 2.0).

 

“Convert class” is a class which dot net provides us to perform type casting from different data type to different data types.
In this article we will start from Boolean. Conversion of different data types (date time, string, object, etc) to Boolean. For this purpose convert class provides us a function To Boolean () with 17 overloaded methods.
String to Boolean:
Convert class provides a function which takes string as a parameter and returns Boolean (True/False). It works when sentence in string is “True” or “False”. It converts “true” string in true and “False” String in false. And it throws an exception if string is other than true or false.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_String_Click(object sender, EventArgs e)
        {
            string str = "True";
           bool b= Convert.ToBoolean(str);
           txt_Result1.Text= b.ToString();
        }        
VB
Private Sub btn_String_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim str As String = "True"
        Dim b As Boolean = Convert.ToBoolean(str)
        txt_Result1.Text = b.ToString()
    End Sub
This simple code converts the string in Boolean. You also take this string from user.
 
Date Time to Boolean:
Convert class provides a function which takes object of data time class as a parameter and returns Boolean (True/False).
But there is an interesting thing that it will not convert and will throw an exception always.
To demonstrate make a window application. Drag one text box and one button on the form. Take system time and date. Press button. You will get result in text box.
Now write the following code on button click event
C#
private void btn_Date_Click(object sender, EventArgs e)
        {
            DateTime obj = new DateTime();
            obj = DateTime.Now;
            bool b = Convert.ToBoolean(obj);
            txt_Result1.Text = b.ToString();
        }
VB
Private Sub btn_Date_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New DateTime()
        obj = DateTime.Now
        Dim b As Boolean = Convert.ToBoolean(obj)
        txt_Result1.Text = b.ToString()
    End Sub
This simple code converts the Date Time in Boolean.
Integer to Boolean:
Convert class provides a function which takes integer as a parameter and returns Boolean (True/False). It returns true on every value except zero.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_int_Click(object sender, EventArgs e)
        {
            int a = 5;
            bool b = Convert.ToBoolean(a);
            txt_Result3.Text = b.ToString();
        }
VB
Private Sub btn_int_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim a As Integer = 5
        Dim b As Boolean = Convert.ToBoolean(a)
        txt_Result3.Text = b.ToString()
    End Sub
This simple code converts the integer in Boolean.
 
Decimal to Boolean:
Convert class provides a function which takes decimal as a parameter and returns Boolean (True/False). It returns true on every value except zero.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_Decimal_Click(object sender, EventArgs e)
        {
            decimal d = 10;
            bool b = Convert.ToBoolean(d);
            txt_Result4.Text = b.ToString();
        }
VB
Private Sub btn_Decimal_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim d As Decimal = 10
        Dim b As Boolean = Convert.ToBoolean(d)
        txt_Result4.Text = b.ToString()
    End Sub
 
This simple code converts the decimal in Boolean.
Float to Boolean:
Convert class provides a function which takes Float as a parameter and returns Boolean (True/False). It returns true on every value except zero.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_float_Click(object sender, EventArgs e)
        {
            float f = 3;
            bool b = Convert.ToBoolean(f);
            txt_Result5.Text = b.ToString();
        }
VB
Private Sub btn_float_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim f As Single = 3
        Dim b As Boolean = Convert.ToBoolean(f)
        txt_Result5.Text = b.ToString()
    End Sub
 
This simple code converts the float in Boolean.
Byte to Boolean:
Convert class provides a function which takes Byte as a parameter and returns Boolean (True/False). Byte stores any 8bit value.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_Byte_Click(object sender, EventArgs e)
        {
            byte bb=new byte();
            bb=Convert.ToByte(1);
            bool b = Convert.ToBoolean(bb);
            txt_Result6.ToString();
        }
VB
Private Sub btn_Byte_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim bb As New Byte()
        bb = Convert.ToByte(1)
        Dim b As Boolean = Convert.ToBoolean(bb)
        txt_Result6.ToString()
    End Sub
This simple code converts the byte in Boolean.
Object to Boolean:
Convert class provides a function which takes object type value as a parameter and returns Boolean (True/False).
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_object_Click(object sender, EventArgs e)
        {
            object obj = new object();
            obj = 1;
            bool b = Convert.ToBoolean(obj);
           txt_Result7.Text = b.ToString();
        }       
VB
Private Sub btn_object_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim obj As New Object()
        obj = 1
        Dim b As Boolean = Convert.ToBoolean(obj)
        txt_Result7.Text = b.ToString()
    End Sub
This simple code converts the object value in Boolean.
Double to Boolean:
Convert class provides a function which takes double value as a parameter and returns Boolean (True/False). It returns true for any value except zero.
To demonstrate make a window application. Drag one text box and one button on the form. Press button. You will get result in text box.
C#
private void btn_double_Click(object sender, EventArgs e)
        {
            double d = 12.5;
            bool b = Convert.ToBoolean(d);
            txt_Result8.Text = b.ToString();
        }       
VB
Private Sub btn_double_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim d As Double = 12.5
        Dim b As Boolean = Convert.ToBoolean(d)
        txt_Result8.Text = b.ToString()
    End Sub
This simple code converts the double value in Boolean.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
 
            this.Text = "Devasp.net Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
        Me.Text = "Devasp.net Application"
 End Sub
 
This simple article tells that how we can convert different data types in Boolean using “Convert Class” in C# and VB (.net 2.0).

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Check that “”CLR has started shutting down our process and how to get current stack trace information using “Environment Class” in C# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net