VB.NET

Difference between int, Int16, Int32 and Int64


In the learning phase developer are not much aware of the difference between primitive, FCL (framework class library), reference, and value types. This cause bugs and performance issues into the code. In this article, I would like to expose the different behavior of integer type.

int

  1. It is a primitive data type defined in C#.
  2. It is mapped to Int32 of FCL type.
  3. It is a value type and represent System.Int32 struct.
  4. It is signed and takes 32 bits.
  5. It has minimum -2147483648 and maximum +2147483647 capacity.

Int16

  1. It is a FCL type.
  2. In C#, short is mapped to Int16.
  3. It is a value type and represent System.Int16 struct.
  4. It is signed and takes 16 bits.
  5. It has minimum -32768 and maximum +32767 capacity.

Int32

  1. It is a FCL type.
  2. In C#, int is mapped to Int32.
  3. It is a value type and represent System.Int32 struct.
  4. It is signed and takes 32 bits.
  5. It has minimum -2147483648 and maximum +2147483647 capacity.

Int64

  1. It is a FCL type.
  2. In C#, long is mapped to Int64.
  3. It is a value type and represent System.Int64 struct.
  4. It is signed and takes 64 bits.
  5. It has minimum –9,223,372,036,854,775,808 and maximum 9,223,372,036,854,775,807 capacity.

Note

  1. A number of developers think that int represents a 32-bit integer when the application is running on a 32-bit OS and it represents a 64-bit integer when the application is running on a 64-bit OS. This is absolutely wrong.
  2. In C# int is a primitive data type and it always mapped to System.Int32 whether the OS is 32-bit or 64-bit.
Advertisements

Swapping of 2 Numbers without using 3rd Variable


In this post we will see how to swap two numbers without using third variable. Check below sample codes.

1. Using (+,-)

a = a+b = 4+5 = 9     // Should Not Overflow
b = a-b = 9-5 = 4
a = a-b = 9-4 = 5

2. Using (*,/)

a = a*b = 4*5 = 20    // Should Not Overflow
b = a/b = 20/5 = 4    // Should Not Overflow and Should Not be Irrational Number
a = a/b = 20/4 = 5    // Should Not Overflow and Should Not be Irrational Number

VB.NET Example :

Module SwappingDemo
    Sub main()
        Dim a, b As Integer
        Console.Write("Enter the values of a and b:")
        a = CInt(Console.ReadLine())
        b = CInt(Console.ReadLine())
        Console.WriteLine("Before swapping values of a=" + a.ToString() + " b=" + b.ToString())
        a = a + b
        b = a - b
        a = a - b
        Console.WriteLine("After swapping values of a=" + a.ToString() + " b=" + b.ToString())
        Console.ReadLine()
    End Sub
End Module

C#.NET Example

static class SwappingDemo
{
	public static void main()
	{
		int a = 0;
		int b = 0;
		Console.Write("Enter the values of a and b:");
		a = Convert.ToInt32(Console.ReadLine());
		b = Convert.ToInt32(Console.ReadLine());
		Console.WriteLine("Before swapping values of a=" + a.ToString() + " b=" + b.ToString());
		a = a + b;
		b = a - b;
		a = a - b;
		Console.WriteLine("After swapping values of a=" + a.ToString() + " b=" + b.ToString());
		Console.ReadLine();
	}
}

Value Type and Reference Types


typesinnetThe Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are stored in the stack.

Value Types

value_typeA Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Predefined datatypes, structures, enums are also value types, and work in the same way. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can’t access the stack.

Examples : int, enum, structs.  

(more…)

Glass Style Button With Dropdown MenuList


MBGlassButton_Demo.JPG

Introduction

Why another Button? The standard Button is too limited in functionality and I couldn’t find a custom control written that did all that I wanted. This is a User Control with lots of properties and versatility. It is simple to use, just drop it on the form, adjust the design time properties, and use it like the normal Button.

