GDi+ Scope and Type of Variable and Performance in VB.NET

In this article you will learn how to Scope and Type of Variable and Performance in GDi+.
  • 1412

One of the best programming practices is the efficient use of variable and their scope. Before adding a new variable to a program, think for a second and ask yourself, "Do I really need this variable?" If you need a variable, do you really need it right now? The scope of variables and use of complex calculations can easily degrade the performance of your applications. Using global scope for pens, brushes, paths, and other objects may be useful instead of defining variable in the OnPaint or OnPaintBackGround methods.

Let's look at a practical example: Listing 13.5 is written on a form's paint event handler, which creates pens and brushes, and draws rectangles and polygons.

LISTING 13.5: Variables defined in the form's paint event handler

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Drawing.Text
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Namespace ScopeType
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
        End Sub
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
            'Create brushed and pens
            Dim hatchBrush As New HatchBrush(HatchStyle.HorizontalBrick, Color.Red, Color.Blue)
            Dim redPen As New Pen(Color.Red, 2)
            Dim hatchPen As New Pen(hatchBrush, 4)
            Dim brush As New SolidBrush(Color.Green)
            'Create a points for curve
            Dim p1 As New Point(40, 50)
            Dim p2 As New Point(60, 70)
            Dim p3 As New Point(80, 34)
            Dim p4 As New Point(120, 180)
            Dim p5 As New Point(200, 150)
            Dim ptsArray As PointF() = {p1, p2, p3, p4, p5}
            Dim width As Single = Me.ClientRectangle.Width - 100
            Dim height As Single = Me.ClientRectangle.Height - 100
            'float width = this.ClientRectangle.Width â€" 100;
            'float height = this.ClientRectangle.Height â€" 100;

            Dim pt1 As New Point(40, 30)
            Dim pt2 As New Point(80, 100)
            Dim lnColors As Color() = {Color.Black, Color.Red}
            Dim lgBrush As New LinearGradientBrush(pt1, pt2, Color.Red, Color.Green)
            lgBrush.LinearColors = lnColors
            lgBrush.GammaCorrection = True
            'Draw objects
            e.Graphics.DrawPolygon(redPen, ptsArray)

            e.Graphics.DrawRectangle(hatchPen, x, y, width, height)
            e.Graphics.FillRectangle(lgBrush, 200, 200, 200, 200)
            'Dispose of objects
        End Sub
    End Class
End Namespace

In this example we define many variables, all of local scope. Throughout the application, the redPen, hatchBrush, hatchPen, brush, and other variable remain the same. Programmatically, it doesn't matter whether we define these variables locally or globally; the choice depends entirely on the application. It may be better to have variables defined with a global scope. If you repaint the form frequently, defining these variables globally may improve performance because time will not be wasted on re-creating the objects for each pass. On the other hand, defining objects globally may consume more resources (memory).

It is also good to avoid lengthy calculations in frequently called routines. Here's an example: Listing 13.6 draws a line in a loop. As you can see, int x and int y are defined inside the loop.

LISTING 13.6: Defining variable inside a loop

        For i As Integer = 0 To 9999
            Dim bluePen As New Pen(Color.Blue)
            Dim x As Integer = 100
            Dim y As Integer = 100
            g.DrawLine(bluePen, 0, 0, x, y)

We can easily replace the code in Listing 13.6 with Listing 13.7, which is more efficient. If a code statement does the same thing every time a control reaches it inside a loop, it is a good idea to move that statement outside the loop to save processing cycles.

LISTING 13.7: Defining variable outside a loop

        Dim bluePen As New Pen(Color.Blue)
        Dim x As Integer = 100
        Dim y As Integer = 100
        For i As Integer = 0 To 9999
        g.DrawLine(bluePen, 0, 0, x, y)

Sometimes using a floating point data type instead of an integer may affect the quality of a drawing, even though floating point data is costly in terms of resources.

A well-designed and well-coded application also plays a vital role in performance. For example, replacing multiple if statements with a single case statement may improve performance.


More Articles

© 2013 dotNetheaven. All rights reserved.