The Form as a Class
While working on a new article about Overloads, Overrides, and Shadows (look for it soon), I ran across some additional information that extends an earlier article about instantiating the Form object. The earlier article is:
Multiple Form Instantiation in VB.NET
The added information is in a new Quick Tip that you can find on the site here:
Base and Derived Classes
The basis of inheritance!
The plan is to feature a whole new series of articles about using classes in VB.NET! The first one is already on the site:
The main idea with classes is that you can adapt them to what you need through inheritance. This article explains this concept "from the ground up" ... it's written for beginners so you don't have to already understand the answer before you read it!
The Visual Studio Class Designer Tool
You might not be using class diagrams in Visual Studio Professional. I seldom see them as part of technical presentations or even in articles. They're not required ... unless, of course, you want to get the very most out of Visual Studio!
A new article on the site introduces the Class Designer in Visual Studio .NET Professional. "Professional" is the mid-priced model that has most of the really useful stuff, but not the high-priced extras. A different class diagram tool is in the Ultimate version of Visual Studio and nearly all of the Microsoft documentation applies to the expensive spread. (I wonder why?) I decided to provide where Microsoft does not.
VB.NET versus Python
The Sequel!
In which your intrepid guide finds out that the Udacity professor can write code in Python lots better than he can.
Just to complete the story (see below and this link), my Python code worked and passed the automated tests in the class, but when I consulted the approved answer, I discovered that it didn't have to be that hard. So ... just to get as much as I could from this lesson, I also translated the "approved" version of the Python solution to a VB.NET program.
Private Sub btnCheck_Click(
sender As System.Object, e As System.EventArgs
) Handles btnCheck.Click
Dim theString As String = txtString.Text
Dim theStringIndex As Integer = 0
CheckState(txtString.Text, 1)
End Sub
Private Sub CheckState(
theString As String, current As Integer)
' The "approved" Python solution:
' def(fsmsim(String, current, edges, accepting)):
' if string == "":
' return current in accepting
' else:
' letter = string[0]
' if (current, letter) in edges:
' destination = edges[(current, letter)]
' remaining_string = string[1:]
' return fsmsim(remaining_string, destination, edges, accepting)
' else:
' return False
' print (fsmsim("aaa111",1,edges,accepting))
' The VB.NET translation
If theString = "" Then
If current = accepting Then
lblMatch.Text = "True"
Else
lblMatch.Text = "False"
End If
Exit Sub
Else
Dim letter As Char = Strings.Left(theString, 1)
Dim checkKey As New state(current, letter)
If edges.ContainsKey(checkKey) Then
Dim destination As Integer = edges(checkKey)
Dim remaining_string =
Strings.Right(theString, Len(theString) - 1)
CheckState(remaining_string, destination)
Else
lblMatch.Text = "False"
Exit Sub
End If
End If
End Sub
VB.NET versus Python
In which your intrepid guide writes a recursive program in both languages and lives to tell about it.
I recently signed up for CS262, Programming Languages, now being taught at the Udacity web site. I highly recommend the site. This course is being taught with the highest of academic standards and the people in charge are great. In fact, it takes me back to "the good old days" when people wrote code simply because they loved it.
But they want you to write the code in the Python programming language! Arrrghhhh!
Shared Events in VB.NET
If you have written code for ... ummmmm ... more than ten minutes, then you have probably run into situations where nothing you change seems to work. You change the code one way and you get an error message. Then you change it another and that error message goes away, but a different one pops up. It seems that there's no way to fix the code.
Using shared events can be like that, so when I wrote this article, I showed how you can get into one of those no-win situations and how to get out of it again.
A shared event is a fairly specialized code technique, however. You won't find yourself doing this very often, if at all. But like they say ...
When you need it, nothing else will do!!
Shared Variables in VB.NET
Most programmers know about shared methods and properties in VB.NET. Not as many know about shared variables, but this relatively obscure use of Share can add power to your coding toolkit as well. This article is a companion to an earlier article on the creative use of the Share keyword.
Return of the Hub!
Visual Web Developer Database Explorer
I've just returned to a project that I started a year ago, the ASP.NET Hub Site. A year ago, fresh from co-authoring the book Pro ASP.NET 4.0 with VB.NET, I started a project to provide short, interlinked articles that would cover the whole of ASP.NET. Nothing works out exactly as you planned. I got a good start and then other priorities took over. But I've added a new article this week covering Database Explorer. The Database Explorer in Visual Web Developer The Database Centered Tutorial for Beginners also covers this same subject with a focus on Visual Studio rather than Visual Web Developer. But the ASP.NET Hub Site needed an article just for this unique developer environment.
Handling Events - The "Custom" Keyword
After posting my most recent article about custom event handlers, I realized that I hadn't covered the actual "Custom" event handler. It turns out that Microsoft doesn't actually cover it either. While Microsoft's site does have a couple of examples (linked in my latest article), they never really explain what the Custom keyword does.
I decided that the best way to explain it would be use the Custom keyword and duplicate the function of the example that my recent article already uses -- even though it's totally unnecessary. After all, the example works without using the Custom keyword and adding it does bolix up the code. I have to admit that I tried using Microsoft's code examples without success for awhile until I realized that I was doing way too much work. The actual solution was a lot simpler. Read the article and you'll see what I mean.
Escaped Names
Here's a little tip that might trip up people who are not completely familiar with VB.NET: The use of square brackets to identify escaped names.
I was reading the Microsoft documentation on Delegates. The following code was used:
Delegate Function myMethodDelegate(myInt As Integer) As [String]
What the heck is String doing with square brackets?
The Microsoft doc on escaped names reads:
... you can define an escaped name, which is enclosed by brackets ([ ]). An escaped name can match any Visual Basic keyword, since the brackets remove any ambiguity.
Well, String is certainly a keyword. But it's not a name in this context. Checking out the statement in Visual Studio reveals that Visual Studio doesn't think it's a name either.

If you remove the brackets, the color coding changes, but Visual Studio still thinks it's a "type" not a "name". (Which it is.)

I can only conclude that the example was written by a C# programmer or something like that because it doesn't make any sense to me. It works the same way with, or without, the brackets in this code.
The way square brackets are actually used in VB.NET would be more like this:
Dim [String] As String
[String] = "This is a String!"
This is, of course, not a recommended style. But the point is, you can use reserved keywords in VB.NET this way. The only place I see square brackets used this way is in Microsoft documentation and very often, as in the example above, when they're not really necessary or appropriate. If you see them, don't let them confuse you.

