WISE OWL EXERCISES
VISUAL BASIC EXERCISES
VISUAL BASIC EXERCISES
- Drawing forms (2)
- Form events (1)
- Variables and data types (2)
- Enumerations and constants (2)
- Form validation (3)
- Menus and toolbars (2)
- Files and folders (1)
- Basic DataGridViews (1)
- Reacting to DataGridView events (3)
- Advanced DataGridViews (2)
- Creating classes (4)
- Coding in VB.NET (2)
- Variables and constants (3)
- Testing conditions (2)
- Passing arguments (3)
- Using arrays (2)
- Loops (2)
- Working with files (3)
- Lists (3)
- Creating properties (3)
Visual Basic | Creating classes exercise | Use classes to generate consonants and vowels for Countdown
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You can learn how to do this exercise on the relevant Wise Owl classroom training course (sadly for the moment only in the UK).
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Right-click on your project in Solution Explorer and choose to add the form from the above folder (you can alternatively press SHIFT + ALT + A ).
The idea behind this exercise is that you should be able to choose your Countdown letters by clicking on the vowel and consonant buttons as required:
This is the sort of thing you should see when everything's working (this one's a bit easy - there's an obvious 9-letter word in FWORJAAMI).
Lots of help now follows, but before turning the page, have a think about how you might design a Letter class to hold each letter chosen.
Every time you click on one of the two buttons, your code should generate a new letter of the appropriate type (vowel or consonant) and add this as a textbox to the form. Here's one suggestion of what this code could look like, for adding a vowel:
Private Sub btnVowel_Click(sender As System.Object, _
e As System.EventArgs) Handles btnVowel.Click
'create a new letter
Dim NextLetter As New Letter
'this is a vowel
NextLetter.IfVowel = True
'add it to the form
'check how many letters there are
The CheckNumberLetters method should count how many letters have been added already, and if this is now 9 it should:
- disable the two buttons by setting their Enabled properties to False; then
- display a message that the countdown clock has begun!
The above shows just one way to solve this problem. With classes there is rarely a right and a wrong way - just degrees of elegance.
To get the code above to work, create a Letter class and give it the following methods and properties:
|IfVowel||Public||Read-write property||Whether this is a vowel or not|
|Choice||Private||Read-only property||The randomly generated letter|
|AddForm||Public||Method||Add a text box to the form|
When you create a new instance of the Letter class you should increase the value of a shared variable called NumberLetters by 1. When you first open the form, you should set the value of this shared variable to 0.
Create the following fields and constants in your Letter class:
'the number of letters so far
Public Shared NumberLetters As Integer = 0
'generate a random number seed
Private Shared r As New Random()
'the horizontal offset for the first letter
Private Const initialGap As Integer = 24
'the horizontal offset for all other letters
Private Const gapBetweenLetters As Integer = 54
Private Const Vowels As String = "AEIOU"
Private Const Consonants As String = "BCDFGHJKLMNPQRSTVWXYZ"
The random number seed has to be shared, as otherwise you'll get the same random number for each letter.
Create a read-write Boolean property called IfVowel, then a public read-only property called Choice which should return the letter generated. You may find the following two lines of code useful (although you'll need to amend them):
'get a random number between 1 and N
Dim ChoicePosition As Integer = r.Next(1, SomeString.Length)
'return the letter at this position
Return SomeString.Substring(ChoicePosition - 1, 1)
Now create the AddForm method, which should begin like this:
'add the letter as a textbox to the form's groupbox
Public Sub AddForm(grp As GroupBox)
This method thus takes the group box as an argument, and adds one textbox to the group box for each letter found. You should now add code to this method so that it:
- Creates a new text box;
- Adds it to the group box;
- Sets various formatting properties; and
- Sets the text in the box to equal the value of the Choice property.
The easiest way to get this code is to add a textbox to the group box within the form, then copy and edit the generated designer code.
When everything is working, generate a Countdown word using your system (but don't worry about the fact that it gives equal weight to common and obscure letters).