|
|
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 |
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 solution is to use a panel that has a picturebox placed on it with DockStyle.Fill. This will make the picturebox assume the size of the panel. In addition, set the DockPadding.All property to the width of the desired border. Then in the Panel's OnPaint method, call the baseclass and then paint the desired borders.
Here are both VB and C# projects that illustrate how you might go about this. The derived PicturePanel class has properties that allow you to set the bordersize and color as well as the image that is to be displayed. This sample retrieves the image from an embedded resource. It also uses double buffering to minimize flashing as you resize the control. |
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 deterministic 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.
|
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 constructor 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();
|
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 << 16) | (LoWord & 0xffff);
|
static IntPtr MakeLParam(int LoWord, int HiWord)
|
return (IntPtr) ((HiWord << 16) | (LoWord & 0xffff));
|
static int HiWord(int Number)
|
return (Number >> 16) & 0xffff;
|
static int LoWord(int Number)
|
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();
|
|
|
|
|