Skip navigation.
Home

CodeProject

Include item in CodeProject blog

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

Subversion Permissions

WARNING
This tip involves editing your Subversion repository .access file.
If your repository is hosted with Dreamhost there is a problem with this.

Whenever you use the Dreamhost Panel to create a new user it will wipe your
work from the .access file, and overwrite it with a default file that gives full
permission to everyone.

If you need to edit your .access file then keep a backup so that when you create new
users you can restore your version of the file.

Subversion Per Directory Access

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).

Syndicate content