About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Wednesday, November 07, 2007

Bug?? Reference variable arrays

A co-worker came to me with this problem:




    1         public static int[] MainArray = new int[2] { 0, 0 };


    2         static void Main(string[] args)


    3         {


    4             GetArray();


    5         }


    6         private static void GetArray()


    7         {


    8             MakeNewArray(MainArray);


    9         }


   10         private static void MakeNewArray(int[] p)


   11         {


   12             p[0] = 1;


   13             Console.WriteLine("p[0]:" + p[0].ToString());


   14             Console.WriteLine("MainArray[0]:" + MainArray[0].ToString());


   15             Console.ReadLine();


   16         }






Result:
p[0]:1
MainArray[0]:1

He wanted a copy version of the integer array, but instead it was acting like it was being reference. The response of the code seems wrong since you would expect it to make a seperate array and not reference the MainArray.

What seems to be happening is that since these are arrays, and of the type Reference. Then the arrays are placed on the heap, with the variable names placed on the stack (some what). So, when making a call to a seperate method, the new variable p is then placed on the stack, but pointing to MainArray's values on the heap. Giving a reference type behavior. The solution is to make a copy of the array.




    1         public static int[] MainArray = new int[2] { 0, 0 };


    2         static void Main(string[] args)


    3         {


    4             GetArray();


    5         }


    6         private static void GetArray()


    7         {


    8             MakeNewArray();


    9         }


   10         private static void MakeNewArray()


   11         {


   12             int[] p = new int[MainArray.Length];


   13             Array.Copy(MainArray, p, MainArray.Length);


   14             p[0] = 1;


   15             Console.WriteLine("p[0]:" + p[0].ToString());


   16             Console.WriteLine("MainArray[0]:" + MainArray[0].ToString());


   17             Console.ReadLine();


   18         }







Result:
p[0]:1
MainArray[0]:0

Tuesday, November 06, 2007

A good .Net Threading tutorial site.

http://www.albahari.com/threading/part2.html

Upgrading from .Net Framework 1.0 to 3.5

  • First upgrade your code from .NET 1.0 to .NET 1.1 using Visual Studio 2003. It has a project upgrade tool with some assistance.

  • Next use Visual Studio 2008 to upgrade the project from .NET 1.1 to .NET 2.0. The upgrade wizard here does not change your code. It will show you warnings for things that you have to change but you will have to go change them. You also have to fix all your new compile errors and do a full retest.

  • Once you're at .NET 2.0 the migration to .NET 3.0 and .NET 3.5 is easy. It's primarily just about using the new features available in these new versions of the framework. All your existing code should work fine, in fact you most likely wont even need to recompile.



The .NET 1.1 -> .NET 2.0 breaking changes are detailed here: http://msdn2.microsoft.com/en-us/netframework/aa570326.aspx

Here's a helpful blog entry from Brad Abrams on upgrade experience:
http://blogs.msdn.com/brada/archive/2007/01/15/real-world-customer-experience-in-migrating-from-net-framework-1-1-to-2-0.aspx

Here's another great post on this topic deom Daniel Moth:http://www.danielmoth.com/Blog/2007/10/migrating-from-net-framework-v1x-to.html

- source: http://blogs.msdn.com/pandrew/archive/2007/10/25/how-to-upgrade-from-net-framework-1-0-to-net-framework-3-5.aspx


So, I guess they make the transition from 1.0 to 3.5 look easy. Just hope I never have to do it. <^_^>

Monday, November 05, 2007

SQL Server 2008 Date and Time data types

No more converting datetimes and combining datetimes together because you wanted to seperate date and time. (EX: '1/1/1900 11:36AM')

Here are the new data types for SQL Server 2008:










































Data typeFormatRangeAccuracy
timehh:mm:ss[.nnnnnnn]00:00:00.0000000 through 23:59:59.9999999100 nanoseconds
dateYYYY-MM-DD00001-01-01 through 9999-12-311 day
smalldatetimeYYYY-MM-DD hh:mm:ss1900-01-01 through 2079-06-061 minute
datetime YYYY-MM-DD hh:mm:ss[.nnn]1753-01-01 through 9999-12-310.333 second
datetime2YYYY-MM-DD hh:mm:ss[.nnnnnnn] 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999100 nanoseconds
datetimeoffsetYYYY-MM-DD hh:mm:ss[.nnnnnnn] [+-]hh:mm00001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 (in UTC)100 nanoseconds


Source: http://blogs.msdn.com/usisvde/archive/2007/11/05/get-a-date.aspx

Friday, November 02, 2007

WCF Tutorial

My first attempt at using WCF, thanks to http://msdn2.microsoft.com/en-us/vbasic/bb736015.aspx was very successful.

I did change the language to C# for my version, since I perfer it over VB.Net. There were some changes that I had to go about in a different way to get it to be successful.

Example:

Dim t As Type = GetType(ServiceLib.HelloService)

vs.

ServiceLib.HelloService hs = new ServiceLib.HelloService();


Type t = Type.GetTypeArray(new object[]{hs})[0];