August 2004 - Posts - Ed's Blog
in

dotnet.org.za

South African .NET Developer Portal

Ed's Blog

Object reference not set to an instance of an object

August 2004 - Posts

  • Going to Kruger

    I'm going to the Kruger national park for a couple of days, we'll be staying at the Tambotie tent camp, which I hear is quite nice. I won't know though, never been to the Kruger before. My parents are sea folk so we went to Scottburgh almost every holiday. Looking foward to it!

    Will be back on Wednesday.

    Posted Aug 26 2004, 02:31 PM by eduard with 2 comment(s)
    Filed under:
  • Improve your parking skills

    Posted Aug 26 2004, 07:30 AM by eduard with 2 comment(s)
    Filed under:
  • Documentation for application blocks released

    The enterprise library workspace on gotdotnet.com have release documentation for 3 existing and 2 new application blocks. It's up for review and comment now.

    The blocks are:
    Caching (existing)
    Logging & Instrumentation(existing)
    Security(existing)
    Data access(new)
    Exception Handling(new)

    At first I thought “but I've used the data access one before” but it seems they are being rewritten

    Get it here under the releases section at the bottom right of the page

  • Yukon Site launched

    Microsoft has launced the official Yukon (SQL Server 2005) site.

    Find it here.

    Source: TheServerSide.Net

    Posted Aug 24 2004, 07:45 AM by eduard with no comments
    Filed under:
  • Regular Expression help sucks

    Is it only me that thinks the section in MSDN on regular expressions really sucks? There is no explanation of the concepts, limited examples and it's very poorly organized. I'm reduced to using the Netscape documentation.

    Here is a site with documentation that's a lot better.

  • IT'S BIG, IT's HUGE........

    IT'S YETI OLYMPICS

    very addictive, don't play if you have a deadline in the near future :)

    http://www.yetisports.org/

    Posted Aug 20 2004, 08:18 AM by eduard with 1 comment(s)
    Filed under:
  • .NET framework 2.0 quickie - generics

    This is the second in a serious of articles I'm writing on the new features in the .NET framework V2.0. Today's topic is generics.

    Generics is a mechanism used to parameterize types(classes,interface etc.) with other types. The best way to explain this is with an example:

    public class Jar<T>
    {

      private
    T _member;

      public T member
      {
        get
        {
          return _member;
        }  
        set
        {
          _member =
    value;
        }
      }

    }

    The new construct introduced is the <T> directly after the class name, called a "type parameter". T is just a variable name, for some reason when using generics, people tend to go for just one capital letter instead of a multi-letter name. Once this name is declared, we can use it inside the class definition exactly as we would any other type such as int,string,Arraylist,Dataset etc. Notice though that T is not linked to any specific type, yet. This is where the advantage of generics comes in, we can replace T with any type when we create an instance of Jar, like so:

    Jar<int> myJar = new Jar<int>();

    or

    Jar
    <ArrayList> myJar = new Jar<ArrayList>();

    a major advantage of this approach is that types will be checked at compile time, for example if we use <int>, the following statement will result in a compile time error:

    myJar.member = "ABC";

    because the types are fixed at compile time, we do not have to do upcasts and downcasts like we would have to do without generics. This is demonstrated in collection classes such as ArrayList (data structure classes are the classical use of generics and are implemented as part of the .NET framework 2.0 under the System.Collections.Generic namespace):

    //OLD WAY /////////////////////////////////////////////
    ArrayList oldWay = new ArrayList();

    for (int x = 0; x < 10; x++)
    {
      //the add method upcasts from int to object
      oldWay.Add(x);
    }

    int sum = 0;

    for (int x = 0; x < 10; x++)
    {
      //have to downcast from object to int (unsafe)
      sum += (int)oldWay[x];
    }

    //NEW WAY /////////////////////////////////////////////
    List<int> newWay = new List<int>();

    for (int x = 0; x < 10; x++)
    {
      //no upcast needed
      newWay.Add(x);
    }

    sum = 0;

    for (int x = 0; x < 10; x++)
    {
      //no downcast needed
      sum += newWay[x];
    }

    Another advantage of generics is the loose coupling achieved between Jar and T.

    A nice feature that Microsoft built in is the ability to specify constraints on type parameters. This is acomplished using another new keyword, "where". For example, if I wanted to write a generic factory class, I could do this:

    public class Factory<T> where T : new()
    {
      public T getInstance()
      {
        return new T();
      }
    }

    The where clause(not to be confused with a SQL where clause :) ) above indicates that whatever type I pass in for T must have a public parameterless constructor, if it doesn't I'll get a compile time error. The other constraints I can use is :

    where T : struct  = T must be a value type

    where T : class = T must be a reference type

    And the most useful one (in my opinion) :

    where T : <class or interface name>

    which will ensure that T inherits from a certain base class or implements a certain interface. We can of course give a comma sperated list of interfaces and a maximum of one base class. Once we've done this we can actually call methods on T like so:

    public class myCollection<T> where T : IList
    {

      private T _myList;

      public myCollection(T t)
      {
        _myList = t;
      }

      public void Add(object x)
      {
        _myList.Add(x);
      }
    }

    Which will compile without error. You can also compose constraints by comma delimiting them:

    public class myCollection<T> where T : new(),IList

    You can use more than one type parameter by comma delimiting them in the declaration for example:

    public class myCollection<T,P,X>

    Type parameters are not limited to classes only, you can specify them on method level as well, like so:

    public class myFactory
    {

      public T createInstance<T>() where T:new()
      {
        return new T();
      }

    }

    (not very useful but good as an example).

    As someone famous once said: "That's all I have to say about that!" :) There's a bit more to generics that you can read up on in the help files, but this should give you a pretty good idea of what it's about.

    Posted Aug 19 2004, 08:01 AM by eduard with no comments
    Filed under:
  • Reshaper bugfix update released

    An update has been released for Reshaper, in particular, it fixes the problem of Visual studio “hanging” for extended periods of time.

    Get it here

    Posted Aug 18 2004, 08:59 AM by eduard with no comments
    Filed under: ,
  • .Text finds a new home

    Armand writes that .Text will now be know as Community Server :: Blogs as part of a larger open source initiative over at Telligent Systems
    Posted Aug 18 2004, 07:34 AM by eduard with no comments
    Filed under:
  • Stephan Hawking changes his theory on black holes

    After thirty years Stephan Hawking decided to change his theory on black holes. He now believes that black holes eventually die out and matter “sucked up” by the black hole is “spit out” at this time.

    Read about it here

    Posted Aug 17 2004, 07:44 AM by eduard with 5 comment(s)
    Filed under:
  • Nunit 2.2 release

    The latest version of Nunit was released last week

    Get it here

    Posted Aug 16 2004, 09:27 AM by eduard with 3 comment(s)
    Filed under: ,
  • .NET framework 2.0 quickie - partial classes

    [Update] - posted this up on sadeveloper.net

    When I started looking at the Visual Studio 2005 Beta, I listed a couple of toppings that I wanted to check out. Then I thought it might be a good idea write up a little “article” on each topic, for my own reference, and who knows, maybe for someone else's as well. So here goes the first one:

    What it's all about
    The idea is to split normal classes into several declarations, these declarations will typically reside in different files. During compilation, these declarations(partial classes) are combined into one and compiled as if there was only one class definition.

    Examle

    [Employee.cs]
    public partial class Employee
    {
      string _name;
      int _empNo;

      public Employee(int empNo,string name)
      {
        _empNo = empNo;
        _name = name;
      }

    }

    [EmployeeProperties.cs]
    public partial class Employee
    {
      public int EmpNo
      {
        get
        {
          return _empNo;
        }
       
    set
        {
          _empNo =
    value;
        }
      }

      public string Name
      {
        get
        {
          return _name;
        }
        set
       {
          _name =
    value;
       }
     }
    }

    Rules

    • All classes that will be joined together must be marked with the “partial” keyword. This will prevent someone from just adding a partial class to your existing class and messing around with the internal workings
    • Of course members (fields,methods, properties etc.) can only be defined once throught all partial classes with the same name.
    • All the partial definitions must be in the same namespace(also obvious) and must be contained in the same module(exe or dll).
    • If you're planning to add partial classes in future, you can use the “partial” keyword, even though you only have one definition. There is no rule that states that you have to have at least 2 partial definitions to be able to use “partial” on a class.

    Pro's

    • Using partial classes we can devide one class into several files, allowing several developers to work on the same class at once while still having the full benefit of source control
    • Allows you to split the functionality of your classes logically into groups. As in the example you can put all your properties in on file, constructors in another, business logic functions in another, or whatever suites your project best. This aids in avoiding “clutter“, as is demonstrated in the new ASP.NET code behind files.
      Another good idea would be to put all the parts of your class that was generated by a code generator in one file.

    Con's

    • The major drawback I see in partial classes is that developers will use it to “extend” classes instead of normal OO mechanism, thus ending up with huge monolithic classes.
    • The other issue is just around locating all the partial classes that contribute to a class. Hopefully Visual Studio or tools like ReShaper will be able to help us quickly navigate between partial classes instead of having to remember in which files they are defined. For now (an at all times really) you can just use a nice naming convention. 

     

     

  • SQL Server Health and History Tool

    Microsoft has released this tool that “allows you to collect information from instances of SQL Server, store this information, and run reports against the data in order to determine how SQL Server is being used”. (1.3MB download)

    The documentation can be downloaded at the bottom of the page.

     

    Posted Aug 13 2004, 07:25 AM by eduard with 1 comment(s)
    Filed under: ,
  • So what's new in SP2 anyway

    Microsoft has release an article that explains what SP2 is all about and what's in and what's out.

    Read it here

    Source: TheServerSide.net

    Posted Aug 12 2004, 07:30 AM by eduard with no comments
    Filed under:
  • How Microsoft uses reflection

    An interesting article on how internal dev team at Microsoft make use of .NET reflection. One example is the XBox team. Interesting also th find out that the XBox runs/will run .net.

    Read it here.

    Source here.

    Posted Aug 11 2004, 08:41 AM by eduard with no comments
    Filed under:
More Posts Next page »
Powered by Community Server (Commercial Edition), by Telligent Systems