DuraSoft TechLet 01, 2003
|
Welcome
to the January 2003 TechLet. If you have received this through some one you know and
would like to subscribe directly for our future issues, please send an email to
techlet@durasoftcorp.com with a subject subscribe. This subscription currently
is absolutely free.
In this issue, we have a detailed article on XML Serialization in .NET, and we will see how easy it is to mix and transform between C# (or any .NET language) objects and XML. |
|
If you like any question to be addressed or topic to be discussed, please send
us an email to techlet@durasoftcorp.com. At the end of the year, one person
will be randomly selected from the list of respondents for a special prize. We
would like to hear from you.
Winner from 2002!: One person was chosen randomly on December 23rd from the Techlet subscribers who wrote to us in 2002. Congratulations to Vijayabaskar Gangadar who is the winner of a $100 gift certificate from Amazon.com. Please send suggestions, corrections and comments to techlet@durasoftcorp.com. Wish you all a very happy, safe and prosperous new year! |
| XML Serialization in .NET? XML Serialization in .NET provides ease of development, convenience and efficiency. This article discusses the benefits of XML serialization and ways to configure the generated XML document. It also goes further to discuss some of the issues and deficiencies of this process as well. The complete article can be found at http://www.durasoftcorp.com/download |
|
In the article "How to create/avoid memory leak in Java and .NET?" we discussed how .NET provides the IDisposable interface as a
streamlined standard interface for resource garbage collection. A user of an object should check to see if the class implements the IDisposable
interface and then call the Dispose method to release the resources utilized by the object. What keyword does C# provide to help with this
process, in a way that this dispose mechanism may be semi automated.
Quiz from the past issue This month's quiz is exclusively for those who consider themselves savvy in C++. In the June article, we discussed about the issues with implementing the equals method in Java. The solution pretty much addresses how you can solve the problem in a language (like Java, .NET languages) that supports reflection. It got me thinking about how one can solve the problem of equals (operator==) in C++ efficiently. So, here is the question to you. Can you implement the operator== for the code given below such that it produces the correct result? #include Answer! There are two answers to this question.
Answer 1:
virtual bool operator==(const Point& other) const
{
int* ptrToMyVptr = (int*) this;
int* ptrToOthersVptr = (int*) &other;
void* pMyVPtr = (void*) *ptrToMyVptr;
void* pOthersVptr = (void*) *ptrToOthersVptr;
if (pMyVPtr == pOthersVptr)
{
if (x == other.x && y == other.y)
return true;
}
return false;
}
ColorPoint::operator== method:
virtual bool operator==(const Point& other) const
{
int* ptrToMyVptr = (int*) this;
int* ptrToOthersVptr = (int*) &other;
void* pMyVPtr = (void*) *ptrToMyVptr;
void* pOthersVptr = (void*) *ptrToOthersVptr;
if (pMyVPtr == pOthersVptr)
{
const ColorPoint* pOtherColorPoint =
dynamic_cast
The above code gets the pointer to the virtual table from the two objects. If the classes of the two object's are the same,
then the virtual table pointers must be equal. Of course, one could get creative and make the above code more cryptic and shorter.
But a more sophisticated answer, if you will, would be the following: Answer 2: It is to use some thing close to reflection in C++. First #include<typeinfo.h>. Then you can use the typeid operator to get the type information of the object at run time. Point::operator== method: virtual bool operator==(const Point& other) const
{
if (typeid(*this) == typeid(other))
{
if (x == other.x && y == other.y)
return true;
}
return false;
}
ColorPoint::operator== method:
virtual bool operator==(const Point& other) const
{
if (typeid(*this) == typeid(other))
{
const ColorPoint* pOtherColorPoint = dynamic_cast
|
| Everything should be made as simple as possible, but not simpler - Albert Einstein. |