Sunday, November 20, 2005

A brief study on 'structure'

We are familiar with the concept of “structure”, and all of us must have used it many times. Most of the languages like C++, Java, VB, and of course C# and VB.NET support the concept of structure, how if we dig a little deep into this concept.

As we all know structure is a complex type, not like int or bool, but there are some more characteristics of a structure, let me explain with the context of dot net platform…..

1) Every structure is inherited from System.Object( like any class ), well, every structure is inherited from System.ValueType which inherits System.Object, hence every structure is inherited form Object.

2) Structure can not be inherited, they are like “sealed class”.

3) Structure instance are stored in the stack, and for this reason it doesn’t need any garbage collection. (well, there are exception of this rule, please read the last paragraph )

4) A structure can be boxed into a System.ValueType variable in addition to an Object variable.

5) As “Abstract” and “Virtual” are useless in case of structure(any value type ) hence can we write structure doesn’t support polymorphism ?? well I think NO, since we can do function overloading so it would be wrong to say it doesn’t support polymorphism at all.

In essence we can say that a structure is a complex type whose behavior can not extended ( the way we do it in case of class, I mean to say using inheritance we can not extend the behavior, but we can use the concept of “containment” in structure ) .

Okay .. so why do we need a structure at all when we had “class” .. because
structure has a very small footprint compare to class instance and faster in performance and no overhead of garbage collection.
There are certain situations where structure is a good candidate( compare to class ) if
1) we need a complex type and typically whose behavior doesn’t need to be extended.
2) there will be no boxing involved.


As we have mentioned above that structure doesn’t need garbage collection and we also know that structure supports “Containment” .. now what if we have a structure which contain a reference type, consider the following example …

public struct A
{
public int y;
public bool z;
}

public struct B
{
public A x;
public Book y; // Book is a class
}

In the above example structure B is contain structure A and class Book, hence when we will create an instance of “B” there will some memory allocation in the heap for the Book class instance, and when the instance of the structure B will go out of scope the memory allocated for y and z will be cleared ( from the stack ) at once, but the Book class instance will be destroyed by the garbage collection( this is an exception of characteristics 3 mentioned above ).

Tuesday, November 01, 2005

"throw" and "rethrow"

Few days back I was working on some C# code, and I found an interesting case .. all I was thing to throw an exception from an called routine to the caller and logging the exception in the caller, and I found the stack trace is not correct( more specifically the stack trace was missing some entries ).
I had to spend some time on this to find the reason behind it ... let me explain with some sample code snippet ..

private void btnTest_Click(object sender, System.EventArgs e)
{
try {
DummyMethod_1();
} catch(Exception ex) {
richTextBox1.Text = ex.GetType().ToString() + " " + ex.Message + "\n";
richTextBox1.Text += ex.StackTrace;

}
}

private void DummyMethod_1()

{
try {
DummyMethod_2();
} catch(Exception ex)
{
MyNewException e = new MyNewException ("My New exception" );
throw e;
}
}

private void DummyMethod_2()

{
ThrowException();
}

private void ThrowException()

{
int x = 0;
int y = 0;
int z = x/y;
}

btnTest is a command button and I am calling a routine from the click event of this routine, then there is a chain of routines .. which ultimately called "ThrowException" routine and which cause an exception and the "DummyMethod_1" catch the exception and send a new exception "My New Exception" to the caller ( throw e ).
I am showing the stack trace of the exception in a rich text box ...
with the above code the stack trace will look like this ...
System.Exception.MyNewException My New exception at ThrowTest.Form1.DummyMethod_1() in f:\personal\rnd\throwtest\throwtest\form1.cs:line 127 at ThrowTest.Form1.btnTest_Click(Object sender, EventArgs e) in f:\personal\rnd\throwtest\throwtest\form1.cs:line 102

