- Create an XML file with the desired structure (for simplicity on the desktop)
- Search and copy the file "xsd.exe" to the desktop
- Open a command line window (Windows key + R -> type "cmd") or (Start -> Run... -> type "cmd")
- Navigate to your desktop folder
- Then type "xsd.exe myfile.xml"
- Use the created file "myfile.xsd" as base for your xml file
Thursday, May 15, 2008
Working with XML files
Saturday, April 26, 2008
Who is faster??
for (int i = 0; i <> 5)
result.Add(numbers[i]);
}
Here we have the well known foreach loop which is and should be used.
foreach (int num in numbers)
{
if (num > 5)
result.Add(num);
}
result = numbers.FindAll(new Predicate(delegate(int i) { return i > 5; }));
var res = from num in numbers
where num > 5
select num;
More lines of code contain more errors.
Ok here there is the result of my measurements:

As you can see on the chart above, LINQ is by far the fastest "method" to perform such "queries". I was very impressed by the velocity. The difference between the first run and the second run lies in the implementation of LINQ. A speaker on the conference said that behind the scenes they build a binary tree and use it for searching. Therefore when this tree is build once all following queries are very very fast, also if you change the search condition.
Tools used:
Microsoft Visual Studio 2008 Express Edition
Wednesday, April 9, 2008
Dynamic number of parameters
First of all I'd like you to show an add method which accepts dynamic number of parameters.
public static int Add(params int[] numbersToAdd)
{
int sum = 0;
foreach (int number in numbersToAdd)
{
sum += number;
}
return sum;
}
It can be called in the following two ways:
int[] numbers = new int[4];
numbers[0] = 3;
numbers[1] = 8;
numbers[2] = 22;
numbers[3] = 2;
Console.WriteLine(Add(numbers));
/* OUTPUT
* 35
*/
or
Console.WriteLine(Add(3, 8, 22, 2));
/* OUTPUT
* 35
*/
Which version do you like more? I prefer the second one because I don't have to create an array of integers. Sure this method makes not really sense but it is only for demonstration purposes.
Another interesting method could be written which is the "Concat" method. It basically concatenates every object passed as parameter to a string and returns it:
public static string Concat(params object[] objects)
{
StringBuilder sb = new StringBuilder();
foreach (object obj in objects)
{
sb.Append(obj.ToString());
}
return sb.ToString();
}
The usage of this method would be the following:
Console.WriteLine("\n" + Concat("hello world ", 4, " ", true));
/* OUTPUT
* hello world 4 True
*/
Two other methods which could be interesting are the "Copy" and "CreateAndCopy" methods. The "CreateAndCopy" method takes an object as parameter, creates a new instance of the same type and copies the properties which are specified by their name in the parameters. In addition it is a generic so that no cast is necessary and it can be used with any object. Note that when calling this method the type has not necessarily be specified because C# defers the type from the object passed.
public static T CreateAndCopy<T>(T source, params string[] propertiesToCopy)
{
T target = (T)System.Activator.CreateInstance(typeof(T));
foreach (string propertyName in propertiesToCopy)
{
PropertyInfo property = typeof(T).GetProperty(propertyName);
property.SetValue(target, property.GetValue(source, null), null);
}
return target;
}
It can be used like this:
MyObject o1 = new MyObject();
o1.Text = "hallo";
o1.Number = 99;
o1.Boolean = true;
Console.WriteLine(o1.ToString());
/* OUTPUT
* MyObject:
* Text -> hallo
* Number -> 99
* Boolean -> True
*/
MyObject newObj = CreateAndCopy(o1, "Text", "Boolean");
Console.WriteLine(newObj.ToString());
/* OUTPUT
* MyObject:
* Text -> hallo
* Number -> 0
* Boolean -> True
*/
The "Copy" method does something similar but it accepts a destination object to which the values of the specified properties are copied to:
public static T Copy<T>(T source, T destination, params string[] propertiesToCopy)
{
foreach (string propertyName in propertiesToCopy)
{
PropertyInfo property = typeof(T).GetProperty(propertyName);
property.SetValue(destination, property.GetValue(source, null), null);
}
return destination;
}
CONCLUSION:
The possibility to specify variable number of parameters may sometimes help to make the code easier to read and better understandable but in some circumstances it is better to create a class which holds all the required values and pass an instance of it to a method.
Monday, April 7, 2008
Generic progress dialog
- it should be generic and therefore not be tied with the work which has to be done
- it should not block or freeze the main application
- the user must have the possibility to abort the operation
The following code shows the progress bar, sets the title and message, assigns a method to the cancel event and starts the computation if the background worker is not already working:
if (!this.backgroundWorker.IsBusy)
{
this.Enabled = false; /*lock the main application*/
ProgressDialog.Show(max);
ProgressDialog.SetTitle("this is the title");
ProgressDialog.SetMessage("I am the message for the very long task. Please be patient and wait...");
ProgressDialog.CancelEvent += new ProgressDialog.CancelEventHandler(pd_CancelEvent);
this.backgroundWorker.RunWorkerAsync();
}
Important is to set the cancellation property of the background worker to true; this is best done in the designer or in the constructor of the main form:
this.backgroundWorker.WorkerSupportsCancellation = true;
The computation is done in the do_work event of the background worker. Here is important that in the loop the check for the cancel event is done because otherwise the backgroundworker will not stop on cancel. To update the progress bar it is enough to call the SetValue method of the progress dialog. This is only a very stupid operation for demonstration:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < max; i++)
{
/*this check is needed in order to abort the operation if the user has clicked on cancel*/
if (this.backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
ProgressDialog.SetValue(i); /*update the value*/
Console.WriteLine(i);
}
}
Another thing that I have to mention. In debug mode there occurs always an exception which says that this operations are not thread safe. Till now I did not had the time to look how I can make this code thread safe, but as soon as I have a thread safe version (if one exists) I will post it.
UPDATE:
I finally had the time to learn how to write thread safe method calls. The code below shows how it is done. First you have to check if the mehtod was called by a Thread other than the own Thread. This does the property "InvokeRequired". If so a delegate is created and the method is called with "Invoke".
public static void SetTitle(string title)
{
if (instance.InvokeRequired) /*if another thread called this method*/
{
SetTitleCallback s = new SetTitleCallback(SetTitle);
instance.Invoke(s, title);
}
else
{
instance.Text = title;
}
}
You can download the full demo project here.
Finally a screenshot on how my progress bar dialog looks like:

