Skip navigation.
Home

Construction

Programming

Iterate over Enum Members

When you are dealing with a variable that can have one of a defined set of values, it is preferable to use an Enum to define those values, rather than use a primitive datatype like Integer.

This tip isn't about why Enums are better, it is about how to iterate over the range of valid values for the Enum. For example, given the following Enum:

    Private Enum FileOperation
        Open = 1
        Close = 2
        Save = 3
        SaveAs = 4
        Copy = 5
        Move = 6
        Delete = 7
    End Enum

Convert lists to comma separated strings

You have a list or even an array of strings. You want to convert it into a single
string with the list items separated by commas.

Once upon a time you would have looped through the array, added the items to the
string with a comma after each one. Then remembered to trim off th comma after the
last item.

Surely there's an easier way. Meet the Join method of String.

Dim theString as String
theString = String.Join(",", theList.ToArray())

Of course if you're dealing with an array you don't need 'ToArray'

Dim theString as String
theString = String.Join(",", theArray)

The separator doesn't have to be a single character, if you like some space between the
items, that will work too.

Dim theString as String
theString = String.Join(", ", theArray)

And the seperator doesn't even have to be a comma, anything will do.

Dim theString as String
theString = String.Join("|", theArray)

Simple.

Use the Browsable Attribute when creating Form properties

The move towards treating forms as objects in VB is very welcome and very useful,
but there are some things that go on behind the scenes that you need to understand.

You might add a property to your form intending it to be used at runtime,
however if you then inherit from the form to create another form, the properties of
the first form will be available in the Property Window when the inherited form is
open in design mode.

The reason for this is the VB is allowing you to set the properties of the base class
so that the settings can be reflected in the inherited class right there in design mode.
Very useful, but not always what you want.

If a property only makes sense at run time then stop it appearing in the Design Mode
Property Window using the "Browsable" attribute, as follows:

Imports System.ComponentModel
Public Class MyForm
    <Browsable(False)> _
    Public Property MyProperty() As String

Of course if the property in the base form makes sense in design mode, and you want to see
the affect it has on inherited forms, then leave the property Browsable attribute as True.

Use Predicate Functions with Lists

Use a predicate function to create a subset of a list. The new list will only contain items that match the function.

Sub Main()
    Dim lst As List(Of Integer) = getList()
    Dim evens As List(Of Integer) = lst.FindAll(New Predicate(Of Integer)(AddressOf GetEvens))
End Sub

The predicate function is a simple boolean function accepting items of the type in the list.

Private Function GetEvens(ByVal value As Integer) As Boolean
    Return value Mod 2 = 0
End Function

For simple predicates a Lambda function can be used.

Sub Main()
    Dim lst As List(Of Integer) = getList()
    Dim evens As List(Of Integer) = lst.FindAll(Function(value As Integer) value Mod 2 = 0)
End Sub

The same technique's can be used for all of the list Find methods (Find, FindAll, FindIndex, FindLast, FindLastIndex).

The Specification Design Pattern

I've written a four part series on the Specification Design pattern. The four articles are available now on codeproject.com. The articles take you from an introduction to the pattern to a simple VB.Net implementation and finaly to a reusable Specification class that uses inheritence, generics and Operator Overloading.

Attributes and Value Objects article on eggheadcafe

My article on modelling attributes using value objects is available on eggheadcafe. It'll take you though the process of modelling a Temperature attribute, starting with a naive implementation and working towards a fully fledged value object.

See Here

VB6 Collections Article

This is another old article, but it may be useful to someone. It originally appeared in The Visual Basic Programmer's Journal, July 2001, Volume 11, Issue 7.

See Here

Visitor Design Pattern Article

This is going back a bit, but my article on the Visitor Design Pattern (in VB6) is available on MSDN. It was originally published in The Visual Basic Programmer's Journal, March 2001, Volume 11, Issue 3.
See Here

Syndicate content