All it was showing btnTest_Click::DummyMethod_1(); and the DummyMethod_1:: throw e; ... what about the DummyMethod_2 and ThrowException method ??? as they were also part of this exception in fact "ThrowException" is the routine where the exception was generated.

Then I have changed my code little bit .. instead of throw e; I modified it as throw; now the stack trace looks like this ..
System.DivideByZeroException Attempted to divide by zero. at ThrowTest.Form1.ThrowException() in f:\personal\rnd\throwtest\throwtest\form1.cs:line 140 at ThrowTest.Form1.DummyMethod_2() in f:\personal\rnd\throwtest\throwtest\form1.cs:line 133 at ThrowTest.Form1.DummyMethod_1() in f:\personal\rnd\throwtest\throwtest\form1.cs:line 127 at ThrowTest.Form1.btnTest_Click(Object sender, EventArgs e) in f:\personal\rnd\throwtest\throwtest\form1.cs:line 102

So what is the mystery of "throw e" and "throw", if we look at the MSIL , in case of thorw e .. the keyword is "throw" ... it creates a new exception( e in this case ) which has a separate stack trace altogether, but in case of thorw .. the MSIL is "rethrow" which re-thorws the existing exception, which means the stack trace will be intact.

hmmmm .. isn't it interesting ?? .. well, there is another way to tackle this situation when we will be creating a new exception we should pass the existing one as an inner exception , like this ..
MyNewException e = new MyNewException ("My New exception", ex ); and in the caller we have to look into the inner exception's stack trace as well, so the updated stack trace showing code will look like this ...
richTextBox1.Text = ex.GetType().ToString() + " " + ex.Message + "\n";
if( ex.InnerException != null )

{
richTextBox1.Text += ex.InnerException.StackTrace + "\n" + ex.StackTrace;
} else
{
richTextBox1.Text += ex.StackTrace;
}

comments are welcome !!!!

Wednesday, October 05, 2005

Event raising issue

Few days back I was going thru an article on event raising in .NET environment and came accords a question " what is best the practice of raising event in multiple thread application" , let me illustrate with an example ...

say I have an delegate < public delegate void MyDelegate( Object sender, EventArgs e ); >
and an event < public event MyDelegate MyEvent; > inside a class named TestClass
I am raising event from a routine named RaiseEvent (Let me write the code snippet for this routine )

public void RaiseEvent()
{
if( MyEvent != null )
{
EventArgs e = new EventArgs();
MyEvent( this, e );
}
}

The above code will work fine in single thread environment, consider the following situation ... there are multiple threads which are using MyEvent( and there is single instance of TestClass is shared among all the threads ) , so it may happen( hypothetically ) when one thread is calling MyEvent( this, e ) another thread is removing the registered delegate from the MyEvent, have a look at the following figure ....

thread1 ...
action 1 : objTestClass.MyEvent -= new MyDelegate(objTestClass_MyEvent); ( removing the registered delegate from the MyEvent )

thread2 ...
action 1: if( MyEvent != null )
action 2: MyEvent( this, e );

If CPU switching between threads happen like the following, then there is a problem ..

thread2 -> action 1
thread1 -> action 1
thread2 -> action2 ( will raise an exception since the registered delegate from the MyEvent is removed ).

How to solve this issue .. firstly, I think we should not allow any thread to remove the registered delegate, if it is inevitable then how about the next solution
MyDelegate tmpMyEvent = MyEvent;

if( tmpMyEvent != null )
{
EventArgs e = new EventArgs();
tmpMyEvent( this, e );
}

This will solve the issue( am I correct ? ). I was looking for some MS documentation in this regards, but in vain , anybody has some comments to add .. please do.( thanks in advance -:) )











Sunday, October 02, 2005

ByVal and ByRef

We all deal with passing parameter ByVal and ByRef and as we all know that if we pass a variable byval to a routine and if the called routine changes the variable value, it does not reflect in the caller routine, opposite happen in case of byref.

