Saturday, November 26, 2011

Arrays 2

When you declare an array, you don’t have to specify the capacity of the array.

You have to do this while initializing arrays.

[VB] Dim v as Integer()

[CS] int[] v;

This is just declaration of integer array variable. Value of v is nothing/null.

Initializing arrays with 5 elements

[VB] v =  new Integer(4){}

[CS] v =  new int[5]

in Visual Basic, the value specified within the parenthesis are Upper bound and the lower bound is always zero. Hence it can hold 5 elements (0,1,2,3,4).

VB developers: To avoid confusion, you can declare like this

[VB] v = new Integer(0 to 4){}

Array elements can be accessed with indexes

[VB] Dim n as Integer = v(2)

[CS] int n = v[2]

Index can also be variable

[VB]

    Dim index as integer = 2

    Dim n as Integer = v[index]

[CS]

    int index = 2;

    int n = v[index];

Accessing array elements by indexes gives a great opportunity for working with large set of data.

You can access series of elements using looping constructs.

[VB]

    For index as Integer = 0 to 4

        v(index) = index * 10

        Console.WriteLine(v(index))

    Next

[CS]

    for(int index=0;index<5;index++)

    {

        v[index] = index * 10;

        Console.WriteLine(v[index]);

    }

Also you can access elements with foreach loops

[VB]

    Foreach element as Integer in v

        Console.WriteLine(element)

    Next

[CS]

    foreach(int element in v)

    {

        Console.WriteLine(v[element])

    }

Arrays

You can use arrays when you have a fixed size collection of data with similar types.

Declaration

[VB] Dim v as Integer()

[CS] int[]

Initialization with specific size

[VB] Dim v as Integer() = new Integer(9){}

[CS] int[] v = new int[10]

Initialization with Data

[VB] Dim v as Integer() = {1,2,3,4}

[CS] int[] v = {1,2,3,4}

You can use object arrays, if you do not want to restrict your data to specific type.

Monday, November 21, 2011

Byte Array/Binary

To store byte array or binary data, Base64 string format is commonly used.

Convert.ToBase64String

This can help you to store binary data to Xml files and string columns in database.

Common Data Types

String and Byte array are the common data types to keep any data types. Of course, sometimes we may need to write code to do that.

String to Number conversion

To convert a string to a number, you can use one of the following methods

<DataType>.Parse
Convert.To<DataType>

For example, to convert a string to int, you can use

int.Parse
or
Convert.ToInt32

Console Screen: runs and disappears?

When you compile and execute a console application, it may run fast as disappear. To get rid of this, you can add

Console.ReadKey

Statement in the end, to pause it. It will wait until you press a key.

Static Members

If you have a set of variable members those don’t change for each instance of that class, you can declare them as Static Variable Members.

If a method inside class, accesses no member variables you can declare the method as Static Methods.

Member methods can not use This (VB)/ base (CS) object, as there is no specific instance associated with it.

For accessing static members, you don’t have to instantiate the class. You can directly call the member with class name.

Saturday, November 19, 2011

Variable Declaration

If you are a VB6 or ASP developer, and if you do not want to declare variables, you can do that by putting this line in the First line of the Source Code file

Option Explicit Off

Link: http://msdn.microsoft.com/en-us/library/y9341s4f%28v=vs.80%29.aspx

But it is better to turn this On, so that you can avoid misspelled variable names

Example

intToatl = intTotal + intValue

Friday, November 18, 2011

Solution, Project , Assembly and Namespace Names

In Visual Studio, sometimes we get confusion about Solution, Project , Assembly and Namespace Names.

Solution is a container for holding one or more related projects. Name it in a way to identify the entire concept for which we are going to create the Visual Studio Projects.

Project is a container for holding one or more related source files which are grouped together to give a single Assembly or Output (an exe or dll file). Name it in a way to identify the concept implemented in the project.

Assembly Name is the name given to the actual output (exe or dll file).

Namespace is a container for holding one or more types, which resides in an assembly. An assembly or Project can have more than one namespaces.

By default all these are same, but you can have all them with different names.

Console Applications

Console applications are the best way to perform many tasks which needs automation by means of batch files and scheduled tasks.

Command line arguments provides you a way to send inputs to the console applications.

In Command prompts redirection operators helps you to link sequence of command line operations.

 

Links

Command line arguments: http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx

Redirection operators: http://technet.microsoft.com/en-us/library/bb490982.aspx

Wednesday, November 16, 2011

Welcome

Welcome to the tips blog for Microsoft .Net 4.0 Framework. Here you can find, exclusive tips and articles regarding .Net Framework 4.0 with C# and VB.Net.