Background

MBGlassButton is a Button which inherits all the properties of a simple Button control. I added some extra functionalities in MBGlassButton like Glow, Split, Highlight, etc. The language used is VB.NET.

Control Properties

Here is the list of properties available in MBGlassButton:

  • Arrow: This property is used to set the arrow on MBGlassButton.
  • BaseColor: This property is used to set the Base Color of MBGlassButton.
  • BaseStrokeColor: This property is used to set the Base Stroke color of MBGlassButton.
  • OnColor: This property is used to set the on color of MBGlassButton.
  • OnStrokeColor: This property is used to set the On Stroke color of MBGlassButton.
  • PressColor: This property is used to set the Press Color of MBGlassButton.
  • PressStrokeColor: This property is used to set the Press Stroke Color of MBGlassButton.
  • ShowBase: This property is used to Show the Glow on MBGlassButton.
  • Radius: This property is used to set the corner Radius of MBGlassButton.
  • SplitButton: This property is used to Split MBGlassButton into two parts.
  • SplitDistance: This property is used to set the Split Distance of MBGlassButton.
  • SplitLocation: This property is used to Split MBGlassButton at a specific location.

(more…)

How to export DataGridView Data to Excel


In this topic we will learn how to export your DataGridView data to Excel without using any excel component or Microsoft Office Excel. Simply add this class in your application.

#Region "   MBExport Class Header"
'********************************************************************************
'  Module Name :  MBExport.vb
'  Project     :  Export DataGridView To Excel
'  Created by  :  Manoj Kishor Bhoir
'  Created on  :  06/02/2014  04:25
'  
'  Copyright © Manoj Kishor Bhoir
'  
'  <Description of the file>
'  All other rights reserved.
'  
'  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
'  EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
'  WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'*********************************************************************************
#End Region

#Region "   Imports"
Imports System.Windows.Forms
Imports System.IO
#End Region