But what happen in case of passing reference type as byval or byref … let us consider the following example..

I have a class named TestClass and which have a public property Name , then all I will do in a win form is create 2 routines which will takes instance of this class as a parameter, first one will take as byval, second one as byref.

private void UpdateTestClassName( TestClass objTestClass )
{
objTestClass.Name = "Neo";
}

private void UpdateTestClassName( ref TestClass objTestClass )
{
objTestClass.Name = "Neo";
}

Then I will add 2 buttons( ByVal and ByRef ) on the form and in the click event of one of them will call the both routines. Like this …

private void btnByVal_Click(object sender, System.EventArgs e)
{
TestClass objTestClass = new TestClass();
objTestClass.Name = "Palb";
Console.WriteLine( objTestClass.Name );
UpdateTestClassName( objTestClass );
try
{
Console.WriteLine( objTestClass.Name );
}
catch( System.NullReferenceException ex )
{
Console.WriteLine( ex.Message );
}
}

private void btnByRef_Click(object sender, System.EventArgs e)
{
TestClass objTestClass = new TestClass();
objTestClass.Name = "Palb";
Console.WriteLine( objTestClass.Name );
UpdateTestClassName( objTestClass );
try
{
Console.WriteLine( objTestClass.Name );
}
catch( System.NullReferenceException ex )
{
Console.WriteLine( ex.Message );
}
}

If we click on ByVal button the output will be following
Palb
Neo

And if we click on the ByRef button the output will be following ..
Palb
Neo

So it seems byVal and byRef behave in same manner in case of Reference type , why is it happening … let us look at what happen inside ..

Data for Reference types is stored on the heap and a pointer (which points to the data on the heap) is created on the stack, whenever an instance of reference type is created the pointer is returned back and is used to manipulate the data on the stack. Hence TestClass objTestClass = new TestClass(); actually returns back the pointer.

Now when this pointer is passed by val, all we are doing is duplicating the pointer but it still points to the same memory on the heap and hence any manipulation done to the object in the called method will manipulate the same data to which original TestClass pointer was pointing. In case of passing by ref, the original pointer itself is passed to the called function,

Now some more interesting facts , let us add one more line to the UpdateTestClassName routines( both byVal and byRef )
objTestClass = null;

Now guess what will the put out …
By Val click will be same , but By Ref click will be
Palb
Object reference not set to an instance of an object.

As we in case of ByRef we pass the original pointer hence if we set objTestClass = null, the original pointer get destroyed( in case of byVal the original pointer still exists, hence it shows the updated value in heap ), hence we will get an NullReferenceException.
Isn’t it interesting ??

Thursday, August 25, 2005

Who process the aspx file ?

A web form has two files one is aspx and the other is .aspx.cs ( or aspx.cs ). When we compile an web Application the .aspx.cs converts into dll, (read MSIL )
After compilation we deploy the web App in the following way , copy the dll and the .aspx file into the target directory ( we copy the web.config , global.asax and the referenced dlls also, but please ignore these part for the time being )

When user request for a aspx page , IIS send the request to ASP.NET, and as the aspx pages are not MSIL, so it can not be processed by CLR, correct ? so , here is the confusion who process the aspx pages ? it's like this , when user hit a aspx page for the first time ASP.NET automatically generates a .NET class file that represents the page and compiles it to a second .dll file. The generated class for the .aspx page inherits from the code-behind class that was compiled into the project .dll file. This single .dll file is run at the server whenever the Web Forms page is requested. At run time, this .dll file processes the incoming request and responds by dynamically creating output and sending it back to the browser or client device.
Those who are familiar with ASP, the ASP.NET page framework model represents something new. The ASP model is one of HTML extended with script code. The ASP page consists of script code, such as (JScript, or VBScript) , coexisting with static HTML within the same file. The ASP parser reads the page, interprets it, and runs only the script code to get output results. ASP then merges the output of the script code with the static HTML output found in the page before sending the output back to the browser or client device. In the ASP.NET Page class model, the entire Web Forms page is, in effect, an executable program that generates output to be sent back to the browser or client device.

