Visual Basic

I am starting to suspect 2008 is not saving all my changes.  This is not a good sign.  Maybe this will be fixed in SP1.  I hope a lot of my VB headaches will go a way with SP1

Comments [0]

Setting a property via anonymous delegates

I was looking for a way to pass a parameter by reference today.  More specifically the setter.

I found a couple interesting blog posts

notice the comment by Tedesco.

He offers a nice way to do this via an anonymous delegate.

    public delegate void InsertString( System.String param );
public partial class Bird
public void FileToDb( int x )
FillEntityColumn( delegate( System.String value )
tExtra.Text = value;
} );

So we actual pass the FillEntityColumn method an anonymous delegate which then sets the property.

Now in vb you can write

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub
Sub testme(ByRef s As String)
s = "hello"
End Sub

which a commenter on eluded to.  But in vb you still cannot pass a Property by reference, at least not really.  Take a look what vb generates into il.

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim VB$t_ref$S0 As TextBox = Me.TextBox1
Dim VB$t_string$S0 As String = VB$t_ref$S0.Text
VB$t_ref$S0.Text = VB$t_string$S0
End Sub
As you can see vb is writing some code for you which is nice, but we are still not passing the property by ref.
   -Mike has also talked about the want of passing parameters. ( the same misguided comment that vb supports passing parameters by ref)
Comments [1]

Packaged ASP.NET Programs

Scott posted a nice article Tip/Trick: Creating Packaged ASP.NET Setup Programs with VS 2005  for deploying your Visual Studio 2005 project.  While Scott's article is focused on deploying to a customer I have found those scenarios to be beyond what VS offers and requires Install shield.  I do this this post is a handy trick for moving your project from dev to QA to staging maybe even production.

Comments [0]

Data driven document generation with Word 2007

Erika wrote a couple posts worth a look regarding Data driven document generation.

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 1

Data-driven document generation with Word 2007 and the Office XML File Formats: Part 2

Also If you wanted to take a Mail Merge word doc and port it over to XML

Migrating Mail Merge Fields to Content Controls ( I republished the code so that its legible)


Private Sub Document_Open()

    If (MsgBox("Convert Mail Merge fields to Content Controls?", vbYesNo, "Convert Mail Merge Fields") = vbYes) Then


    End If

End Sub

'** Traverse Mail Merge fields collection and convert each mail merge field into a content control
'** while preserving its font style and format.
Private Sub ConvertMailMergeFields()

    Dim i As Integer, Count As Integer
    Dim currentFont As Font
    Dim mergeFieldname As String
    Dim field As MailMergeField

    For Each field In ActiveDocument.MailMerge.Fields

        '** Select Mail Merge field to process.
        Set currentFont = Selection.Font

        '** Set name for content control.
        mergeFieldname = GetMailMergeFieldName(field.Code)
        Debug.Print ("Processing [" & mergeFieldname & "]")

        '** Remove Mail Merge field and replace it with addition of new content control.

        '** Add new content control.
        AddContentControl mergeFieldname, currentFont
        Count = Count + 1


    Debug.Print ("Number of Mail Merge Fields converted to Content Controls: " & Count)

End Sub

'** Add new content control.
Private Sub AddContentControl(ByVal mergeFieldname As String, ByVal currentFont As Font)

    Dim newControl As ContentControl
    Dim fontStyleName As String

    Set newControl = ActiveDocument.ContentControls.Add(wdContentControlText)

    '** The Title property only allows for 64 characters.
    '** If name is longer, we will put the rest in the Tag property.
    If (Len(mergeFieldname) < 64) Then

        newControl.Title = mergeFieldname
        newControl.Tag = "" 


        newControl.Title = Mid(mergeFieldname, 1, 64)
        newControl.Tag = Mid(mergeFieldname, 65, Len(mergeFieldname) - 64)

    End If

    newControl.SetPlaceholderText , , "Click to add."

    '** Set font for content control.
    fontStyleName = GetFontStyleName(currentFont)
    SetFontStyle newControl, fontStyleName, currentFont

    '** Allow carriage return.
    newControl.MultiLine = True

