December 2004 - Posts

How many times have you written a statement like this?
 
if ( (myString != null) && (myString != string.Empty) )
 
Often as a developer you have to check for both null and empty.  But now thanks to the new property IsNullOrEmpty() on the String object, you can check for both null or empty at the same time.  Just think of how much code you won't have to write now.
 
The above line would look like this:
 
if (String.IsNullOrEmpty(myString))
 
That is so much easier.
This really isn't a tip but I thought it was worth posting.  The Avalon (Microsoft's next generation Presentation Subsystem) SDK CTP has been released.  If you have VS2005 installed and Windows XP Service Pack 2, you can install this add-on to Visual Studio.  Once it is installed, you will have the option to create Avalon applications in addition to Windows Forms Applications.
 
Avalon uses XAML, a new markup language, for rendering controls etc.  Once Longhorn is released, this will allow for Vector Based graphics, etc.
 
Here is an example of what a XAML application looks like.
 
<DockPanel xmlns="http://schemas.microsoft.com/2003/xaml">
    <Border Background="LightBlue"
            DockPanel.Dock="Top">
        <Text>Some Text</Text>
    </Border>
   
    <Border DockPanel.Dock="Bottom"
            Background="LightYellow">
        <Text>Some text at the bottom of the page.</Text>
    </Border>
   
    <Border DockPanel.Dock="Left"
            Background="Lavender">
        <Text>Some more text</Text>
    </Border>
   
    <Border DockPanel.Dock="Fill">
        <DockPanel>
            <Button DockPanel.Dock="Top"
                    Height="30px"
                    Width="100px"
                    Margin="10,10,10,10">Button1</Button>
            <Button DockPanel.Dock="Top"
                    Height="30px"
                    Width="100px"
                    Margin="10,10,10,10">Button2</Button>
            <Border DockPanel.Dock="Fill"
                    Background="LightGreen">
                <Text >Some Text Below the Buttons</Text>
            </Border>
        </DockPanel>
    </Border>
</DockPanel>

One thing interesting about XAML applications is that they can be viewed as a stand-alone application or through a browser.
 
As a side note, it looks like some Indigo templates snuck into this add-on: Indigo Messaging Framework Client, Indigo Service Framework Service, Indigo Messaging Framework Service.  I am not sure if these are functional at all so if anyone tries them, let us know.
The StopWatch class is kind of a neat new class which lets you time things in a manner similar to a stop watch.  There was no simple way to time things in ASP.NET other than to keep tracks of Ticks and a start time, etc.  Now, to time how long something takes simply instantiate the StopWatch class and call the Start() method.  When you are finished timing simply call the Stop() method.  To get the time elapsed, access the Elapsed property.  This property will return a TimeSpan which you can use to get any sort of unit of time that you are interested in.
 
Here is a simple code example:
 
// instantiate the stopWatch
StopWatch stopWatch = new StopWatch();
 
// start the stopWatch
stopWatch.Start();
 
// do something that you want to time the duration of
 
// stop the stopWatch
stopWatch.Stop();
 
// get the time elapsed
TimeSpan timeSpan = stopWatch.Elapsed;
 
This class is not terribly exciting but it will make things like Average Time Performance Counters a little easier to implement.  This class is located in System.Diagnostics.
Ever needed to see if a string contained a date or int?  In .NET 1.1, about the only way to do this is to use DateTime.Parse() and see if it throws an exception.  That's a bad time.
 
Now thanks to TryParse(), you can check and see if the input is valid, before parsing.  This prevents the need for using a try/catch block every time you use the parse method.
 
The syntax is kind of wierd so this method can be called in two ways:
 
Method 1:
string dateTimeString = "12/23/2004 09:00 AM";
DateTime myDateTime;
 
// returns true if dateTimeString is valid
if (DateTime.TryParse(dateTimeString, myDateTime))
    myDateTime = DateTime.Parse(dateTimeString);
 
You can also get the DateTime directly from an output parameter.
Method 2:
 
string dateTimeString = "12/23/2004 09:00 AM";
DateTime myDateTime;
 
DateTime.TryParse(dateTimeString, out myDateTime);
 
It can also be used for ints, bytes, chars, doubles, bools, etc. 
 
i.e.:
int32.TryParse(intString, myInt);
bool.TryParse(boolString, myBool);
 
It's a simple feature, but I think it will prove to be useful in the future.
 
More information can be found here:
ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/cpref/html/M_System_DateTime_TryParse_2_c11ce641.htm
More Posts « Previous page