Monday, August 22, 2005

Last few days...

After quite sometime I am writing again .. I was not really tied up with events, but sometime you are just not in a mood to pen down anything.

Although I was not tied up with events, but I was part of couple of them staged last few days, let me start with Sandeep's marriage, we went to a place named Baharampur( quite far from Kolkata, it took 6 hours or so by bus) to attend his marriage, it was quite fun, then the reception party also went well, my handy-cam has caught all those precious moments.

Palash came from Hydrabad, stayed at my place for couple of days, we really had some nice time. We had a plan to go to Some place else one night, but ultimately couldn't make it.

Then I went to my brother's place on the independence day after a loooooong time, as usual lot of jhar/preaching on "why do you stay like this ?", "why do you go to bed so late ?" " why do you..... bla bla bla", after all these there was a grrrrrrand lunch( sometime I consider the food is enough for a whole week ). Now it's time for my chhoooo chweeet niece Barsha, she is still very young, still love to sit on laps rather than walking -:) I thought she will be talking to me this time, but she still talks in some unknown language( hahaha ), but overall it's really fun spending time with her.

Lastly something tragic, I have lost my mobile phone, ahhh. rather some body has stolen from my cot( hard to believe !!! ), and as usual people first doubt on the maid first, so did I, but later on I came to know from my land lady that it was an salesman who had stolen. anyway whoever it is .. I bought a new one( rather I had to buy one to survive), but I have lost all contact numbers of friends/relatives/corporate contacts, seems it will cause me trouble in coming days.

Saturday, August 06, 2005

Good luck friends

