February 2006 - Posts

Creating an Object at Runtime
28 February 06 11:11 PM | adrian | with no comments

Abstract: No, it's not that usual Dim a As New Object kind of thing. This one's using unknown class (at design time), from unknown assembly (at design time).

Context: you want to instantiate a certain Class during runtime. The Class and the Class' Assembly is unknown at design time, therefore cannot be “strong”-typed.

Solution: Using classes from System.Reflection namespace, we can achieve this in only in one line of code:

Dim object As Object = Reflection.Assembly.LoadFrom(assemblyFileName).CreateInstance(classNameFull)

There are two input strings you must provide:

  • assemblyFileName: replace with the EXE or DLL file name. Ex: “System.dll“
  • classNameFull: replace with the full name (incl. the Namespace) of the Class to instantiate. Ex: “System.Object“

In many case, we don't have control over the name of the Class to instantiate, but we do have some workaround. One method is to use subclassing. The Class we are going to instantiate inherits a specific Class already defined at design time and is known to everyone. We are going to instantiate the Class, but will use only a part of it (only the inherited ones). The code would be:

Dim assemblyPackage As Assembly = Assembly.LoadFrom(assemblyFileName)
Dim object As parentClass

For Each assType As Type In assemblyPackage.GetTypes
  If assType.IsSubclassOf(GetType(parentClass)) Then
    object = CType(assemblyPackage.CreateInstance(assType.FullName), parentClass)
  End If
Next

  • parentClass: replace with the Class everyone inherited from. And since it is known at design time, you will get strong type support.

This method also adds strong typing to the object instantiated. OO purist would agree that subclassing is just overkill. Therefore a lighter method is using Interface to define contracts to implementors. We can use the following snippet to instantiate a Class that implements a known Interface.

Dim assemblyPackage As Assembly = Assembly.LoadFrom(assemblyFileName)
Dim object As IImplemented
For Each assType As Type In assemblyPackage.GetTypes
  For Each interfaceType As Type In assType.GetInterfaces
    If interfaceType.GUID = GetType(IImplemented).GUID Then
      object = CType(assemblyPackage.CreateInstance(assType.FullName), IImplemented)
    End If
  Next
Next

  • IImplemented: same thing like parentClass above, the Interface everyone is implementing.

Advanced users should try looking at Type.FindInterfaces method to filter in more than one Interface. You can also find more anti-OO tools in the System.Reflection namespace.

Conclusion: the more you know about the Class you are instantiating, the less code is needed to instantiate the Class. And since performance isn't the biggest problem here, feel free to use the easiest method for your clients/implementors. And don't forget those try-catch blocks to catch illegal Assembly/Class.

P.S.: Useful when using the Generic Strategy Pattern.

Share this post: | | | |
Filed under:
Mini-Competition!
27 February 06 09:26 PM | adrian | 9 comment(s)

Write a function that returns all permutations of a given string, in this case: “INDCRulez!” (10 character)

How:

  1. Download the SDK to compete.
  2. Create an implementation of PermutationTestBench.IPermutationGenerator interface.
  3. Run ptb.exe assemblyname.dll classname, and take notes of your score in milliseconds.

Prize: theSpoke backpack (Dondy: I know you want this, earn it!)

Timeframe: you have until this weekend (Satuday, 4 March 2006, 12:00 a.m. GMT) to e-mail me your DLL. My e-mail: adrian.godong@gmail.com.

