.NET: Passing a DataSet through the ExtendedProperties of another DataSet

 Sometimes you find the need to pass more than one DataSet as the result of a routine/function.  Although you can use reference parameters for doing so, there's another approach.

You can pass additional DataSets by adding them to the ExtendedProperties property of the "main" DataSet.  ExtendedProperties is defined as being of type PropertyCollection, which inherits from the Hashtable type, so its limit depends on the amount of memory that the host machine has.

A limitation that becomes apparent when using .NET remoting is that the values stored in the ExtendedProperties should be of type string in order to be persistent when it crosses the application domains between the server and the client.  With this limitation in mind, the steps are roughly as follows:

1.   Convert the "additional" DataSet to string.
2.   Store the result of step 1 in the ExtendedProperties of the "main" DataSet.
3.   Pass/return the "main" DataSet to the calling process.
4.   Convert the string representation of the "additional" DataSet back.

Essential routines/information:
Step 1:
The DataSet class has a GetXml method that returns the XML representation of the DataSet.

Steps 2 & 3:
Self explanatory.

Step 4:
The DataSet class has a ReadXml method for initalizing a DataSet based on XML.

Combining all the information, the code can look something like this:
// SAMPLE FUNCTION (SampleFunction())
using System.Data;
DataSet dsMain;
DataSet dsSub;
// Populate the DataSets here
string dsSubAsString;
dsSubAsString = dsSub.GetXml();
dsMain.ExtendedProperties.Add("dsSub", dsSubAsString);
// Pass dsMain to calling routine/function

using System.Data;
using System.IO;
DataSet dsMainReturn;
DataSet dsSubReturn;

// Call function to retrieve the DataSets
// and convert dsSub
dsMainReturn = SampleFunction();
dsSubReturn = new DataSet();

string dsSubReturnAsString = (string) dsMainReturn.ExtendedProperties["dsSub"];

using (System.IO.StringReader sr = new StringReader(dsSubReturnAsString))
    dsSubReturn.ReadXml(sr, XmlReadMode.InferSchema);

// Use dsSubReturn as required

And there you have it.  Taking it a step further, the code for conversion from XML/string to DataSet can be wrapped as a function in a helper class, so as not to affect programmer productivity. :)

Posted by raistlin | with no comments
Filed under: , ,

.NET: How to determine if a string comes before another string based on the dictionary order

 Language: C#

Usually string comparisons are needed just to determine the equality (or inequality) of 2 strings.  However, there are times when determining inequality isn't the end in itself.  Sometimes a developer also needs to know if a particular string should "come before" another string --  alphabetically , based on the common dictionary order.

The regular relational operators ( <, >, =<. >=) do not work on string variables and/or literals. So using:

    if (string1 < string2)
       //    code here

is out of the question because the compiler will not even allow it.  So what's the alternative?  As it turns out, the same function that I mentioned for performing case-insensitive string operations (string.Compare), is also capable of determining if the first string parameter alphabetically comes before the second string parameter.  The function returns zero (== 0) if the strings are equal, a negative value (< 0) if the first string is "less than" (alphabetically comes before) the second string, and a positive value (> 0) if the first string is "greater than" (alphabetically comes after) the second string.  Given this, the following code will return a negative value:

    string.Compare("B21", "B22");

whereas this will return a positive value:

    string.Compare("B22", "B21");

Now, if the strings are not of equal length, the longer string is always considered "greater" than the shorter string.  So the following code will return a negative value:

    string.Compare("B2", "B21");

And, of course, you can perform case-insensitive comparisons, e.g.

    string.Compare("b22", "B21", false);

As always, please refer to the MSDN documentation for additional information. Link

Posted by raistlin | with no comments
Filed under: , ,

.NET: Performing case-insensitive string comparison

 Language: C#

Performing string comparisons can be considered as one of the "common" tasks that a programmer needs to do and, more often than not, the comparisons need to be case-insensitive.  The common method taught (or learned, as the case may be) is to convert the entire string to upper case or lower case and perform the comparison accordingly against the appropriate literal.  There's nothing wrong with that method and it has been used since time immemorial.

However, in the .NET environment strings are immutable, and calling the ToLower()/ToUpper() methods of the string class internally creates a new string variable.  A new variable means additional overhead in terms of processor time (the duration consumed during the allocation, creation and assignment of the new variable) and the actual memory consumption of the new variable.  It may not be enough to affect performance, but when you need to squeeze every last bit of available memory for an application, this can become crucial.

So what's the alternative?  Incidentally, the string class provides a static method for performing case-insensitive comparisons without the additional overhead.  It's the Compare method.  It has six overloads but, for the purpose of this mail, i'll just mention one.  The overload we're interested in has the following signature:

    public static int Compare( string strA, string strB, bool ignoreCase)

Equality is indicated by a return value of 0 (zero).  Given this, instead of:

    if (someString.ToUpper() == "VALUE")
we can use this:

    if (string.Compare(someString, "VALUE", true) == 0)

    if (string.Compare(someString, "value", true) == 0)

    if (string.Compare(someString, "vAlUe", true) == 0)


As you can see, it's also less prone to mistakes.  As against to calling ToUpper() and comparing a string to a literal which is composed of lowercase characters by accident.