It was another Saturday night at someplace else, I was chilling with a beer and Floyd, suddenly I felt somebody yelled at me "hey buddy ... seems you wanna get pissed off with Floyd" ..., I saw a young lad holding his beer bottle in one hand and a cigarette in another looking at me with a brief smile. "Yeah man, that's the idea ... how about you ? ".. I yelled "same here" .. he replied back.
This is how I met my friend Sumit and then introduced to Shamik and Gilmore( so far I don't know his real name .. this chap is so freak .. he almost changed his name to Gilmore < David Gilmore is the lead guitarist of Floyd >) ). They are all IIT KGP, comp science guy .. passed out between 2002 - 04 , all of them got handsome job but none of them liked it and left their companies. All they said me that they are involved with some research oriented project with IIT KGP.

We used to meet in almost every Saturday at Someplace else and then we used to involve into very serious things ... ( as you guess )beer, rock n roll and pure adda.
Gilmore is hailed our friendship as "halusinative friendship". We had a nice mental setup, every body is freak about rock n roll and all of are crazy about the world of software.
Last Saturday Sumit broke the news( it's good as well as sad ) to me ... they all 3 are going to join MIT, and they will leave next month, it's a kind of dreams comes true situation, I felt so happy but the next moment I felt I will miss them for a long time, that made me lil sad , but all in all MIT keeps me high ... I am really proud of you my friends .. go and make a mark in life. kudos to Sumit, Shamik and Gilmore.

I must mention about one more friend named Cherry, last month she joined us as a trainee, I used eat up her brain a lot .. -:) , but I must admit she is very sharp...
All I knew she was doing some research and study on electronics here, she is not a rock-n-roll freak but loves music and one thing I like about her is she also hates hip-hop( I just can't tolerate this sound, all trash to me ), she is a true Indian ....we used to fight on whether India is better country India than Australia ...... she left few days back to join her college

In summery I am going to loose 4 friends( one already out of the horizon ) .. so it's a tough time ... but anyway .. this is life, people come into your life become friend, spend time with you and then disappear, but thanks to internet .. we will be in touch. Good wishes to all of them...

God kept some good news for me as well, Palash is coming to town to attend Sandeep's( another common pal ) marriage ceremony. Just few months back we had a small gang......... Palash, Sandeep, Amitava, Amit and myself ... we used to paint the city in RED and enjoy to the fullest. Now we are all parted, Palsh is in Hydrabad, Amitava and Amit is in Bangalore.
But god is kind enough to give us one more opportunity to get together again and taste the same fun once more. thank you god, I have no complain whatsoever.







Monday, August 01, 2005

& and && in C# - Part II

Revisiting & AND && operator, in the part I, I have talked about them and written a very short code snippet, in part II, I will discuss how we can write more useful code using these operators.
Consider we have to write a user control and it has several flags like( IsVissible, IsEditable, IsDraggable etc etc ), common practise is creating boolean variable for each flag, but we can get away with all boolean flags, using one single integer variable .. how ? let me explain it.

We will declare one integer variable ( say name it as m_AllFlags ) and each bit within m_AllFlags variable would represent the current state of one of the flags. we would then use bit manipulations to set and to get each flag.
First, we need to set up constants that indicate the various flags for our UC, these flags should each be a different power of 2 to ensure that each bit is used by only one flag. we will set bits of flags accordingly to the current state of each flag.

int flags = 0 ; ( means that all flags are false (none of the bits are set) )
const int TESTCONTROL_VISIBLE = 1;
const int TESTCONTROL_EDITABLE = 2;
now we will write property for each flags .. lets have a look at IsVisible property

public bool IsVisible
{
set
{
if( value )
{
m_AllFlags = m_AllFlags | TESTCONTROL_VISIBLE;
}
else
{
m_AllFlags = m_AllFlags ^ TESTCONTROL_VISIBLE;
}
}
get
{
if( (m_AllFlags & TESTCONTROL_VISIBLE) == TESTCONTROL_VISIBLE )
{
return true;
}
else
{
return false;
}
}
}

so if user send TRUE we will do a inclusive OR and if it is FALSE we will do exclusive OR, and at the time GET we will do a bitwise AND , if the returned value matches with the constant then we will return TRUE else FALSE. Those who are not yet familiar with inclusive/exclusive OR the following table will help you to understand the concept ..
The Bitwise Inclusive OR Function Bit in op1 Corresponding Bit in op2 Result
0 0 0
0 1 1
1 0 1
1 1 1
The Bitwise Exclusive OR (XOR) Function Bit in op1 Corresponding Bit in op2 Result
0 0 0
0 1 1
1 0 1
1 1 0

lets see what happen if caller pass TRUE to this property ...
m_AllFlags = m_AllFlags | TESTCONTROL_VISIBLE; [ m_AllFlags = 0 inclusive OR 1, so now m_AllFlags = 1 ]

now if caller does a GET the following line will execute ...
if( (m_AllFlags & TESTCONTROL_VISIBLE) == TESTCONTROL_VISIBLE ) it does a bitwise AND then compare the value with a constant, based on the comparison result it returns TRUE or FALSE,
For those are not yet familiar with bitwise AND .. please have a look at the following table ..
The Bitwise AND Function Bit in op1 Corresponding Bit in op2 Result
0 0 0
0 1 0
1 0 0
1 1 1
(m_AllFlags & TESTCONTROL_VISIBLE) == TESTCONTROL_VISIBLE ) [ 1 bitwiseAND 1 == 1 , hence it returns TRUE ]

Let us consider, after this caller set the flag as FALSE
m_AllFlags = m_AllFlags ^ TESTCONTROL_VISIBLE; [ m_AllFlags = 1 exclusive OR 1, so now m_AllFlags = 0 ]

now if we do a GET
(m_AllFlags & TESTCONTROL_VISIBLE) == TESTCONTROL_VISIBLE ) [ 0 bitwiseAND 1 == 1 , hence it returns FALSE ]

