Object and Collection Initializers (C#)

C# 3.0 was released as a part of the Microsoft .NET 3.5 Framework. In this blog post, I’ll cover the object and collection intitializers feature of C# 3.0. Initializers introduce a concise syntax that combines object creation and initialization in a single step. It even allows for parentheses to be optional for paramaterless constructors as shown below:

 
Point p = new Point {
   X=3,
   Y=99 };
 
Employee myEmployee = new Employee() {
   Name       = "John Doe",
   Position   = "Marketing Manager",
   Department = "Marketing" };
Initializers work very well with constructors as shown below:. The following code snippet shows an instance where a parameterized constructor is called along with remaining properties initialized.
 
class Customer
{
   public Customer(string customerKey) { … }
   public string CustomerKey { get { … } }
   public string ContactName { get; set; }
   public string City { get; set; }
}
 
var c = new Customer("MARKS") {
   ContactName = "Mark Strawmyer",
   City = "Indianapolis"
}

myVSTS October 2009 Technical Talk (Presentation Slides)

Last week myVSTS user group invited Ms. Lai Ee to deliver a talk at the user group. Thanks for those who attended my talk. The following are the details of the talk:

 
Topic:
Improving Software Processes Through Application Lifecycle Management
 
Synopsis:
Microsoft Visual Studio Team System 2008 is a platform for productive, integrated, and extensible software development life-cycle tools. It helps software teams by improving communication and collaboration throughout the software development process. Application Lifecycle Management involves the co-ordination of developmental activities throughout the lifecycle of a project. In a software development project, developmental activities include modeling, code development, build, testing, and so on. This talk provides an overview of how Microsoft Visual Studio Team 2008 facilitates streamlined and efficient development of software in a team environment.
 
Agenda:
06:45pm – 07:00pm Registration
07:00pm – 07:15pm Keynote
07:15pm – 08:15pm Application Lifecycle Management Using Microsoft Visual Studio Team System 2008
08:15pm – 08:30pm Dinner
 
Just managed to snap 1 photo. 🙂 Thanks to Patrick. You may visit the album at: http://cid-d1df34a904545dc5.skydrive.live.com/browse.aspx/.res/d1df34a904545dc5!746?ct=photos
 

Setup a Hidden Camera for Watching a Folder (C#)

Watching activities inside a folder is often a task for application servers. For example, you have an application (APPLICATION1) which needs to wait until another application (APPLICATION2) uploads an XML file into a special folder. Once APPLICATION2 drops the file into the folder, APPLICATION2 does some processing according to the file contents.
 
using System.IO;
 
// Watches the C:\Temp folder and notifies creation of new text files
public static void WatchTempFolder()
{
    // Create the FileSystemoFileSystemWatcher object and set its properties
    FileSystemWatcher oFileSystemWatcher = new FileSystemWatcher();
    oFileSystemWatcher.Path = "C:\\Temp";
    oFileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess
          | NotifyFilters.LastWrite | NotifyFilters.FileName |
            NotifyFilters.DirectoryName;
    oFileSystemWatcher.Filter = "*.txt";
    // Add event handlers.
    oFileSystemWatcher.Created += new FileSystemEventHandler(OnCreated);
    // Begin watching.
    oFileSystemWatcher.EnableRaisingEvents = true;
    // Wait for the user to quit the program.
    Console.WriteLine("Press \’q\’ to quit the sample.");
    while(Console.Read()!=’q’);
}
 
// The event handler
private static void OnCreated(object source, FileSystemEventArgs e)
{
    Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}

Writing to Windows Event Log (C#)

You have written a server-based application. But as is the case with most servers, it runs in background, either as a Windows Service or may be with a hidden window. Now, how are you going to notify the System Administrator, in case some failures or important events occur? I would recommend that you use the Windows Event log for this purpose.
 
using System.Diagnostics;
 
public void WriteEventLog(string sCallerName, string sLogLine)
{
  if (!System.Diagnostics.EventLog.SourceExists(sCallerName))
    System.Diagnostics.EventLog.CreateEventSource(sCallerName, "Application");
 
  EventLog EventLog1 = new EventLog();
  EventLog1.Source = sCallerName; 
  EventLog1.WriteEntry (sLogLine, EventLogEntryType.Warning);
}
 
Note: Use the relevant EventLogEntryType for your application.

myVSTS October 2009 Technical Talk

myVSTS user group is organizing a technical talk this month. Admission is free. We have invited Ms. Lai Ee to share with us on ways to improve software processes through application lifecycle management. The following are the details of the talk:
 
Topic:
Improving Software Processes Through Application Lifecycle Management
 
Synopsis:
Microsoft Visual Studio Team System 2008 is a platform for productive, integrated, and extensible software development life-cycle tools. It helps software teams by improving communication and collaboration throughout the software development process. Application Lifecycle Management involves the co-ordination of developmental activities throughout the lifecycle of a project. In a software development project, developmental activities include modeling, code development, build, testing, and so on. This talk provides an overview of how Microsoft Visual Studio Team 2008 facilitates streamlined and efficient development of software in a team environment.
 
Agenda:
06:45pm – 07:00pm Registration
07:00pm – 07:15pm Keynote
07:15pm – 08:15pm Application Lifecycle Management Using Microsoft Visual Studio Team System 2008
08:15pm – 08:30pm Dinner
 
Speaker Profile:
Ms. Hen Lai Ee is the Application Lifecycle Management Consultant at K365Labs Sdn Bhd. She owns more than 8 years experience in the IT industry where she obtained professional certifications from Microsoft and Sun. She also accumulates in depth knowledge in Java and .NET technologies such as Java 2 Standard Edition (currently known as Java Standard Edition), Java 2 Enterprise Edition (currently known as Java Enterprise Edition), and also .NET Framework. She is able to describe these technologies to build different kinds of software, including Web-based applications, smart client applications, and XML Web services- components that facilitate integration by sharing data and functionality over a network through standard, platform-independent protocols such as XML (Extensible Markup Language), SOAP, and HTTP. She has published several of his research works in academic journals such as Journal of Computer Science and International Journal of the Computer, the Internet and Management.

Supported Windows Account Types to Run the SQL Server Agent Service

Sometimes, my customers complaint that their SQL Server Agent is not stable. I was really surprised. SQL Server Agent is pretty stable since the earlier versions of SQL Server. SQL Server Agent is a Microsoft SQL Server 2005 component that lets you automate some administrative tasks. SQL Server Agent runs jobs, monitors SQL Server, and processes alerts. Most of the time this is due to service account type that is configured. I thought it would be worthwhile to blog on this. The following are all the supported service account types to run the SQL Server Agent service: 
 
Service account type Nonclustered server Clustered server Domain controller (nonclustered)
Windows domain account (member of the Windows Administrators group) Supported Supported Supported
Windows domain account (nonadministrative) Supported Supported Supported
Network Service account (NT AUTHORITY\NetworkService) Supported Not supported Not supported
Local user account (nonadministrative) Supported Not supported Not applicable
Local System account (NT AUTHORITY\System) Supported Not supported Supported
Local Service account (NT AUTHORITY\LocalService) Not supported Not supported Not supported

myVSTS User Group October 2009 Technical Talk

This month myVSTS user group will be organizing a talk at Microsoft’s office. The following are the details of tha talk:
 
Date:
Thursday, October 22, 2009
Time:
6:45pm – 8:30pm
Location:
Microsoft Auditorium East, Level 29, Tower 2, Petronas Twin Towers, KLCC, 50088 Kuala Lumpur
 
Topic:
Improving Software Processes Through Application Lifecycle Management

Synopsis:
Microsoft Visual Studio Team System 2008 is a platform for productive, integrated, and extensible software development life-cycle tools. It helps software teams by improving communication and collaboration throughout the software development process. Application Lifecycle Management involves the co-ordination of developmental activities throughout the lifecycle of a project. In a software development project, developmental activities include modeling, code development, build, testing, and so on. This talk provides an overview of how Microsoft Visual Studio Team 2008 facilitates streamlined and efficient development of software in a team environment.

Agenda:
06:45pm – 07:00pm Registration
07:00pm – 07:15pm Keynote
07:15pm – 08:15pm Application Lifecycle Management Using Microsoft Visual Studio Team System 2008
08:15pm – 08:30pm Dinner