If you have suggestions, ideas or You know how to improve this code or if You have a thread save version You are welcome to post a comment.
Tuesday, March 4, 2008
How to draw a disabled control?

The code to accomplish this is the following:
public class MyButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.FillRectangle(Brushes.White, pevent.ClipRectangle);
g.DrawRectangle(new Pen(Brushes.Black), pevent.ClipRectangle);
if (Enabled)
{
if(Image != null)g.DrawImage(Image, 0, 0);
g.DrawString(Text, Font, Brushes.Black, new PointF(100, 0));
}
else
{
if(Image != null)ControlPaint.DrawImageDisabled(g, Image, 0, 0, Color.Transparent);
ControlPaint.DrawStringDisabled(g, Text, Font, Color.Transparent, new RectangleF(100, 0, 100, 20), StringFormat.GenericDefault);
}
}
}
Tuesday, February 12, 2008
Wrong time under Vista in a domain
Recently I faced the following problem: my work computer is in a domain and unfortunately the clock of the server who provides the time for all clients is wrong. After trying to change my local clock I succeeded but only for a couple of minutes because then the old wrong time was used again. After an unsuccessful search on the Internet I found myself a solution for my problem. There exists a service which is called “Windows Time” and its description is:
“Maintains date and time synchronization on all clients and servers in the network. If this service is stopped, date and time synchronization will be unavailable. If this service is disabled, any services that explicitly depend on it will fail to start.”
After stopping this service and setting its start type to manual the problem was resolved.
CAUTION:
I do not know if switching off this service causes other things to not work correctly any more but till now I did not had any problems.
Thursday, January 31, 2008
Object - Relational converter
I was faced with the same problem and wanted to do something against this problem. If you are starting to build a project I would suggest you to use a free object relational mapper such as Castle Active Record which takes care of all the database stuff and makes your life a lot easier. It supports by far more features and supports different DBMS. If you are already within development or you do not want to use an object relational mapper or you simply do not want to inherit all your classes from a base class (which is used often used by object relational mapper) you could consider to use my approach.
I created a small object relational converter which is able to generate insert, update and delete statements for you based on an object that you pass as parameter. Additionally it provides the possibility to create objects from a datatable or datareader object.
I developed this code for Microsoft SQL Server Express Edition and therefore if you use another DBMS or you need to add another data type you have to change the “ToSQL” method which is responsible to convert the different data types into their correct sql representation:
/* This method is responsible to convert the datatypes into their corresponding */
/* sql representation. Change it if you encounter problems or if a data type */
/* needs special treatmend. */
private static string ToSQL(object obj)
{
if (obj is string || obj is Boolean)
return "'" + obj.ToString() + "'";
else if (obj is DateTime)
return "'" + ((DateTime)obj).ToString(DATE_TIME_FORMAT) + "'";
else if (obj is decimal)
return obj.ToString().Replace(',', '.');
else
return obj.ToString();
}
/* This method is responsible to obtain all public, not inherited properties of an object. */
private static PropertyInfo[] GetProperties(object obj)
{
return obj.GetType().GetProperties(
BindingFlags.DeclaredOnly |
BindingFlags.Instance |
BindingFlags.Public);
}
CAUTION: DO NOT USE RESERVED KEYWORDS OF THE SQL LANGUAGE BECAUSE THIS IS NOT CHECKED AND MAY CAUSE UNEXPECTED PROBLEMS!!
If the property represents the primary key of the table then you have to add a true because the primary key is needed for the update and delete statement. Here is now a sample class:
[Table("myTable")]
public class TestObject
{
private int table_pk;
private string text;
private DateTime datetime;
private int number;
private bool boolean;
private decimal decimalNum;
[Column("mytable_pk", true)]
public int Table_PK
{
get { return table_pk; }
set { table_pk = value; }
}
[Column("datetime")]
public DateTime Datetime
{
get { return datetime; }
set { datetime = value; }
}
[Column("number")]
public int Number
{
get { return number; }
set { number = value; }
}
[Column("text")]
public string Text
{
get { return text; }
set { text = value; }
}
[Column("boolean")]
public bool Boolean
{
get { return boolean; }
set { boolean = value; }
}
[Column("decimalnum")]
public decimal DecimalNum
{
get { return decimalNum; }
set { decimalNum = value; }
}
public TestObject()
{
this.table_pk = 999;
this.number = 5;
this.text = "this is a test string";
this.datetime = DateTime.Now;
this.boolean = true;
this.decimalNum = new decimal(1.5);
}
public override string ToString()
{
return "pk = " + this.Table_PK + ", datetime = " + this.Datetime.ToLongDateString() + ", number = " + this.Number.ToString() + ", text = " + this.Text + ", boolean = " + this.Boolean.ToString() + ", decimal = " + this.DecimalNum.ToString() ";";
}
}
TestObject a = new TestObject();
string sql = ObjectToSqlConverter.CreateInsertStatement(a);
sql = ObjectToSqlConverter.CreateUpdateStatement(a);
sql = ObjectToSqlConverter.CreateDeleteStatement(a);
You can download the whole project and a small sample from here.
If you have suggestions or you found an error feel free to write a comment and tell me about them.
I give you this code “as is” without any warranty that it works or that it does not cause problems or delete/modify important data. Use it at your own risk in your private and commercial projects and feel free to modify it.
Related links:
SQL Zoo (provides SQL statements for actual DBMSes)
Reserved words in standard SQL
Microsoft SQL Server datatypes and corresponding .NET datatypes