Jagged Array in C#

June 4, 2008

Jagged array represents array of arrays where each array can have arbitrary number of elements. It’s the similar concept of double pointers in C.

Jagged array provides flexible services for manipulating and controlling data. In the case of C pointers will have to do the manual book keeping for better control over the bounds of data. We can have the same implementation by using dequeue/vector container classes in C++. But still we can’t say they’re representing array. Rather than they’re containers and provides common “container” interfaces.

You can check the number of facilities available for jagged arrays and arrays from MSDN. Also you will get some other good examples

How to use Jagged Arrays – Listing 1

// constructing jagged array

int[][] myJaggedArray = new int[5][];

for (int i = 0; i < myJaggedArray.Length; i++)

{

myJaggedArray[i] = new int[i + 1];

}

// iterating each arrays

for (int i = 0; i < myJaggedArray.Length; i++)

{

// iterating each elements in an array

for (int j = 0; j < myJaggedArray[i].Length; j++)

{

Console.Write( “{0} “, myJaggedArray[i][j]);

}

Console.WriteLine(” - {0} elements”, myJaggedArray[i].Length);

}

How to use Jagged Arrays – Listing 1

int[][,] jaggedArray4 = new int[3][,]

{

new int[,] { {1,3}, {5,7} },

new int[,] { {0,2}, {4,6}, {8,10} },

new int[,] { {11,22}, {99,88}, {0,9} }};


Parameter Modifiers in C#

May 19, 2008

In C++, we can pass by value, reference (pointers are also there) when we need to pass some data to a C++ function.

In the case of C# also we’ve the same facilities.(Pointers are still there). They’re named as ref, out and params.

Let’s take a look at ref keyword. As the name indicates, “ref” stands for reference. If the formal parameter (ref variable) modified inside the function, it will be also get reflected in the actually parameter. (Same as C++ reference). In C#, the object must be initialized before passing as a reference. The sytanx is clean in C# because we’re specifying the parameter modifier at both function definition and at the time of usage.

static void Foo(ref int nData)

{

nData++;

}

static void Main(string[] args)

{

int x = 10;

Foo(ref x);

int y;

Foo(ref y); // ERROR: Uninitialized variables

}


Let’s take a look at out keyword. Out behaves similar to ref keyword but there’s no need to initialize the object passing to function. Even the object is initialized, inside the function, we’ve to re-initialize object. We can’t exclude this step.

In C++, there’s no constraint to use the reference variable inside the function. We can omit with/ without conditional statements. But in C# if you use out keyword, you will have to initialize the object inside the calling function. If you want the same behavior of C++ reference, it’s better to use ref keyword.


Ref and out keywords are same at the compilation time but behaves different at run time. So that you can’t overload “ref” and “out” with similar function signature.

static void Foo(ref Math m){}

static void Foo(out Math m){} // ERROR: can’t overload with ref and out keyword


params are allows to pass pass arbitrary number of parameters to this function. Params keyword has the following constraints. There should be only one params argument as function parameter and it should be appeared as the last parameter of a function(or no other parameters can be passed after a param variable)

See the same sample from MSDN

using System;

public class MyClass

{

public static void UseParams(paramsparams int[] list)

{

for (int i = 0 ; i < list.Length; i++)

{

Console.WriteLine(list[i]);

}

Console.WriteLine();

}

public static void UseParams2(params object[] list)

{

for (int i = 0 ; i < list.Length; i++)

{

Console.WriteLine(list[i]);

}

Console.WriteLine();

}

static void Main()

{

UseParams(1, 2, 3);

UseParams2(1, ‘a’, “test”);

// An array of objects can also be passed, as long as

// the array type matches the method being called.

int[] myarray = new int[3] {10,11,12};

UseParams(myarray);

}

}


Adding Basic shell auto-complete feature for your edit box or combo box

November 3, 2007

Just now I read about adding Auto complete feature to Edit box using Shell API.It’s very easy thing, you can touch up your application with Auto complete feature in 1-2 steps.1. Call CoInitiailize(NULL) when application initializes2. When the dialog initializes put the code.HRESULT hResult = SHAutoComplete(m_Edit.m_hWnd, SHACF_FILESYSTEM);The above code would be enough for a list box. So what you need in additional to put Auto complete feature for a combo box.Either your combo box is having of extended type (CComboboxEx) and use SendMessage function with as parameter to retrieve the handle of Edit control and use the edit control handle in SHAutoComplete function.For the normal combo box, you can use the following tweakHWND hWnd = ::FindWindowEx(m_Combo.m_hWnd,NULL,”Edit”,NULL);SHAutoComplete(hWnd, SHACF_URLHISTORY);Auto completeSHAutoComplete function will give a minimal but enough auto completion mechanism available in Shell. You can checkout a variety of options from MSDN documentation.You can also check Using Autocomplete article from MSDN. It will give more information on using IAutoComplete Object.