Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleAnswer: .NET Declarations
Keywordsquick question answer, quiz answer, answer
Categories
 
1. Dim bm As Bitmap = New Bitmap(picCanvas.Image)

    This declaration creates a new variable bm of type Bitmap. It then sets it to a new Bitmap object initialized using the picCanvas control's Image property. This is fine.

2. Dim bm As New Bitmap(picCanvas.Image)

    This declaration also creates a new variable bm of type Bitmap. It also sets it to a new Bitmap object initialized using the picCanvas control's Image property. This is fine, too.

3. Dim bm = New Bitmap(picCanvas.Image)

    While this declaration looks almost the same as the previous one, this version is terrible! The statement doesn't actually say what data type the variable bm should have so it defaults to type Object (in VB 6 this would be type Variant). The result is a generic Object variable containing a Bitmap object.

    Calling the properties and methods of a generic Object is much slower than calling those of an object declared with type Bitmap. Depending on what you are doing, you may need to call the object's methods a lot. For example, a typical image processing application examines and/or sets the value of each pixel in an image. In a 200 x 200 pixel image, that's at least 40,000 property or method calls. In one test, this simple change from the previous declaration made the program run about 100 times slower!

    Bad, bad, bad!

4. Dim bm As Bitmap
   bm = New Bitmap(picCanvas.Image)

    This code declares bm to be of type Bitmap and then later assigns it to a new Bitmap object initialized using the picCanvas control's Image property. This is fine, although some prefer the single-line styles of versions 1 and 2 just because they are more concise.


Versions 1, 2, and 4 are fine and provide pretty much the same performance. The difference is stylistic. Personally, I prefer version 1.

Version 3 is a remarkably deceptive disaster.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated