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
 

Buffer Class Operations in VB and C# (.net 2.0).

Author: Zunnair
Download Source Code : 915_control.zip

In this simple article you will learn that how we can do different array operations using buffer class in VB and C# .net 2.0.

 

Buffer:
Buffer is used to do different array operations. Different type of arrays can be manipulated by this buffer class. Buffer class provides static functions for different operations.
Block Copy:
To copy number of bytes from one array (source) to the other array (destination) and from particular offset in source array to the particular offset in destination array. For this purpose buffer class provides us a static function named block copy. Function takes source array from where bytes will be copied, offset from which index copy start, destination array where bytes will be copied, offset of destination array and the count which decides the number of bytes should be copied from source array to destination array, as parameter and returns nothing.
To demonstrate make a window application. Drag three text boxes, list box and one button on form.
Now write the following code on Button click event:
C#
private void btn_Copy_Click(object sender, EventArgs e)
        {
            int Soff=0;
            int Doff=0;
            int Count=0;
            string str;
            string str1;
            string str2;
            str=txt_SourceOffset.Text;
            str1=txt_DestinationOffset.Text;
            str2=txt_count.Text;
            Soff = Convert.ToInt16(str);
            Doff = Convert.ToInt16(str1);
            Count = Convert.ToInt16(str2);
            Buffer.BlockCopy(arr1, Soff, arr2, Doff, Count);
            MessageBox.Show("Copied");
            for (int i = 0; i < Count/4; i++)
                listBox1.Items.Add(arr2.GetValue(i));
        }
VB
Private Sub btn_Copy_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim Soff As Integer = 0
        Dim Doff As Integer = 0
        Dim Count As Integer = 0
        Dim str As String
        Dim str1 As String
        Dim str2 As String
        str = txt_SourceOffset.Text
        str1 = txt_DestinationOffset.Text
        str2 = txt_count.Text
        Soff = Convert.ToInt16(str)
        Doff = Convert.ToInt16(str1)
        Count = Convert.ToInt16(str2)
        Buffer.BlockCopy(arr1, Soff, arr2, Doff, Count)
        MessageBox.Show("Copied")
        For i As Integer = 0 To Count / 4 - 1
            listBox1.Items.Add(arr2.GetValue(i))
        Next
    End Sub
This simple code copies the number of bytes from source to destination array.
Get Length:
To get the total number of bytes in array, buffer class provides us a function named byte length. Function takes array class object as parameter and returns integer which represents the number of bytes stored in array.
To demonstrate make a window application. Drag one button on form.
Now write the following code on Button click event:
C#
private void btn_Length_Click(object sender, EventArgs e)
        {
            int length;
            length = Buffer.ByteLength(arr1);
            string str = length.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub btn_Length_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim length As Integer
        length = Buffer.ByteLength(arr1)
        Dim str As String = length.ToString()
        MessageBox.Show(str)
    End Sub
This simple code will get the number of bytes in an array.
 
Set Byte:
To set a particular byte value at particular index, buffer class provides a function named set byte. Function takes array class object, index and byte value. Function returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on form.
Now write the following code on Button click event:
C#
private void btn_SetByte_Click(object sender, EventArgs e)
        {
            int index;
            byte b;
            string str;
            string str2;
            str = txt_Index.Text;
            str2 = txt_Value.Text;
            index = Convert.ToInt16(str);
            b = Convert.ToByte(str2);
            Buffer.SetByte(arr1, index, b);
            MessageBox.Show("Value Set");
            for (int i = 0; i < 5; i++)
                listBox1.Items.Add(arr1.GetValue(i));
        }
VB
Private Sub btn_SetByte_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim index As Integer
        Dim b As Byte
        Dim str As String
        Dim str2 As String
        str = txt_Index.Text
        str2 = txt_Value.Text
        index = Convert.ToInt16(str)
        b = Convert.ToByte(str2)
        Buffer.SetByte(arr1, index, b)
        MessageBox.Show("Value Set")
        For i As Integer = 0 To 4
            listBox1.Items.Add(arr1.GetValue(i))
        Next
    End Sub
This is the simple code to set the byte value of a particular index.
Get Byte:
To get the byte value of a particular index of array, dot net provides us a function named get byte. Function takes index and array class object as parameter and returns byte.
To demonstrate make a window application. Drag one text box and one button on form.
Now write the following code on Button click event:
C#
private void btn_GetByte_Click(object sender, EventArgs e)
        {
            byte b;
            string str;
            int index;
            str = txt_GetIndexByte.Text;
            index = Convert.ToInt16(str);
            b = Buffer.GetByte(arr1, index);
            str = b.ToString();
            MessageBox.Show(str);
        }
VB
Private Sub btn_GetByte_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim b As Byte
        Dim str As String
        Dim index As Integer
        str = txt_GetIndexByte.Text
        index = Convert.ToInt16(str)
        b = Buffer.GetByte(arr1, index)
        str = b.ToString()
        MessageBox.Show(str)
    End Sub
This is simple code to get the value of a particular index.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Buffer Application";
            arr1.SetValue(10, 0);
            arr1.SetValue(20, 1);
            arr1.SetValue(30, 2);
            arr1.SetValue(40, 3);
            arr1.SetValue(50, 4);
            for (int i = 0; i <5 ; i++)
                listBox1.Items.Add(arr1.GetValue(i));
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Buffer Application"
  arr1.SetValue(10, 0)
        arr1.SetValue(20, 1)
        arr1.SetValue(30, 2)
        arr1.SetValue(40, 3)
        arr1.SetValue(50, 4)
        For i As Integer = 0 To 4
            listBox1.Items.Add(arr1.GetValue(i))
        Next
    End Sub
This simple article tells that how we can do different array operations using buffer class in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Queue operations (clear, count, Trim, find, Copy to array) in VB and C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net