Most important point about the above code snippet is .. when the caller pass a flag value .. we need to first check the current bit value and if it is same we will just ignore it else we will set the value,[ if( (m_AllFlags & TESTCONTROL_VISIBLE) == TESTCONTROL_VISIBLE ) { //caller has passed the same value .. ignore it.} ].
So we can end up with one single int variable which will hold all the flags, I have uploaded the entire project code in this following location( Operator test source code )please download and play with it if you feel interested.

Note : Thanks to Palb for his comments.

Neo













Sunday, July 31, 2005

& and && in C# , part - I

There are 2 AND,OR operators in C# ( of course it exists in language like C++ or Java as well ), one is &, | and second one is &&, II , most of the time we work with the second one, first one is less popular in regualar programming, but the first one is more powerfull and usefull than the second one( my personal comments :) ).
As per MSDN documentation difference between them two is following ...

The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.
Binary & operators are predefined for the integral types and bool. For integral types, & computes the bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true.
The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.
Let me use the following code to explain it ....
int i = 0;
int j = 0;
if( false & ( ++i > 0 ) )
{
MessageBox.Show( "Inside if condition" );
}
if( false && ( ++j > 0) )
{
MessageBox.Show( "Inside if condition" );
}
MessageBox.Show( i.ToString() );
MessageBox.Show( j.ToString() );
guess what will be the value of i and j when MessageBox will show ... i will be 1 and j will be 0, since first if has used & so run time will evaluate both the operand( hence i will be increased by 1), but in case && as the first oeprand is FALSE it will not evaluate the scond one at all( hence value of j will remain 0 ), but none of them will show "Inside if condition".
I will write some more interesting facts of & operator in part II.

Note : in C++ we can use & as a unery operator. & gives the address of the operand.

Wednesday, July 27, 2005

Dangling Pointer

Last night I was trying my hand on C++ .... and I came across an interesting problem ...
all I was trying to do following
1) create a class ( say TestClass ) which will have an int variable( age ), and we can set value into this variable using 2 methods(set and get).
2) create a global scope pointer of this type in the file.( say m_TestClass ) ...
3) write a method( say createTestClassObject ) where I will create an object of the class "TestClass" and the pointer( m_TestClass ) will point to this object.
then I will do a set and get( printf ).
4) "createTestClassObject" method will be called from the main and after this method returns to main .. I will do another m_TestClass->getAge()( priintf ) .
This is what I have written ...( I have used .NET IDE )

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "conio.h"
#using
using namespace System;
using namespace std;

void createTestClassObject();


class TestClass
{
int m_Age;
public:
TestClass()
{
// dummy constructor
}

void setAge( int age )
{
m_Age = age;
}

int getAge()
{
return m_Age;
}
};

TestClass *m_TestClass;
int main()
{
createTestClassObject();
printf("\n%d", m_TestClass->getAge() );
int i = getch();
return 0;
}

void createTestClassObject()
{
TestClass obj;
m_TestClass = &obj;
m_TestClass ->setAge(100);
printf("\n%d", m_TestClass->getAge());
}

the output of the program is first 100 and then it is 4( sometime it is 6, sometime some crazy values), I was wondering why not my program is printing 100 twice?

After spening some time on it I got the cause .. as "obj" has a local scope( in the routine createTestClassObject ), hence the object dies when program flow returns to "main" and m_TestClassbecome a Dangling Pointer( I hope I am correct :) ).

I was just trying out several options to fix this issue ... and one of them paid off , this is what I did ...
I have just modified the createTestClassObject routine .. and the updated version looks like........
void createTestClassObject()
{
TestClass *obj = new TestClass();
m_TestClass = obj;
m_TestClass ->setAge(100);
printf("\n%d", m_TestClass->getAge());
}

All I have changed is created a pointer of TestClass and it points to new instance of TestClass, and it started working, but all in all I am confused, doesn't the object( new TestClass() )get dies when the routine returns to the main function ? I am in dark, can anybody please throw some light.
Neo.





