Windows Forms from MFC

12.1 As a VC++ programmer, what should I look out for when using C#?

1] The conditionals in if-statements must calculate to a boolean value. You cannot write something like

if (nUnits) { ... }

where nUnits is a int.

2] All code must reside in a class. There are no global variables per se.

3] Bitwise & and | operators can be used with boolean types as logical operators. The && and || operators are also used, and do have the "short-cut calculation" behavior found in C++. The & and | operators do not have this ""short-cut calculation" behavior.

4] Pointers are not available in managed code.

5] In managed code, destructors are not hit immediately when as object goes out of scope. Ie, there is no deterministic destruction.

12.2 Where can I find a succinct discussion of Windows Forms?

One place to look is Jeff Prosise's article, Windows Forms: A Modern-Day Programming Model for Writing GUI Applications, from the Feb 2001 MSDN magazine .

A second place is Shawn Burke's articleUsing the Microsoft .NET Framework to Create Windows-based Applications found in the MSDN Library.

12.3 When I add a destructor to a class, why isn't it hit when my instantiated object goes out of scope?

Your managed code doesn't have a destructor, just a finalizer. The difference is a destructor (as in C++) fires immediately when an object goes out of scope, but a finalizer is run when the CLR's garbage collector (GC) gets to your object. It's not determanistic when this will occur, but it's very unlikely to occur right after the object goes out of scope. It will happen at a later time, however.

(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)

12.4 How do I convert a string to a double or int? What plays the role of atof and atoi in C#?

You use static members of the Convert class found in the System namespace to handle conversions in the .NET framework.

string s = "45";

int h = Convert.ToInt32(s);

double d = Convert.ToDouble(s);

12.5 What class or method do I use to retrieve system metrics like the width of a scrollbar?

In the .NET framework, you use the SystemInformation class from the System.Windows.Forms namespace. This class has comparable information to what GetSystemMetrics() returned in VC6. There is also a Screen class that contains additions display device properties.

12.6 In C++, the code "MyClass ht;" creates an object ht on the stack frame. But in C#, this same code compiles, but gives a runtime error. Why?

MyClass ht; does not create any object. Instead, it creates a variable (a reference) of type MyClass, and sets its value to null. No object is created. To create an object, you need to explicitly call the class contructor with a new.

MyClass ht = new MyClass();

You can also use ht to refer to an instance of MyClass that has been created (with a new) at some other point in your code.

MyClass myClass = new MyClass();

......

MyClass ht;

......

ht = myClass; // both ht and myClass are references to the same object...

12.7 I need to encode the LParam argument of a mouse message. How do I do MakeLong , HiWord and LoWord type conversions?

You can create static methods that encode and decode these values. Here are some.

static int MakeLong(int LoWord, int HiWord)

{

return (HiWord <<>

}

static IntPtr MakeLParam(int LoWord, int HiWord)

{

return (IntPtr) ((HiWord <<>

}

static int HiWord(int Number)

{

return (Number >> 16) & 0xffff;

}

static int LoWord(int Number)

{

return Number & 0xffff;

}

12.8 How do you clear the contents of a list box?

You have to access the Items of the ListBox using the Items property and clear (or otherwise operate on the items) these.


this.lb1.Items.Clear();

No comments: