Bob,
You may want to grab a copy of this book and check out chapter 5
for a coverage of constructors and more in designing encapsulated classes.
/tr
From:
helpwithvb@yahoogroups.com [mailto:helpwithvb@yahoogroups.com] On Behalf Of Robert
Dade
Sent: Monday, July 06, 2009 11:46 AM
To: helpwithvb@yahoogroups.com
Subject: [helpwithvb] Help with constructors and elementary OOP
Hello
I am a novice programmer who is doing an Open University
[UK] degree level unit [for fun] on object oriented programming with VB Express
Edition 2008.
I am trying to get my head around a number of ideas right
now, the main ones at present being:
Constructors, what do they do, how, when and where to use
them.
MY PROGRAM
I have a very small program with one form that allows me to
populate a list and then to see those items appear in a list box on a form.
The form's controls consist of :
addNewNumberButton – please see the code below.
addNewNumberTextBox: where I type in a string, in this case
a number but it could be any string , to be added to the list, and then to the
list box on the form.
numberListBox where the strings I type in to addNewNumberTextBox
appear.
Here is the form’s code:
Public Class Form1
Private numberList As
List(Of String)
Public Sub New()
numberList = New List(Of
String)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent()
call.
End Sub
Private Sub
addNewNumberButton_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
addNewNumberButton.Click
numberList.Add(addNewNumberTextBox.Text)
addNewNumberTextBox.Clear()
addNewNumberTextBox.Focus()
updateView()
End Sub
Private Sub
updateView()
numberListBox.Items.Clear()
For Each digit As String In numberList
numberListBox.Items.Add(digit)
Next
End Sub
End Class
I also have an empty class called Lists_AddNumber.
MY FIRST PROBLEM
What I want to do is to use the class Lists_AddNumber to contain
the code [that currently operates in the form’s code] which sets up a list to
accept strings in exactly the same way that the form’s code does.
I would be grateful for an idiot-proof solution to this problem.
MY SECOND PROBLEM
Can anyone please give me some absolutely elementary guidance as to
- What a constructor is [in VB 2008]
and what does it do?
- Where do you use it – in the form
file or in a separate class file?
- What is the syntax of a
constructor?
Best wishes to all you clever folks out there.
Hope that you can point me in the right direction.
Many thanks.
Bob Dade