Wednesday, February 11, 2009

Sorting 2D string list in C#




/*
* In application code
*/
// Declare a variable for data
public static List recordList = new List();


// Set a list
// For example,
// 2/11/2009, apple, 20
// 2/10/2009, banana, 5
// 1/30/2009, pineapple, 3
recodeList.Add(new clsDataRecord(DateTimeVar1, TextVar1, IntVar1));
recodeList.Add(new clsDataRecord(DateTimeVar2, TextVar2, IntVar2));
recodeList.Add(new clsDataRecord(DateTimeVar3, TextVar3, IntVar3));


// Call sorting by date
clsDataRecord.CompType = clsDataRecord.ComparisonType.Date;
Program.altRecordList.Sort();
// Sorted result will be
// 1/30/2009, pineapple, 3
// 2/10/2009, banana, 5
// 2/11/2009, apple, 20


// Call sorting by text
clsDataRecord.CompType = clsDataRecord.ComparisonType.Text;
Program.altRecordList.Sort();
// Sorted result will be
// 2/11/2009, apple, 20
// 2/10/2009, banana, 5
// 1/30/2009, pineapple, 3


// Call sorting by number
clsDataRecord.CompType = clsDataRecord.ComparisonType.Number;
Program.altRecordList.Sort();
// Sorted result will be
// 1/30/2009, pineapple, 3
// 2/10/2009, banana, 5
// 2/11/2009, apple, 20




/*
* Class for clsDataRecord
*/
using System;
using System.Collections;


class clsDataRecord : IComparable
{
public enum ComparisonType
{
Date = 1,
Text = 2,
Number = 3
};


// This class has three different members.
// You can customize these with your own data.
private DateTime _date;
private string _text;
private int _number;


private static ComparisonType _compType;


public clsDataRecord(DateTime date, string text, int number)
{
_date = date;
_text = text;
_number = number;
}


public DateTime Date
{
get { return _date; }
set { _date = value; }
}


public string Text
{
get { return _text; }
set { _text = value; }
}


public int Number
{
get { return _number; }
set { _number = value; }
}


public static ComparisonType CompType
{
get { return _compType; }
set { _compType = value; }
}


public override string ToString()
{
return String.Format("{0} - {1} - {2}", _date.ToShortDateString(), _text, _number.ToString());
}


public int CompareTo(object obj)
{
if (obj is clsDataRecord)
{
clsDataRecord r2 = (clsDataRecord)obj;


switch (_compType)
{
case ComparisonType.Date:
return _date.CompareTo(r2.Date);
case ComparisonType.Text:
return _text.CompareTo(r2.Text);
case ComparisonType.Number:
default:
return _number.CompareTo(r2.Number);
}
}
else
throw new ArgumentException("Object is not an clsDataRecord.");
}
}

Rectangular Array vs. Jagged Array in C#


Friday, February 6, 2009

Set DateTimePicker in C# with specific date

string example = "2/6/09";
char[] delimiter = {'/'};
string[] dateInfo = example.Split(delimiter);

private System.Windows.Forms.DateTimePicker myDateTimePicker;

myDateTimePicker.Value = new DateTime(int.Parse(dateInfo[2]) + 2000, int.Parse(dateInfo[0]), int.Parse(dateInfo[1]));

string to integer in C#

Example

string str = "09";
int year = int.Parse(str) + 2000;

Output
year == 2009

Split string in C#

Example code

class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}

Output

Original text: 'one two three:four,five six seven'
7 words in text:
one
two
three
four
five
six
seven

Reference: http://msdn.microsoft.com/en-us/library/ms228388(VS.80).aspx

Related work searching

"http://www.scitopia.org/" provides an interesting related work searching facility supoorted by a large number of professonal societies - led by IEEE.

How to access the file system on Windows Mobile 5 Smartphone emulator

Here is the short description about how to access the file system on Windows Mobile emulator.

If your application is using data files or images, you need to copy them in a memory for the emulator.

I assume that you use Visual Studio 2005, you didn't connect any mobile device to your desktop, and you have already installed ActiveSync.

Here is simple procedure.
1. Under "Tools" in Visual Studio 2005, select "Device Emulator Manager".
2. Scroll down the list of emulated devices until you see what you want to use.
(i.e., I selected "Windows Mobile 5.0 Smartphone Emulator".)
3. Under "Actions" in the emulator manager, select "Connect". Then, an emulator will pop up.
4. Under "Actions" in the emulator manager, select "Cradle".
5. Now open ActiveSync. Under "File" in ActiveSync, select "Connection Settings". Check "Allow connections to one of the following" and select "DMA". You only need to do this once. When you click “Connect” in ActiveSync, it connects to the emulator and give you some setup screens. After the first time, the connection happens automatically when you “cradle” the emulator via the Device Emulator manager.
6. Now you can browse the file system on the emulator by selecting “Explore” on ActiveSync (or
selecting “Mobile Device” from My Computer). By default, your application is deployed to
“Program Files\[your application name]”.

Reference: http://inst.eecs.berkeley.edu/~cs160/sp06/section/vstutorial.pdf