Other overloads for the Compare method enable developers to specify culture information, compare the substrings of 2 strings, etc.
Please refer to the MSDN documentation for additional information. Link

Posted by raistlin | 3 comment(s)
Filed under: , ,

Making the LINQ (Preview) Project Templates appear in VS2005 (and other template "tricks")

I encountered this problem last week, wherein the LINQ project templates were missing in the "New Project" dialog of VS2005 Team System even after doing "everything" (machine reboot, "install IDE support", etc.).  By searching the web (through the help of our trusty buddy, "google"), i was able to determine that the LINQ project templates files were:







DLinqObjects_CS (item template)



LINQ Console Application.zip

LINQ Class Library.zip

LINQ Web Application.zip

LINQ Windows Applicztion.zip

DLinqObjects_VB.zip (item template)

After obtaining the information, all I had to do now was to determine where to place the said files.  After some searching within "Program Files\Microsoft Visual Studio 8" I eventually found the place:

<drive>:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Project Templates

There's a folder for each language, just place the appropriate files in the appropriate language folder. If you want the templates in their own "category display" within the "New Project" dialog of Visual Studio, just create a subfolder in the language folder.  In my case, I created a "LINQ Preview" subfolder under "CSharp" and placed the files there.

I did the same thing for the DLinqObjects_CS.zip file, but i placed it in the appropriate subfolder under "Item Templates" instead of "Project Templates".

As a side note, I'd like to mention that I never encountered this problem when I was still using Visual C# 2005 Express Edition.  Weird.

I hope somebody finds this helpful/useful.

Posted by raistlin | 3 comment(s)
Filed under: ,

Making things unnecessarily complicated

Here's another interesting post that i found today.  I admit that I sometimes tend to think this way. Big Smile
Posted by raistlin | with no comments

Programmers Don't Like To Code

I found this interesting post today.  And I have to say that I agree. Big Smile
Posted by raistlin | with no comments

Using PInvoke Slides and Demos (PHINUG January Tech Share)

For those who are interested in the presentation slides and demos that I used last night, get them here.

I've also added a sixth demo, the one the I wasn't able to finish on the spot. Big Smile.

Posted by raistlin | with no comments

Is IT certification still relevant?

I received an interesting article, dealing with the issue on the necessity of getting certified.  I have to admit, that I'm one of those sitting on the fence.  For one, some certifications' lifetimes are so short that they may not be worth it.  An example of this would be Microsoft's .NET certifcations, with .NET 2.0 barely out for more than a year, .NET 3.0 certifications are already in the works.

Another thing is the easy access to "sample" questionnaires.  I know of several former co-workers (in a previous company) who got certified just by reading the questionnaires over and over again and memorizing the correct answer for each question.  It's kind of disheartening to know that some people get certified without actually "deserving" it.  Given that, it somehow lessens the "weight" of holding a certification title.  What's to stop employers from assuming that you're just one of those who used a "sample questionnaire" to obtain the certification?

If certifications were really awarded only to those who deserve it, and if there were no, for lack of a better term, "cheat sheets", I think IT certification would definitely be worth it.  But hey, that's just my opinion.  Feel free to share your comments.

Any thoughts?

Posted by raistlin | 9 comment(s)
Filed under:

An interesting read...

This article, which I first read in December 2005, really made think... and reminisce... Big Smile

It's a bit long, but i think it's worth reading.  I never thought I'd miss the "old days" of programming. Smile

Posted by raistlin | with no comments

.NET: How to determine if a variable can be converted to a particular type

Platform: .NET Framework
Language: C#

Using reflection, we can determine if a particular variable can be converted to another data type.  This is useful if you're not sure if the conversion that you're about to perform is valid or not.  Although this can be done using the common way of converting, for example:

      int x = 10;

However, the previous code will throw an exception, so you're obligated to enclose the code in the "try...catch" block.  Exceptions are, however, expensive operations and can be avoided in cases of invalid conversions.  By changing the above code to:

       using System.ComponentModel;

       int x = 10;

       IConvertible c = x as IConvertible;
       if (c == null)
             // code here

       if (TypeDescriptor.GetConverter(x).CanConvertTo(typeof(System.DateTime)) == false)
             // code for invalid conversion here

you can avoid using exceptions just to handle invalid conversions.

I know what you're thinking, it takes several lines more than the "common" way.... but you can encapsulate all this code into a conversion function, and using the above code would become more "convenient".  One thing to note though, the "CanConvertTo" returns false when checking if a string variable can be converted to a boolean type.  So special handling should be done within the function for such a case.

I've attached a simple console app demonstrating this.  Also, take note of the "IConvertible.ToType" conversion function in the attachment.  Very useful, IMHO.

For further information, refer to the MSDN documentation on the TypeDescriptor, TypeConverter classes and the IConvertible interface.


Posted by raistlin | 6 comment(s)
Filed under: , ,

First Blog, First Post

Hi! Welcome to my blog.  Hopefully, I'll be able to post a lot of stuff that you'll fnd helpful/useful/informative.

Check back from time to time. Smile

Posted by raistlin | 5 comment(s)
Filed under: