Welcome Guest!
 VB Helper
 Previous Message All Messages    
VB Helper Newsletter  Rod Stephens
 Jan 06, 2012 14:35 PST 

I keep forgetting to mention Jeff Jenness, who solved the "Find the
equilateral triangles" puzzle in Visual Basic 6.

You can see the puzzles (and links to solutions, including Jeff's) at:

    http://www.vb-helper.com/howto_triangle_puzzle.html
    http://www.vb-helper.com/howto_net_triangle_puzzle.html

Hopefully next week I'll post a new puzzle that asks you to find
squares.
-----
Beginning Database Design Solutions
http://www.vb-helper.com/db_design.htm

    A good database design doesn't guarantee success, but a bad one
guarantees failure.
-----
Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Twitter feeds:
    VBHelper
    CSharpHelper
==========
==========

    VB.NET Contents:
1. New HowTo: Add delegates to combine lists of methods in Visual Basic
.NET
2. New HowTo: Add a DrawRectangle method to the Graphics class that
takes a RectangleF as a parameter in Visual Basic .NET
3. New HowTo: Copy a two-dimensional array in Visual Basic .NET
4. New HowTo: Make SubArray extension methods that let you easily copy
parts of one- and two-dimensional arrays in Visual Basic .NET

    Both Contents:
5. New online article
==========
++++++++++
      <VB.NET>
++++++++++
==========
1. New HowTo: Add delegates to combine lists of methods in Visual Basic
.NET
http://www.vb-helper.com/howto_net_add_delegates.html
http://www.vb-helper.com/HowTo/howto_net_add_delegates.zip

Delegates are a confusing topic. A delegate is a data type much like
Integer or Boolean but a delegate represents a method (subroutine or
function) instead of a simple piece of data. Specific delegate types
represent methods that have specific signatures. For example, a delegate
might represent a function that takes two Integer parameters and returns
a Single result.

After a program defines a delegate type, it can declare variables of
that type. It can then store a reference to a method in a delegate
variable and later invoke the method by calling whatever method is in
the variable. For an example of using delegates, see the example "Use
delegates to pass a method's address to another method in Visual Basic
.NET"
(http://www.vb-helper.com/howto_net_plot_equation_with_delegate.html).

As if that wasn't confusing enough, you can add delegates. So what does
it mean to add delegates? If you add two delegates, the result is a new
delegate that executes the first followed by the second. You can sort of
think of a delegate as a list of references to methods. If you add two
delegates, the second one's methods get added to the first one's list.
Later if you execute the combined delegate, both lists are executed.

This example uses the following code to demonstrate delegate addition.

' A delegate that represents a method that adds something to the
ListBox.
Private Delegate Sub AddToListDelegate()

' Specific methods that match the delegate.
Private Sub MethodA()
    lstResults.Items.Add("    This is MethodA")
End Sub

Private Sub MethodB()
    lstResults.Items.Add("    This is MethodB")
End Sub

' Define and use three delegates.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    ' Define delegate variables.
    Dim A As AddToListDelegate = AddressOf MethodA
    Dim B As AddToListDelegate = AddressOf MethodB
    Dim C As AddToListDelegate = _
        DirectCast([Delegate].Combine(A, B), AddToListDelegate)

    ' Use the delegates.
    lstResults.Items.Add("Calling A:")
    A()
    lstResults.Items.Add("Calling B:")
    B()
    lstResults.Items.Add("Calling C:")
    C()
    lstResults.Items.Add("Calling C - A:")
    C = DirectCast([Delegate].Remove(C, A), AddToListDelegate)
    C()
End Sub

The code first declares the type AddTolistDelegate. A delegate of this
type can hold a reference to a subroutine that takes no parameters.

Next the code defines two methods that match the delegate type.

The form's Load event handler defines two delegate variables A and B,
and initializes them so they refer to MethodA and MethodB. It then
creates a third delegate C and sets it equal to A + B. It uses the
Delegate class's Combine method to add the two delegates and uses
DirectCast to convert the result into an AddTolistDelegate object.

The code then executes the delegates. It calls the method referred to by
A, then B, and then C. The delegate C contains references to A and B so
invoking it makes MethodA and MethodB both execute.

Next the code removed A from C. That removes the call to A's method from
the "list" contained in C leaving only B. When the code invokes the
result, only MethodB executes.

Note that you can subtract a delegate more than once from another
delegate without harm. In this example, the code could remove A from C
many times without causing any problems.

However, if you subtract the last delegate from another delegate, then
the result is Nothing and you can no longer invoke that result. In this
example if the code removed B from C, then C would become Nothing and
trying to invoke C would throw an exception.
==========
2. New HowTo: Add a DrawRectangle method to the Graphics class that
takes a RectangleF as a parameter in Visual Basic .NET
http://www.vb-helper.com/howto_net_add_graphics_methods.html
http://www.vb-helper.com/HowTo/howto_net_add_graphics_methods.zip

For some unknown reason, the Graphics class's DrawRectangle method
cannot take a RectangleF as a parameter. It can take a Rectangle or four
Singles, and the FillRectangle method can take a RectangleF as a
parameter, but the DrawRectangle method cannot.

Fortunately it's easy to add this as an overloaded version of the method
by creating an extension method for the Graphics class. The following
code shows how the example program does this.

Imports System.Runtime.CompilerServices

Module GraphicsExtensions
    ' Draw a RectangleF.
    <Extension()> _
    Public Sub DrawRectangle(ByVal gr As Graphics, ByVal the_pen As Pen,
ByVal rectf As RectangleF)
        gr.DrawRectangle(the_pen, rectf.Left, rectf.Top, rectf.Width,
rectf.Height)
    End Sub
End Module

Now the program can use the new version of the DrawRectangle method just
as if it had been included in the original Graphics class. The following
code shows how the example program draws its rectangle.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    ' Draw a rectangle.
    Dim rectf As New RectangleF(10, 10, _
        ClientSize.Width - 20, ClientSize.Height - 20)
    e.Graphics.DrawRectangle(Pens.Red, rectf)
End Sub
==========
3. New HowTo: Copy a two-dimensional array in Visual Basic .NET
http://www.vb-helper.com/howto_net_copy_2d_array.html
http://www.vb-helper.com/HowTo/howto_net_copy_2d_array.zip

The Array class's Copy method can copy items from one array to another.
This example uses the following code to copy the values in the Values
array into a new array.

Dim new_values(Values.GetUpperBound(0), Values.GetUpperBound(1)) As
String
Array.Copy(Values, new_values, Values.Length)

This code makes a new array with the same dimensions as the original
one. It then uses Array.Copy to copy the items from the original array
to the new one.

The example contains additional code to display the arrays but it's not
important for making the copy so it isn't shown here. Download the
example to see how it works.
==========
4. New HowTo: Make SubArray extension methods that let you easily copy
parts of one- and two-dimensional arrays in Visual Basic .NET
http://www.vb-helper.com/howto_subarray_extensions.html
http://www.vb-helper.com/HowTo/howto_subarray_extensions.zip

The Array class's Copy method can copy items from one array to another
but it requires that you figure out how many items you want to copy. It
will also copy parts of a two-dimensional array but it only copies
contiguous pieces not rectangular chunks in the middle.

This example creates two SubArray extension methods that let you copy
parts of a one- or two-dimensional array much as you can use a String's
SubString method to copy parts of a String.

The following code shows the extension method that copies part of a
one-dimensional array.

' Copy the indicated entries from an array into a new array.
<Extension()> _
Public Function SubArray(Of T)(ByVal values() As T, ByVal start_index As
Integer, ByVal end_index As Integer) As T()
    Dim num_items As Integer = end_index - start_index + 1
    Dim result(0 To num_items - 1) As T
    Array.Copy(values, start_index, result, 0, num_items)
    Return result
End Function

This method calculates the number of items to copy and creates an array
to hold that number of items. It uses Array.Copy to copy the desired
items into the new array and returns the array.

The following code uses this extension method to copy items 1 through 3
of a one-dimensional array into a new array.

' Copy the array.
Dim new_values() As String = Values1d.SubArray(1, 3)

The following code shows the extension method that copies part of a
two-dimensional array.

<Extension()> _
Public Function SubArray(Of T)(ByVal values(,) As T, ByVal row_min As
Integer, ByVal row_max As Integer, ByVal col_min As Integer, ByVal
col_max As Integer) As T(,)
    Dim num_rows As Integer = row_max - row_min + 1
    Dim num_cols As Integer = col_max - col_min + 1
    Dim result(0 To num_rows - 1, 0 To num_cols - 1) As T

    For row As Integer = 0 To num_rows - 1
        For col As Integer = 0 To num_cols - 1
            result(row, col) = values(row + row_min, col + col_min)
        Next col
    Next row

    Return result
End Function

This method calculates the number of rows and columns it must copy and
creates an array to hold the necessary number of items. It then loops
through the desired items, copying them into the new array, and returns
the new array.

The following code uses this extension method to copy the items in rows
2 - 3 and columns 1 - 3 into a new array.

' Copy the array.
Dim new_values(,) As String = Values2d.SubArray(2, 3, 1, 3)
==========
++++++++++
      <Both>
++++++++++
==========
5. New online article

Building 3D Games for Windows Phone
http://msdn.microsoft.com/library/hh389798.aspx

(The date on this article is August 2011 but I'm not sure it was reallky
posted then. I only found it recently.)

This series of articles shows how to write a simple three-dimensional
game for Windows Phone 7. The final Marvelous Maze game lets the user
explore a simple maze looking for treasures and overcoming obstacles to
reach a final goal. This article explains how to create objects in
three-dimensional space. It explains how to transform those objects and
use projections to display them in a Windows Phone 7 game. It explains a
simple 3-D example and how culling lets XNA draw closed solids more
efficiently.

The code is in C# because at the time you couldn't write XNA
applications for Windows Phone with Visual Basic. Since then support for
Visual Basic has been added so you should be able to translate the
examples without too much difficulty.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Twitter feeds:
    VBHelper
    CSharpHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages    
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.