Wednesday, July 20, 2005

XML Writer and encoding schemes

This all started when my friend Richard asked me to look into some code snippets, all he was trying to do is framing a xml document and flushing it to the console .. and although he has specified utf-8 as encoding scheme( he has used XmlWriterSetting object for this purpose ), but when it comes to console it was like this

so .. it is confusing .. isn't it ? I will request to read his blog as well .. < http://blogs.geekdojo.net/richardhsu/archive/2005/07/16/8675.aspx >
one thing to mention Richard has written codes in .NET Framework 2.0 ... so if you try his code snippet in framwwork 1.1 .. you will get couple of compilation errors.

I did some research on the code and I think I have found the reason of IBM437 issue. Please look at the code folliowing code sinppets ... ( I have used framework 1.1 )

private void btnWriter_Click(object sender, System.EventArgs e)
{
try
{
string fileName = @"C:\test.txt";
System.IO.TextWriter tx = new System.IO.StreamWriter( fileName );
XmlTextWriter writer = new XmlTextWriter( Console.Out );
//XmlTextWriter writer = new XmlTextWriter( tx ); // this will give u utf-8 encoding.

//Console.Out.Encoding = System.Text.Encoding.UTF8; // can't do this..!!!
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument( true );
WriteQuote(writer, "MSFT", 74.125, 5.89, 69020000);
writer.Flush();
writer.Close();
MessageBox.Show( " Done ");
}
catch(Exception ex)
{
MessageBox.Show( ex.Message );
}
}

private void WriteQuote(XmlWriter writer, string symbol,
double price, double change, long volume)
{
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", symbol);
writer.WriteElementString("Price", XmlConvert.ToString(price));
writer.WriteElementString("Change", XmlConvert.ToString(change));
writer.WriteElementString("Volume", XmlConvert.ToString(volume));
writer.WriteEndElement();
}

My understanding ....
Console.Out gives you an object of type TextWriter( which is essentially point to console .. unless you are doing any console.SetOut- > and pointing the output to any object, other than console ) and if you look at the encoding scheme of this TextWriter object, it is IBM437 .. just use this simple code you can get the encoding scheme of the Console.Out ..

System.Text.Encoding enc = Console.Out.Encoding;
MessageBox.Show( enc.HeaderName);

and unfortunately it is a Readonly property, -:(

Now .. the most important point is Richard have used another object XMLWritersetting .. which is a new addition of .NET framework 2.0( I have played very lil time with 2.0 framework ) ... I am not sure if we set encoding scheme using this object whether it will override the default encoding scheme, if it should .. then it's just another bug in the MS code .. -:).
alternatively
1) we can use a StreamWriter object which also inherits TextWriter object and here the default encoding scheme is utf-8, which solve the purpose.
2) we can use WriteProcessingInstruction method which overrides the default behavior , which Richard does as well.





Tuesday, July 19, 2005

Complicated Heart

Neo's den: July 2005

Love... emotion .. anger ... happiness .. they say it all comes from heart. Sometime I wonder about the mechanism of this machine ..well .. huh .. is it a machine ? I had a big fight with my sys Esha on this. anyway ... so what if it malfunction ? like it will only produce love nothing else ... I think we can avoid all the wars ... and hatred.. and bla bla .. which are the cause of all the problems we have today. Imagine we have got the mechanism to control heart ... and we will pull out the parts from the heart which produce Anger/hatred ..etc etc .. ( I never said it will be easy ).
then hey presto .. all the problem solved .. we don't have to worry about another world war or another 9/11 or 7/7. we will all live in peace for ever.
Lastly I want to add a joke ... why love is easier to promote than war ? hmmm .. condom is cheaper than even a bullet( forget gun ) .. nice one .. huh ?

Saturday, July 09, 2005

sfdsf