#Region "   MBExport Class"
Namespace Helper
    '''<summary>
    ''' Represents all activities required for Export Data in different formats.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MBExport
        '''<summary>
        ''' Export Data from data grid to Excel file.
        ''' </summary>
        ''' <param name="FilePath">Excel file name with path as String</param>
        ''' <param name="DataGrid">Data Grid to export in Excel</param>
        ''' <remarks></remarks>
        Public Shared Sub ExportToExcel(ByVal FilePath As String, ByVal DataGrid As DataGridView)
            ExportToExcel(FilePath, DataGrid, "Sheet1")
        End Sub
        '''<summary>
        ''' Export Data from data grid to Excel file.
        ''' </summary>
        ''' <param name="FilePath">Excel file name with path as String</param>
        ''' <param name="DataGrid">Data Grid to export in Excel</param>
        ''' <param name="SheetName">Excel Sheet Name as String</param>
        ''' <remarks></remarks>
        Public Shared Sub ExportToExcel(ByVal FilePath As String, ByVal DataGrid As DataGridView, ByVal SheetName As String)
            Try
                Using _mFileStream As StreamWriter = New StreamWriter(FilePath, False)
                    _mFileStream.WriteLine("<?xml version=""1.0""?>")
                    _mFileStream.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
                    _mFileStream.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
                    _mFileStream.WriteLine("    <ss:Styles>")
                    _mFileStream.WriteLine("        <ss:Style ss:ID=""1"">")
                    _mFileStream.WriteLine("           <ss:Font ss:Bold=""1""/>")
                    _mFileStream.WriteLine("           <ss:FontName=""Courier New""/>")
                    _mFileStream.WriteLine("        </ss:Style>")
                    _mFileStream.WriteLine("    </ss:Styles>")
                    _mFileStream.WriteLine("    <ss:Worksheet ss:Name=""" & SheetName & """>")
                    _mFileStream.WriteLine("        <ss:Table>")
                    For x As Integer = 0 To DataGrid.Columns.Count - 1
                        _mFileStream.WriteLine("            <ss:Column ss:Width=""{0}""/>", DataGrid.Columns.Item(x).Width)
                    Next
                    _mFileStream.WriteLine("            <ss:Row ss:StyleID=""1"">")
                    For i As Integer = 0 To DataGrid.Columns.Count - 1
                        _mFileStream.WriteLine("                <ss:Cell>")
                        _mFileStream.WriteLine(String.Format("                   <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Columns.Item(i).HeaderText))
                        _mFileStream.WriteLine("</ss:Cell>")
                    Next
                    _mFileStream.WriteLine("            </ss:Row>")
                    For intRow As Integer = 0 To DataGrid.RowCount - 2
                        _mFileStream.WriteLine(String.Format("            <ss:Row ss:Height =""{0}"">", DataGrid.Rows(intRow).Height))
                        For intCol As Integer = 0 To DataGrid.Columns.Count - 1
                            _mFileStream.WriteLine("                <ss:Cell>")
                            _mFileStream.WriteLine(String.Format("                   <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGrid.Item(intCol, intRow).Value.ToString))
                            _mFileStream.WriteLine("                </ss:Cell>")
                        Next
                        _mFileStream.WriteLine("            </ss:Row>")
                    Next
                    _mFileStream.WriteLine("        </ss:Table>")
                    _mFileStream.WriteLine("    </ss:Worksheet>")
                    _mFileStream.WriteLine("</ss:Workbook>")
                    _mFileStream.Close()
                    _mFileStream.Dispose()
                End Using
            Catch ex As Exception

            End Try
        End Sub
    End Class
End Namespace
#End Region

How to use :

MBExport.ExportToExcel("File Path to Save Excel", "DataGridViewControl")
'OR
MBExport.ExportToExcel("File Path to Save Excel", "DataGridViewControl", "SheetName")

TreeView Explorer Control For Windows Applications


Introduction

What is the MBTreeViewExplorer? This is a simple TreeView derived control that uses the Windows Explorer interface. It provides no added functionality aside from the standard TreeView control methods, properties and events, however it does provide a Shell Item class that can be used to extend this basic example for your own needs and it is also a good starting point for those wanting to get to grips with the system image list and Shell32 programming.

Background

MBTreeViewExplorer is an explorer that inherits all the properties of a simple TreeView control. The language used is VB.NET. Parts of the code are based on other CodeProject tutorials and code samples found elsewhere on the Internet. All of the code was written by me but some of the concepts are from other references and books.

What I think is best about this program is:

  1. It is a simple design that should help you to learn how to use the TreeView control.
  2. Compared with many of the other sample or demo programs available, it loads directories fast!
  3. The program only loads the necessary folders.
  4. Here are only two events and one method that drives the MBTreeViewExplorer control.

(more…)

ColorPicker Control For Windows Applications


Introduction

What is the use of MBColorPicker? It provides a graphical interface to select a color from a set of various colors. It is simple to use, just drop it onto a form, adjust the design time properties and use it like normal controls.

Background

MBColorPicker is a color picker control that provides various functionalities so the user can select the required color from a set of colors. The language used is VB.NET.

Control properties

Here is the list of properties available in MBColorPicker:

  • PickedColor: This property gets and sets the color picked by MBColorPicker.
  • Arrow: This property sets the arrow on the MBColorPicker button.
  • BaseColor: This property sets the base color of the MBColorPicker button.
  • BaseStrokeColor: This property sets the base stroke color of the MBColorPicker button.
  • OnColor: This property sets the on color of the MBColorPicker button.
  • OnStrokeColor: This property sets the on stroke color of the MBColorPicker button.
  • PressColor: This property sets the press color of the MBColorPicker button.
  • PressStrokeColor: This property sets the press stroke color of the MBColorPicker button.
  • Radius: This property sets the corner radius of the MBColorPicker button.

(more…)