End Sub

'** Quick and dirty way to check to see if the given font style exist; if it does not, create it.
Private Sub SetFontStyle(ByRef newControl As ContentControl, ByVal fontStyleName As String, ByVal currentFont As Font)

    On Error GoTo Error_FontStyleDoesNotExist

    newControl.DefaultTextStyle = fontStyleName

    Exit Sub


    AddNewFontStyle fontStyleName, currentFont
    newControl.DefaultTextStyle = fontStyleName

End Sub

Private Sub AddNewFontStyle(ByVal newFontStyleName As String, ByVal currentFont As Font)

    Dim fontStyle As Style

    Set fontStyle = ActiveDocument.Styles.Add(newFontStyleName)

    With currentFont

        fontStyle.Font.Name = .Name
        fontStyle.Font.Size = .Size
        fontStyle.Font.Bold = .Bold
        fontStyle.Font.Italic = .Italic

    End With

End Sub

Private Function GetFontStyleName(ByVal currentFont As Font) As String

    Dim fontStyleName As String

    With currentFont

        fontStyleName = .Name
        fontStyleName = fontStyleName & .Size
        fontStyleName = fontStyleName & .Bold
        fontStyleName = fontStyleName & .Italic

    End With

    '** Return result.
    GetFontStyleName = fontStyleName

End Function

Private Function GetMailMergeFieldName(ByVal fieldCode As String) As String

    Dim mergeFieldname As String: mergeFieldname = ""

    '** Name of Mail merge field: MERGEFIELD MailMergeFieldName \* MERGEFORMAT
    mergeFieldname = Replace(fieldCode, "MERGEFIELD", "")
    mergeFieldname = Replace(mergeFieldname, "\* MERGEFORMAT", "")
    mergeFieldname = Trim(mergeFieldname)

    '** Return result.
    GetMailMergeFieldName = mergeFieldname

End Function
Comments [0]

How-To share class/resources between projects in Visual Studio 2005

start by having a solution with two projects.

In the solution exploder window right click the solution and choose add New or existing item.


For this how two we will add a new c# class

click add.  Visual studio will create a new suborder under the solution called "Solution Items" Class1.cs will be in this folder.

now select one of your projects and right click select add existing item.  Navigate to your solutions directory and select Class1.cs

click the drop down arrow next to the add button

select add as link

You will now have a shared link to a common solution file/class

repeat for each project you wish to add this file/class to.

Comments [2]

LINQ for Orcas Beta1

You can find the updated LINQ samples for both VB and C# here

Comments [0]

Beta 1 Of Orcas Released Into The Wild

Incase you missed it yesterday Soma announced the release of Orcas Beta 1, it can be downloaded here.

Comments [0]

My New Favorite Function

I wanted to take a moment to point out my new favorite DotNet Function.  At VSLive in San Francisco I was introduced to System.IO.Path.Combine( path1, path2) as string.  I no longer have to deal with the logic of adding a trailing or leading slash when combing  file and directory locations.

Comments [0]

Teach a child to program

I have finally found some programing books at my reading level ;)


  • C# for Sharp Kids - An ebook on programming with C#
  • VB For Very Bright Kids - An ebook for learning how to programming with Visual Basic
  • Code Rules - A curriculum for learning how to program using Visual Basic. This package includes Teacher materials, Student lessons and quizzes and has been used in schools around the country.
  • CurliQue Studios - A curriculum for learning to build a dynamic web site for a rock band. This one also includes teacher materials. My friend Dave Jacobus blogs about using it with his class and seems to like it.
  • Creating your First Website using HTML - Follow along with this Microsoft DigiGirlz Tech Camp presentation to learn how to use HTML to create your own website!
  • A list of other kid friendly websites and learning resources.

Microsoft's Kid's Corner

Comments [0]

Code Conversion Again

I found another online code converter this time by SharpDevelop. This will convert between C#, VB,  and Boo.  They also have a web service.

Comments [0]