December 14, 2005

How does StringAssert.Equals work?

Posted in C Sharp at 6:33 am by Frank

While trying some futures of unit testing in Visual Studio 2005 team suite, I never suppose StringAssert.Equals(“abcde”, “fgdhi”) will pass.  It  puzzles me  a lot.  And  I have to  use  Assert.AreEqual  in the end.  How  does it work? It seems useless and will often confuses testers.

December 6, 2005

Tried NHibernate

Posted in C Sharp at 3:32 am by Frank

Hibernate is so cool. So when I heard of its .net clone NHibernate I couldn’t wait to try have a look at it. NHibernate is not so adult as hibernate. Now its newest realease is 1.01. It should be stable I think. When I google on the internet to see if there are some good materials to learn it. I found a quickstart yestoday, and I went through it after a few hours as I am not familiar enough with some .net conceptions. And today I found an article at codeproject named nhibernate in real world applications. It seems very helpful. When I downloaded it and ran it in the visual studio, I got errors. And I found some people met the same error with me in the comments. When I read their discussion I settled it. The problem is because the imcompatibility between and ISet and IDirectory. Collections in .net now is not so powerful as that in jdk. e.g, it’s in lack of ISet. But hibernate uses many Set collections. NHibernate now use Iesi.Collections.ISet to do set-maps.

Someone in the comments of the article said “nHibernate is garbage“. I don’t know if it’s a good choice in real life projects. Since much time must be paid to learn it and many strange problems will occur during the learning and every problem is not so easy to be settled.

December 1, 2005

IronPython demo video on MSDN

Posted in brief, C Sharp, Python at 3:00 am by Frank

The video has been on the msdn site for 20 days but I just happened saw it today. Jim Hugunin, the author of Jython, is now working in ms on IronPython project. I heard it a few months ago. The demo shows how to use clr classes from inside ironpython, and then gives a few UI examples. There’s also a good news that the full feature of debuging in visual studio are available for ironpython. I hope vs will offer some good support in the python editing. Python is so lack of a powerful IDE like visual studio. But I think when ironpython 1.0 realeases, this will be totally changed, because ironpython will offer nearly full support for CPython. I can write CPython script in visual studio then. I really can’t wait it.

November 22, 2005

Streams in .net framework(2)

Posted in C Sharp at 3:11 am by Frank

Copying a file
Copy a stream from one the another is just to read and write the data byte by byte. The following code shows how to copy a file from srcfile to destfile:
public static void CopyUsingFileStream(string srcfile,string destfile)
{
    FileStream srcf=new FileStream(srcfile,FileMode.Open,FileAccess.Read);
    FileStream destf=new FileStream(destfile,FileMode.Create,FileAccess.Write);

    byte[] buff=new byte[1024];
    int len=0;
    while((len=srcf.Read(buff,0,buff.Length))!=0)
          destf.Write(buff,0,len);

    srcf.Close();
    destf.Close();
}

Compressing/Decompressing a file
.Net 2.0 provides us a GzipStream class, which can compress/decompress a basic stream. It’s defined in System.IO.Compression. It’s very easy to use just as the other streams do. I guess .net here use decorate pattern, like jdk streams do. The function ZipFile demonstrates how to compress a file srcf and save the compressed file to destf, and UnzipFile can decompress a compressed file srcf and save it as file destf.
public static void ZipFile(string srcf,string destf)
{
    Stream s = new GZipStream(new BufferedStream(new FileStream(destf, FileMode.Create, FileAccess.Write)), CompressionMode.Compress);
    Stream f = new FileStream(srcf, FileMode.Open, FileAccess.Read);

    byte[] buff = new byte[1024];
    int len = 0;
    while ((len=f.Read(buff, 0, buff.Length)) != 0)
    s.Write(buff, 0, len);

    f.Close();
    s.Close();
}

public static void UnzipFile(string srcf, string destf)
{
    Stream dest = new FileStream(destf, FileMode.Create, FileAccess.Write);
    Stream source = new GZipStream(new BufferedStream(new FileStream(srcf, FileMode.Open, FileAccess.Read)),CompressionMode.Decompress);

    byte[] buff = new byte[1024];
    int len=0;
    while((len=source.Read(buff,0,buff.Length))!=0)
        dest.Write(buff,0,len);

    dest.Close();
    source.Close();
}

Collections of .net framework

Posted in C Sharp at 2:51 am by Frank

Collections of .net framework are defined in namespace System.Collections, the class diagram of the namespace are as below:

13

To master the collections, the 3 interfaces is the key:IEnumerable,IEnumerator,ICollection. Nearly all the collections in .net framework implement these interfaces. ICollection indicates a class to be a collection class, it defines a Count property, which is very useful in Queue or Stack like class. IEnumerable can return an IEnumerator interface, which can iterate through a collection.

Usual usage of collections is like this:
//define a collection such as ArrayList, Queue, Stack
ArrayList al=new ArrayList();
//add some data in the collection
al.Add(new MyClass());
//Get the IEnumerator interface and iterate through it
IEnumerator ie=al.GetEnumerator();
while(ie.MoveNext())
{
        MyClass mc=ie.Current as MyClass;
        //do something with mc
}

Every collection class can be iterated the same way, but as IEnumerator.Current is an Object, it must be cast to proper class which you want, sometimes it’s boring. But by using foreach, it can be cast implicitly:
foreach(MyClass mc in al)
{
        //do something to mc, it needn’t be cast manually anymore
}

To use iterator or to use foreach, it’s up to you. But I like iterator, despite it’s a little fussier.

Follow

Get every new post delivered to your Inbox.