Rules:

  • Managed code only.
  • Time taken is the smallest in three execution.
  • Prizes are for Indonesian area only (I can't afford international shipping), but submissions are international.

Current standing:

1. Adrian Godong (reference Permutation.dll) 6609,375 ms.

Share this post: | | | |
Start Coding the Future!
27 February 06 05:57 AM | adrian | with no comments

Here'e the one page that you need to start developing the next big thing from Microsoft.

http://msdn.microsoft.com/windowsvista/getthebeta/default.aspx

Now, if only those SDKs are smaller... And, BTW, these files are going to be in the Imagine Cup 2006 DVD Starter Kit. Can't wait to see those DVDs coming!

Share this post: | | | |
Upgrading Visual Basic 6
26 February 06 03:40 PM | adrian | with no comments

This was a session I brought last Friday at the Community in Touch held at Microsoft Indonesia office. You can download the slides here. It was supposed to have good demos, but lack of laptop prep kind of ruined it. Maybe I should consider buying my own laptop?

Joule: thanks for the laptop man! Can't live without it last Friday...

UPDATE: David Boschmans' Vista To Support Legacy VB6 Apps

Share this post: | | | |
String Permutation Generator
25 February 06 10:46 PM | adrian | 13 comment(s)

From yesterday's .NET Internal Meeting (dIM): Generate a list of permutation of a string, optimize for speed and space. Here's the code:

Dim inputString As String = Console.ReadLine

For x As Long = 0 To Math2.Factorial(inputString.Length) - 1

  Dim outputstring As String() = New String(inputString.Length) {}

  Dim outputInteger As Integer() = _

    Math2.Permutation(inputString.Length, x)

 

  For y As Integer = 0 To outputInteger.Length - 1

    outputstring(y) = inputString(outputInteger(y))

    Console.Write(outputstring(y))

  Next

  Console.WriteLine()

Next x

I need to use two functions not available in System.Math, so I’ve extended to System.Math2, containing two functions; Factorial and Permutation. Factorial is a standard recursive multiplication, while Permutation uses a more advanced technique. I stumbled on this technique while doing a small simulation project (where a random permutation of 20 elements was needed). Got it from MSDN, the link? Go find it yourself…

Using Permutation function, I need to enter two arguments, the number of elements, and the permutation “ID” to return.

Finally, it is just a matter of conversion between integers and characters (the original Permutation function is designed for a series of integers, not characters).

You’ll need the Permutation function to run the code above. Drop me a comment if you need it (or unsuccessful looking for it on MSDN)!

Share this post: | | | |
Introducing Microsoft Expression...
20 February 06 08:15 PM | adrian | with no comments
This is a presentation I brought to the fellow students of Universitas Indonesia last Friday. The ppt file can be downloaded from this link. You should read the slide notes, the ppt itself just not self-explanatory.
Share this post: | | | |
DirectX 10 vs. Windows Presentation Foundation
19 February 06 03:53 PM | adrian | 5 comment(s)

An interesting, but wrong statement popped up yesterday at Universitas Indonesia.

Aristo: “Avalon is deprecated, and DirectX 10 will replace it.”

Sceptical, I'm trying to find some answers on MSDN Forums (hey, the forum is nicer than before!) at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=257236&SiteID=1.

The answers are:

  • There are no more version number in DirectX. That means, there will be no DX10, DX11, and so on, but there is only one DirectX, no version. What being versioned are the components. For example, DirectX February SDK includes Beta Direct3D 10 (which will only work in Vista).
  • Avalon was renamed to Windows Presentation Foundation, no questions asked.
  • Windows Presentation Foundation will use DirectX API/internals to render the User Interface, so both of them will exist in Vista.

One statement, and one question.

“Will Microsoft Expression replace Adobe Photoshop or Corel Draw?”

Short answer, no.

Long answer, IMO, Expression will become a reference application for graphic editing suite in Vista. It uses WinFX (especially WPF) and it has market for deesigners who develops application. Not professional digital image editors, not professional desktop publishers. So Photoshop and Draw should exist in Vista for professional graphic markets.

Share this post: | | | |
Two Long Days
18 February 06 09:22 PM | adrian | with no comments

17.02.06 Imagine Cup Campaign, Universitas Indonesia

01:30 Finished presentation design on “Introducing Microsoft Expression“.

05:30 Woke up to go to class.

07:00 Went to campus (which means I missed my class)

07:30 Found out that the card reader does not work without AC adaptors. That means I cannot access my presentation resources.

08:30 Finally found a working card reader, copied the resources and went to print them.

09:00 Breakfast, finally!

10:00 On the road to MS Office, Jakarta.

12:15 On MS Office (ha! two hours and fifteen minutes)

12:30 Finished “look self-check”...

13:00 Lunch at name-easy-to-forget Chinese restaurant at JSXB. Two noodle-soups, one salty noodle, two lime juices, one honey-lime, one Diet Coke, one milk sode, five pieces of spring rolls, and six pieces of chicken dumplings.

14:00 Finally, off to Universitas Indonesia Depok...

15:00++ Arrived at Univeristas Indonesia, late. Setting up the stage for “Imagine Cup 2006 Campaign“. Zeddy talked first, then Umar, then...

16:35 Starting up my presentation.

17:05 An assassin-type guy questioned my presentation (luckily, I know this guy! Ha!)

17:15 Finished presenting, with the biggest applause I ever had! THANK YOU, THANK YOU!!!

17:30 On the way to Gambir.

19:00 Oops... No space left on trains going to Bandung. Rent a car? Fly by plane?

19:15 Nope, we're going home with my family.

20:00 Got off taxi, reloaded the stuff (these things are HEAVY and BULKY!) to the car taking us to Bandung.

20:30 Got some cozy-looking place to eat dinner.

21:00 Rain starts pouring down. 5 minutes later, it's no longer raining, TROPICAL RAINSTORM!

22:00 No sign of clear skies, yet.

22:15 Finally we're able to move on to drier ground.

22:30 Off to Bandung.

18.02.06 IT Within, Institut Teknologi Bandung

00:30 Arrived at Jayakarta. We have decided that Sheraton is a nicer place, with Internet.

01:00 Done taking a long bath. Sleeps.

03:00 Zed, wake up, create your presentation!

05:00 Zeddy was still in bed.

06:00 Zeddy finally moved up to his laptop.

06:30 Morning shower. Did I just bathed?

07:00 Breakfast; bread, croissant, butter, blueberry jam, raw toasts, tea, rice, strange-named veggies, chicken, sausages, sunny-side-up, tea, milk, (croquette) potatoes, fish fingers, sausages (again), tea, and finally orange.

08:00 Zeddy finally got his Speech to Text working. “Call 123456...” Sorry, calling a number works in PDA only, he used a smartphone.

09:15 Checked out, and can't find a good taxi.

09:30 Finally used the previously rejected taxi.

09:45 Dropping off the stuff to the expo booth. FYI, it's trial CDs from every Product Manager in MS Office... I think it was around 2K pieces of CD and DVDs.

10:00 Zeddy started his presentation about Windows Mobile 5.0 to the Windows Mobile 5.0 workshop audience.

11:00 Back to the expo booth. If other booths are filled with products, logos, discounts, and Sales Promotion Girls, Microsoft booth are filled with free CD/DVDs, Ready to Launch posters, Sales Promotion Guys, and, of course, audiences. We literally held the whole expo floor!

14:00 Finally I stopped explaining what Visual Studio, BizTalk Server, Infopath, OneNote, Publisher are and stopped giving them away because none left. Phew, what a talk. I must've said “Visual Studio - a development tool” like a hundred time?

14:30 Zeddy went to watch S.K. Chong speaking about hacking (with Perl).

15:30 Finally, off for a real lunch. But, no taxi.

15:45 Got a nice taxi. Drove to nearest Pizza Hut.

16:00 Starts ordering; American Favorite large with Cheesy Crust, Hawaiian Chicken large with Cheesy Crust, eight pieces of Garlic Breads with Mozzarella, ten pieces of Chicken Wings, a pitcher of Lipton, and a pitcher of Coke.

17:00 Zeddy off to the train station. I was trapped on one side of the road because of a small stream of water coming out of nowhere.

18:15 Home, at last.

21:00 Writing this...

To be written:

  • Avalon vs. DirectX 10 vs. Windows Presentation Foundation
  • Some good questions (and answers) from the Imagine Cup Campaign
  • Upload my presentation slide, won't do much good without notes.
Share this post: | | | |
MSN Search UI
16 February 06 10:21 PM | adrian | 9 comment(s)

Introducing the new MSN Search UI (and search improvements), are we finally be able to move away from the Evil Google EmpireTM?

Check http://search.msn.com now to prove it!

Share this post: | | | |
Filed under:
Keeping Updated
14 February 06 11:37 PM | adrian | with no comments

It's been 2 whole weeks since last blog post. I'm currently busy setting up new term schedules and a personal web site.

http://adrian.godong.or.id

It won't replace this blog, but will provide an extension of some sort. See that .Text is missing file upload features?

I also posted a list of things I've done for the last 9 years, here: http://blogs.netindonesia.net/adrian/articles/8587.aspx

Share this post: | | | |
Filed under: