Scott Hanselman’s 2007 Ultimate Developer and Power Users Tool List for Windows

May 18th, 2009 Comments off

What else can i say, a must read!

http://www.hanselman.com/blog/ScottHanselmans2007UltimateDeveloperAndPowerUsersToolListForWindows.aspx

Share
Categories: Random Talk Tags:

LINQPad: A Lightweight SQL Studio Replacement for LINQ Developer

March 27th, 2009 Comments off

Are you those people like me occasionally need try-and-error in order to figure a relative complicated LINQ statement?linqpadlogo

Are you those people like me love to have a interactive LINQ shell to evaluate ideas quick ? Then here you go!

 

LINQPad lets you interactively query SQL databases in a modern query language:LINQ.  Kiss goodbye to SQL Management Studio!

LINQPad supports everything in C# 3.0 and Framework 3.5:

  • LINQ to SQL
  • LINQ to Objects
  • LINQ to XML

 

For basic features it is free, and it only costs $7 bucks to get the Auto-completion, which is a must have IMHO. And the best part is you can even run code snippet in the editor!

image

 

Nice table views :)

 

Technorati Tags:
Share
Categories: Linq, Random Talk Tags: , ,

LINQ: Generate File System Hierarchy in XML

March 26th, 2009 Comments off

I happen to come across the need to export file system hirachy into an XML form for further processing, and here is my LINQ statements that does the job. Please note the recursion usage in XDirectoryElement function. Last word you need to ensure you have permission to read the directories, which doesn’t worry me in my case.

Main Function:

private static XElement XDirectoryElement(String root, String filespec)
{
return /// Create "Directory" element with Path attribute
new XElement("Directory", new XAttribute("Path", root),
new XElement("Files",  /// Add "Files" sub-element
from file in Directory.GetFiles(root, filespec)orderby file
select new XElement("File", Path.GetFileName(file))),
new XElement("Subdirectories", /// Add "Subdirectories" sub-element
from dir in Directory.GetDirectories(root) orderby dir
select XDirectoryElement(dir, filespec)));  /// Do it recursively!
}

The usage of the function:

String root = @"C:\Program Files\Microsoft Games";
root = Path.Combine(root, @"");
XElement xde = XDirectoryElement(root, "*.*");
String filename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SubDir.xml");
xde.Save(filename);
Process.Start(filename);  /// Open explorer to view the file.

A trimmed down version of the Output:


< ?xml version="1.0" encoding="utf-8"?>
<directory Path="C:\Program Files\Microsoft Games">
  <files />
  <subdirectories>
    <directory Path="C:\Program Files\Microsoft Games\Chess">
      <files>
        <file>Chess.dll</file>
        <file>Chess.exe</file>
        <file>ChessMCE.lnk</file>
        <file>ChessMCE.png</file>
        <file>desktop.ini</file>
      </files>
      <subdirectories>
        <directory Path="C:\Program Files\Microsoft Games\Chess\en-US">
          <files>
            <file>Chess.exe.mui</file>
          </files>
          <subdirectories />
        </directory>
      </subdirectories>
     </directory>
</subdirectories>
</directory>

Share
Categories: Linq Tags: , ,

Use DIVs to layout grid form controls in ASP.Net

March 24th, 2009 Comments off

It is interesting that people tend to usie tables to layout contents in a Page, or controls in ASP page. Because this may be one of the easiest way to do the layout job quick. However to restruct the layout is always a headache.

I guess in ASP.net the most typical page layout is form, it consists of labels controls, textboxes, dropdown boxes, and sometimes datagrids etc. I prefer to use DIV blocks to control layouts instead of table.

I use the stylesheet to class the div tags, an example is below:

in file:mycss.css

DIV.base-layer {
background: none ; border:
margin: 0.5em 12px 0.5em 12px; padding: 0; text-align: center; width: 80%;}
DIV.table-row {
border:1px;
margin-top: 0; margin-right: auto; margin-bottom: 0; margin-left: auto;}
DIV.left-cell41 {
border: none; float: left;border: thin solid red 1px; margin: 0; padding: 0; width: 20%;}
DIV.left-cell42 {
border: none; float: left;border: thin solid blue 1px; margin: 0; padding: 0; width: 30%;}
DIV.left-cell43 {
border: none; float: left;border: thin solid green 1px; margin: 0; padding: 0; width: 20%;
overflow:visible;}
DIV.left-cell44 {
border: none; float: left;border: thin solid yellow 1px; margin: 0; padding: 0; width: 29%;
visibility:visible;
overflow:visible;}

in mysample.html (or you can put that in aspx)

<html>
<head>
<style type=”text/css”>@import url(mycss.css);</style>
</head>
<body>
<div class=”base-layer”>
   <div class=”table-row” >
        <div class=”left-cell41″>cell1</div>
        <div class=”left-cell42″>cell2</div>
        <div class=”left-cell43″>cell3</div>
  <div class=”left-cell44″>Last cell</div>
</div>
</div>
</body>
</html>

However there is still a problem with overflowing, when the text in the cell is over the size of the cell, the text draws outside the box.
I am still doing research to seek for solution

Share
Categories: ASP, Tech Tags: , , ,

C# function parameters passing

March 24th, 2009 Comments off

Yes, C# doesn’t support variable number of parameters in function signature,
by default, according to MSDN, the parameters passed by value:

A parameter declared with no modifiers is a value parameter. A value parameter corresponds to a local variable that gets its initial value from the corresponding argument supplied in the method invocation.

But there is something more interesting than that:

ref keyword: pass the value of the parameter into the function body by reference.

void addme(ref int i){
i++;
}
...... in some function body..
i = 10;
addme(i);
Console.WriteLine(i); /// output 11;

out keyword: sometimes a single retrurn is not enough; why not use the input parameter as return value container? the parameter specifies with out keyword doen’t need to be initialize.
params keyword: A parameter declared with a params modifier is a parameter array. If a formal parameter list includes a parameter array, it must be the last parameter in the list and it must be of a single-dimensional array type. For example, the types string[] and string[][] can be used as the type of a parameter array, but the type string[,] cannot. It is not possible to combine the params modifier with the modifiers ref and out.

using System;
class Test
{
static void F(params int[] args) {
Console.Write(”Array contains {0} elements:”, args.Length);
foreach (int i in args)
Console.Write(” {0}”, i);
Console.WriteLine();
}
static void Main() {
int[] arr = {1, 2, 3};
F(arr);
F(10, 20, 30, 40);
F();
}
}

Output is
Array contains 3 elements: 1 2 3
Array contains 4 elements: 10 20 30 40
Array contains 0 elements:
For details you can refer to MSDN document HERE

Share
Categories: C#, Tech Tags: , ,

C# modifiers for classes (C), methods(M), properties(P), indexers(I), events(E), field(F) and operator(O)

March 22nd, 2009 Comments off

There are quite a few the C# modifiers, depends on the modifiers, they can be used on classes (C), methods(M), properties(P), indexers(I), events(E), field(F) and operator(O) here are the summary (which may be imcompleted). abstract Usage: C M P I E const Usage: P Note: Only can be initialize in declearation field. All const are static! readonly Usage: F NOTE: Assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. You can only pass the readonly var into a method with ref or out modifier. event Usage: M (delegate) Note: An event must modify a deligate, it acts as a subscription point for other methods with the same signiture of the deligate it modifies. You need to declear a delegate, then associate the event with it. // event_keyword.cs using System; public delegate void MyDelegate(); // delegate declaration extern Usage: M Note: Use the extern modifier in a method declaration to indicate that the method is implemented externally. A common use of the extern modifier is with the DllImport attribute. (Similar to C/C++ external link)

public interface I {  
event MyDelegate MyEvent;  
void FireAway(); 
} 
public class MyClass: I {
  public event MyDelegate MyEvent;
  public void FireAway(){
   if (MyEvent != null)
   MyEvent();  
  } 
} 

public class MainClass {
  static private void f()  {
   Console.WriteLine{"This is called when the event fires.");  
  }
  static public void Main ()  {
   I i = new MyClass();
   i.MyEvent += new MyDelegate(f);
   i.FireAway();
  } 
}

override Usage: M P I E Note: The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. You cannot override a non-virtual or static method. Both the override method and the virtual method must have the same access level modifier. new Usage: M P F Note: use it to hide the parent class’s member. sealed Usage: C M P Note:A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class. Structs are implicitly sealed; therefore, they cannot be inherited. static Usage: F M P O E constructor Note: A constant or type declaration is implicitly a static member. F/M/P/O/E: class level static, must access from class level Class.method or Class.field Constructor: Static constructor is a special method which gets called when the class assembly get loaded, and NO extra modifier for static constructor unsafe Usage: M Note: The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. virtual Usgae: M P Note:By default, methods are non-virtual. Virtual enables dynamic binding. volatile Usage: F Note: It indicates modified Object can be chaanged unexpectedly. The type of a field marked as volatile is restricted to the following types: Any reference type. Any pointer type (in an unsafe context). The types sbyte, byte, short, ushort, int, uint, char, float, bool. An enum type with an enum base type of byte, sbyte, short, ushort, int, or uint. unchecked Usage: Block Note: The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

unchecked {
  int val = 2147483647 * 2; 
} 
public int UncheckedAdd(int a, int b) {
  return unchecked(a + b); 
}
Share
Categories: C#, Tech Tags: , , ,

Different thinking styles

March 21st, 2009 Comments off

A good lawyer friend and me drove to a petrol station to refill. my friend tried to open the car door, and it seemed to stuck. He kept trying for another 30 seconds and felt pretty pissed. Certainly, it happens first time. I then suggested him to low down the window a bit, and open the door again (since the car is convertable), and he got the car door opened by following the suggestion.
Everything was not ammusing so far, I went into the shop while my friend was refilling and cleaning up the front screen of the car. Here is the conversation between me and shopkeeper.

SK: Good day! What is the job of your friend.
Me: lawyer.
SK: You?
Me: Engineer.
SK: No wonder he couldn’t open the door of a nice car!
(I shocked and started to laugh)
SK pointed at my friend and he was wiping the front screen of the car.
SK: Look! Lawyer doesn’t know how to clean up front screen of a car! He wiped up to down instead of left-right, it leaves couples of verticle marks on your front screen instead of a horizontal one.
I started to laugh…. it is true, and I always do it form left to right since I know it is better even I won’t think of the reason.

Thoughts: That is true, as an engineer, we tends to think why a thing should work like X, for a lawyer, like my friend, he is trained to think a thing should always work like X since it should. Nothing wrong with both ways, but I do appreciate my engineering thinking style most of the monents. Engineers creates tools as tools, lawyer can use languages as tools. Right and software engineers can use computer languages as tools as well. Both are about creativity, but obviously in different ways :)

Share
Categories: Random Talk Tags:

A story of a (almost) Microsoft Cleaner

March 21st, 2009 Comments off

I just come across the story in one of my email, it is def a nice read and worth thoughts…

An unemployed man goes to apply for a job with Microsoft as a janitor. The manager there arranges for him to take an aptitude test (Section: Floors, sweeping and cleaning).

After the test, the manager says, “You will be employed at minimum wage. Let me have your e-mail address, so that I can send you a form to complete and tell you where to report for work on your first day.” Taken aback, the man protests that he has neither a computer nor an e-mail address. To this the MS manager replies, “Well, then, that means that you virtually don’t exist and can therefore hardly expect to be employed.”

Stunned, the man leaves. Not knowing where to turn and having only $10 in his wallet, he decides to buy a 25 lb. flat of tomatoes at the supermarket. Within less than two hours, he sells all the tomatoes individually at 100% profit.

Repeating the process several times more that day, he ends up with almost $100 before going to sleep that night.

And thus it dawns on him that he could quite easily make a living selling tomatoes. Getting up early every day and going to bed late, he multiplies his profits quickly. After a short time he acquires a cart to transport several dozen boxes of tomatoes, only to have to trade it in again so that he can buy a pick-up truck to support his expanding business.

By the end of the second year, he is the owner of a fleet of pick-up trucks and manages a staff of a hundred former unemployed people, all selling tomatoes. Planning for the future of his wife and children, he decides to buy some life insurance. Consulting with an insurance adviser, he picks an insurance plan. At the end of the telephone conversation, the adviser asks him for his e-mail address in order to send the final documents electronically.

When the man replies that he has no e-mail, the adviser is stunned, “What, you don’t have e-mail? How on earth have you managed to amass such wealth without the Internet, e-mail and e-commerce? Just imagine where you would be now, if you had been connected to the internet from the very start!”
After a moment of thought, the tomato millionaire replied, “Why, of course! I would be a floor cleaner at Microsoft!”

Moral of this story:

Seeing that you got this story via e-mail, you’re probably closer to becoming a janitor than you are to becoming a millionaire.

Share
Categories: Random Talk Tags: , ,