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 Byte, (object to Byte, Boolean byte, Date Time to Byte, string to Byte, etc…..) using “Convert Class” in C# (Conversion PART2).

Author: Zunnair
Download Source Code : 871_source.zip

In this article you will learn that how we can convert different data types in Byte 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 patr1 I discussed about Boolean conversion.
Conversion of different data types (date time, string, object, etc) to Byte. For this purpose convert class provides us a function To Byte () with 19 overloaded methods.
String to Byte:
Convert class provides a function which takes string as a parameter and returns Byte. It works when values in string is digits. It throws an exception if string is other than digits.
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 = "12";
            byte b = Convert.ToByte(str);
            txt_Result2.Text = b.ToString();
        }
VB
Private Sub btn_string_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim str As String = "12"
        Dim b As Byte = Convert.ToByte(str)
        txt_Result2.Text = b.ToString()
    End Sub
 This simple code converts the string in byte. You also take this string from user.
 
Date Time to Byte:
Convert class provides a function which takes object of data time class as a parameter and returns Byte.
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;
            byte b = Convert.ToByte(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 Byte = Convert.ToByte(obj)
        txt_Result1.Text = b.ToString()
    End Sub
This simple code converts the Date Time in Byte.
Integer to Byte:
Convert class provides a function which takes integer as a parameter and returns Byte.
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;
            byte b = Convert.ToByte(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 Byte = Convert.ToByte(a)
        txt_Result3.Text = b.ToString()
    End Sub
This simple code converts the integer in Byte.
 
Decimal to Byte:
Convert class provides a function which takes decimal as a parameter and returns Byte.
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.ToByte(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.ToByte(d)
        txt_Result4.Text = b.ToString()
    End Sub
 
This simple code converts the decimal in Byte.
Float to Byte:
Convert class provides a function which takes Float as a parameter and returns Byte.
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.ToByte(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.ToByte(f)
        txt_Result5.Text = b.ToString()
    End Sub
 
This simple code converts the float in Byte.
Boolean to Byte:
Convert class provides a function which takes Boolean as a parameter and returns Byte.
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_Boolean_Click(object sender, EventArgs e)
        {
            bool bb = true;
            byte b = Convert.ToByte(bb);
            txt_Result6.ToString();
        }       
VB
Private Sub btn_Boolean_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim bb As Boolean = True
        Dim b As Byte = Convert.ToByte(bb)
        txt_Result6.ToString()
    End Sub
This simple code converts the Boolean in Byte.
Object to Byte:
Convert class provides a function which takes object type value as a parameter and returns Byte.
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;
            byte b = Convert.ToByte(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 Byte = Convert.ToByte(obj)
        txt_Result7.Text = b.ToString()
    End Sub
This simple code converts the object value in Byte.
Double to Byte:
Convert class provides a function which takes double value as a parameter and returns Byte.
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;
            bool b = Convert.ToByte(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
        Dim b As Boolean = Convert.ToByte(d)
        txt_Result8.Text = b.ToString()
    End Sub
This simple code converts the double value in Byte.
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 Byte using “Convert Class” in C# and VB (.net 2.0).
 
Article Comments
last week our class held a similar talk about this topic and you illustrate something we haven't covered yet, appreciate that.

- Kris

Posted on 10/22/2010 7:54:53 AM by mallorca autovermietung

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< 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).

Disclaimer - Privacy
© 2002-2017 DevASP.net