Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I define and use classes in VB?

Status
Not open for further replies.

stigejg

Technical User
Mar 28, 2001
55
NO
Hello,

I am a novice in visual basic programming, and I have just started to learn about this type of programming. During this studies I have been given this problem (and my book doesn’t cover this subject). So my hope is that some of you out there can give me a tip or two about how I can solve this problem:

You are going to make a simple address book, where you shall register name, address and phone number. Every name (just he names) in this address book shall be put into a list box, where you just have to click on the name and the rest of the information shall be listed in the same textboxes where they where put in to this address book.

Problem to solve:

You have to make a class called “Friends” where every name of in the address book is represented by objects in this class.

A little tip to the end: You can’t store object direct in the list box, just strings. Therefore you have to make a table of objects where the object is stored together with the index “item number” in the list box (check property “new index”)

The form contains the list box with the name of “my” friends, there are textboxes for name, address and phone number. There is also command buttons for register new person, store and quit.

Hope someone bother to answer me on this question, and any tip is welcomed with great pleasure.

Thanks in advance Stig
 
Hi,

I'm not going to do your study for you (in that way you won't learn anything anyway). But just let you know how to start:

Add a class to you project (menu Project -> add class module). When you make public functions and subs in the class you create methods of your object. Use 'Public property let' and 'Public property Get' to create properties of your object.
Say Your class is call MyClass and contains
--------------------------------------------------------
Public function AddNumbers(x as single, y as single) as single
AddNumbers=x+y
end function
----------------------------------------------------------

you can use this in your vb project as
Result = MyClass.Addnumber (2.3,4.5)


Good Luck


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I don't know what anyone else thinks but this sounds like it needs a collection class...?

In Friends class (this class holds a reference to all 'Friend' objects in existence):

PS: I've renamed the friend class as cFriend since Friend is a keyword in VB classes

' begin Friends class

Option Explicit
Private m_ColFriends As New Collection

'create a new member in the collection
Public Function add(strName As String) As cFriend
Dim new_friend As New cFriend
new_friend .Name = strName
m_ColFriends.add new_friend , strName
Set add = new_friend
End Function

'return no of objects in collection
Public Function Count() As Long
Count = m_ColFriends.Count
End Function

'returns the item from the collection
Public Function Item(strName As String) As cFriend
Set Item = m_ColFriends.Item(strName)
End Function

'some setup is required for this method,
' make sure the class window is visible and click Tools>Procedure Attributes. Select Enum from the Name drop-down, click 'advanced>>' and in the procedure id enter '-4' (without the quotes. This sets the friends class so you can do foreach...next loops

Public Function NewEnum() As IEnumVARIANT
Set NewEnum = m_ColFriends.[_NewEnum]
End Function

'<---
'delete from collection commented out since not needed
'Public Sub Delete(strName As String)
' m_ColFriends.Remove strName
'End Sub
'--->

'end Friends class

in cFriend class

option explicit

' first declare private properties

private m_strName as String
private m_strAddress as String
private m_intPhoneNumber as String

'next create public properties so somebody outside the class can give them a value ('let') and get their value ('get')

public property let Name(Name as String)
m_strName = Name
End Property

public property Get Name() as String
Name=m_strName
end property

' do the same for the other variables above, just change the names

Now in your form's code

dim f as cFriend
set f=Friends.add(&quot;Joe&quot;)
f.name=&quot;Joe&quot;
f.Address=&quot;13 Kensington Road&quot;
f.PhoneNumber=&quot;(087) 314563&quot;

And for the combobox

for each f in friends
cboNames.additem f.name
next f


...or something like this - I think I may just be confusing you even more. Anyway good luck!

D.

 
Thank you Deetee, you have given me a good start on my work.

Stig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top