Wednesday, October 21, 2009

Generics

Generics allow us to create classes that are not bound to a specific type, and must provide generic functionality for all types. It can be used to provide type-safety during compile-time.

Here is a sample program I wrote to demonstrate how to create a Generic class. I intentionally avoided using for the type, to show this is not a requirement.


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace SystemTypesCollections
{
public class GenericsTest<ZX>
{
public ZX MyVal { get; set; }

public override string ToString()
{
if (MyVal is String)
{
return "My String is: '" + MyVal + "'";
}
else if (MyVal is int)
{
return "I am an Integer: " + MyVal;
}
else
{
return "I am an object";
}

}
public GenericsTest(ZX val)
{
MyVal = val;
}
}

public class Program
{
static void Main(string[] args)
{
GenericsTest<int> g1 = new GenericsTest<int>(5);
GenericsTest<String> g2 = new GenericsTest<string>("Hello, world");
GenericsTest<IList> g3 = new GenericsTest<IList>(new ArrayList());

System.Console.WriteLine(g1);
System.Console.WriteLine(g2);
System.Console.WriteLine(g3);
}
}
}



Output result:
I am an Integer: 5
My String is: 'Hello, world'
I am an object

Value Types

As I've been studying for the Microsoft .NET certification test, I am posting my study notes on my blog.

Value Types


Value Types are types that are copied when passed in as arguments (passed by value).


Value Types are stored on the stack (whereas Reference Types are stored in the Heap). Here is an excellent series of articles about Heap vs. Stack:


C# Heap(ing) Vs Stack(ing) in .NET: Part I


C# Heap(ing) Vs Stack(ing) in .NET: Part II


C# Heap(ing) Vs Stack(ing) in .NET: Part III


C# Heap(ing) Vs Stack(ing) in .NET: Part IV


There are two kinds of Value Types:



  • Built-in value types. These include:


  1. Byte (8-bit unsigned integer, byte)

  2. SByte (8-bit signed integer, byte)

  3. Int16 (16-bit signed integer, short)

  4. Int32 (32-bit signed integer, int)

  5. Int64 (64-bit signed integer, long)

  6. UInt16 (16-bit unsigned integer, ushort)

  7. UInt32 (32-bit unsigned integer, uint)

  8. UInt64 (64-bit unsigned integer, ulong)

  9. Single (32-bit single-precision floating point, float)

  10. Double (64-bit double-precision floating point, double)

  11. Boolean (boolean value true/false, boolean)

  12. Char (16-bit Unicode, char)

  13. Decimal (128-bit, decimal)

  14. IntPtr (signed integer, depends on underlying platform)

  15. UIntPrt (unsigned integer, depends on underlying platform)
  • User-defined Value Types.

The User-defined Value Types will extend System.ValueType or System.Enum. I copied the Complex Number Representation example from MSDN into VS.NET 2008 and played around with it.

Structs are also Value Types. This article from CodeProject covers Enums and Structs.

Tuesday, October 20, 2009

Nullable types

A new language construct as of the .NET 2.0 Framework is Nullable types. Basically, it allows one to extend a Value Type (such as int, bool), and make it nullable.

Here's a small sample class I wrote to experiment with using Nullable Types.


namespace Project
{
public class DataTest
{
// The syntax T? (in C#) is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.
private int? age = null;
private System.Nullable<system.int32> weight = null;

public int? Age
{
get { return age; }
set { age = value; }
}

public int? Weight
{
get { return weight; }
set { weight = value; }
}

public DataTest()
{}

public DataTest(int? myAge, int? myWeight)
{
Age = myAge;
Weight = myWeight;
}

///
/// Return String displaying age and weight
///

///
public override string ToString()
{
if (Age.HasValue && Weight.HasValue)
{
return "Your age is: " + Age.Value + ", and your weight is: " + Weight.Value;
}
else
{
return "No age or weight entered";
}
}
}
}


The following is a good reference for Nullable Types: Introduction to .Net Framework 2.0 Nullable Types.

Microsoft Certification Studies: 70-536

Today, I'm starting studies for Microsoft .NET certification. It looks like the first step in the journey to any certification path is the 70-536: Microsoft .NET Framework Application Development Foundation.

First question that immediately came to mind: is the test given in C# or VB? Part of the beauty of the .NET Framework is the underlying CLR, BCL (base class libraries) and language-independence. However, this has also been a bit of a challenge for me during past .NET development projects.

I'm a C# guy, myself. Sometimes I'll find excellent blog entries, posts, or forum responses of what I want to do, but the examples are in VB. Now, I can stumble through VB code examples, but it feels like reading text in a foreign language, whereas reading C# feels like reading something in my native language (English).

I found the answer to my question here:
http://www.proprofs.com/forums/index.php?showtopic=20207

I'll definitely be selecting C# for my exam.

Tuesday, July 7, 2009

VMware Server and Fedora Core 11, Part 4

At this point, I have been happily able to run existing VMs.

However, I went to create a new VM today, and my connection to local host dropped immediately.

I went to examine my log files (/var/log/vmware) and found the following core dump:

Jul 07 10:13:41: | VMHSWorkerMain done : thread id = 6737
Jul 07 10:13:41: | Caught signal 6 -- pid 6737
Jul 07 10:13:41: | SIGNAL: eip 0xf7f71430 esp 0xf7bfac4c ebp 0xf7bfac64
Jul 07 10:13:41: | SIGNAL: eax 0x0 ebx 0x1a51 ecx 0x1a52 edx 0x6 esi 0x3b edi 0x5c3ff4
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac4c : 0xf7bfac64 0x00000006 0x00001a52 0x004817c1
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac5c : 0x005c3ff4 0xf7bfad84 0xf7bfad8c 0x00483092
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac6c : 0x00000006 0xf7bfad04 0x00000000 0x005c3ff4
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac7c : 0x00000001 0xf7bfac98 0x00000000 0xf79007f0
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac8c : 0x00000000 0xf7900010 0x00000018 0x00020000
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfac9c : 0x00000001 0x00001000 0x00000000 0xffffffc8
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfacac : 0x00530163 0xf7900010 0x005c5ec0 0x0000006e
Jul 07 10:13:41: | SIGNAL: stack 0xf7bfacbc : 0x005c76a8 0xf7900010 0x00020a60 0x005c3ff4
Jul 07 10:13:41: | Backtrace:
Jul 07 10:13:41: | Backtrace[0] 0xf7bfa688 eip 0x80c9d62
Jul 07 10:13:41: | Backtrace[1] 0xf7bfa6f8 eip 0x80c9fdb
Jul 07 10:13:41: | Backtrace[2] 0xf7bfac64 eip 0xf7f71400
Jul 07 10:13:41: | Backtrace[3] 0xf7bfad8c eip 0x483092
Jul 07 10:13:41: | Backtrace[4] 0xf7bfb2f8 eip 0x4bfdad
Jul 07 10:13:41: | Backtrace[5] 0xf7bfb318 eip 0x4bfed4
Jul 07 10:13:41: | Backtrace[6] 0xf7bfb33c eip 0x61ecaa
Jul 07 10:13:41: | Backtrace[7] 0xf7bfb354 eip 0x61ed4d
Jul 07 10:13:41: | Backtrace[8] 0xf7bfb370 eip 0x61c788
Jul 07 10:13:41: | Backtrace[9] 0xf7bfb378 eip 0x616850
Jul 07 10:13:41: | Backtrace[10] 0xf7bfb398 eip 0x81e378c
Jul 07 10:13:41: | Backtrace[11] 0xf7bfb498 eip 0x615935
Jul 07 10:13:41: | Backtrace[12] 00000000 eip 0x53482e
Jul 07 10:13:41: | Your core dump size limit is 51200 kb.
Jul 07 10:13:41: | Attempting to dump core ...
Jul 07 10:13:41: | VMServerd Panic: Exiting on unexpected signal.

I did a bit of research and found out that I needed the 32-bit version of libgcc (I already had the 64-bit version, but that was not sufficient).

Ran the following command:

yum install libgcc.i586

And now I am able to create new VMs.

Monday, July 6, 2009

VMware Server and Fedora Core 11, Part 3

I ended up recompiling the kernel by following the directions here step-by-step:

http://fedoraproject.org/wiki/Docs/CustomKernel

I applied the kernel patch that was included in: vmware-update-2.6.29-5.5.9-3.tar.bz2 and built new RPM packages.

However, when I went to install the new kernel, I ended up getting an error about a failed dependency on kernel-firmware.

I found this post: http://forums.fedoraforum.org/showthread.php?t=206844&highlight=kernel+rpm+failed+dependencies

and made the changes described to kernel.spec


%define with_firmware %{?_with_firmware: 1} %{?!_with_firmware: 0}
-----------to look like this-------------------
%define with_firmware %{?_with_firmware: 0} %{?!_with_firmware: 1}



%define with_firmware %{?_without_firmware: 0} %{?!_without_firmware: 1}
-----------to look like this-------------------
%define with_firmware %{?_without_firmware: 1} %{?!_without_firmware: 0}


And then re-recompiled the kernel.

This time, there was a kernel-firmware.*.rpm in my ~/rpmbuild/RPMS/x86_64 (here is a directory listing):

kernel-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-debug-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-debug-debuginfo-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-debug-devel-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-debuginfo-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-debuginfo-common-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-devel-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-firmware-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm
kernel-headers-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm

I ran the following command:

rpm -ivh kernel-firmware-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm kernel-headers-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm kernel-devel-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm kernel-2.6.29.5-191.pjt20090702.fc11.x86_64.rpm

And was able to get my custom kernel installed.

From here, I just followed the steps in my first post for getting through vmware-config.pl successfully (vmmon and vmnet compiled and all that good stuff).

Successful execution of VMware Server 1.0.9 and able to start up existing VMs.

Thursday, July 2, 2009

VMware Server and Fedora Core 11, Part 2

Well, after going through the steps described in my last post, I had a successful run of vmware-config.pl and was *finally* able to run the VMware Admin Console.

However, whenever I tried to Power On a VM, I would get a general error message. I turned on logging for one of the VMs and got the following:

Unable to change virtual machine power state: The process exited with an error:
vmxvmdb: Index name being generated from config file
POST(no connection): Version mismatch with vmmon module: expecting 138.0, got 208.0.
You have an incorrect version of the `vmmon' kernel module.
Try reinstalling VMware Server.

I'm pretty sure this is because of using the patch that modified the VM module source.

So, I'm stepping back and going through the painful process of running a patch against the Linux kernel source and recompiling the kernel, to get past the init_mm issue.

Recompiling the kernel takes forever, and something went wrong on my first attempt, so I am re-recompiling now...