Valhalla Legends Forums Archive | Visual Basic Programming | Cls's vs. Mod's

AuthorMessageTime
Networks
Lately I've become really confused between classes and modules. Some people tell me I use them too much others say it's ok in any event I would like to know when it is best to use a class over a module and vice versa.
For example let's say I want to create a lastseen index using an array, would a class be best for that? For a sub or function that parses user commands, which do I use? How a database that stores users and allows editting of them, which do I use? I'd like also to go back to basics..What do classes allow you to do differently from modules and vice versa. I've heard different things from Stealth and Sidoh, and other people as well. What If I don't ever unload my database class until the application ends should it have been a module? Anyway please post.
August 11, 2004, 8:56 PM
hismajesty
We discussed this in detail here
August 11, 2004, 8:58 PM
Stealth
Classes are for code that needs to be duplicated, and accessed on a custom scope. Because you can instantiate a class as many times as you want, you can create copies of the same code dynamically as your program runs. Also, you can instantiate classes on any scope - module-level, global, procedure-level, as you please. They're a lot more flexible in that way.

Modules are for code that does not change and needs to be globally accessible.


IMO:

Your lastseen index that uses an array, is probably best implemented as a class. That way if you need more than one index you can instantiate another one. Also, you can write a simple wrapper for the Collection class that you can reuse later.

Your database is probably best implemented as a class as well.

Your function that parses user commands can go either way.
August 11, 2004, 9:19 PM
ChR0NiC
[quote author=Networks link=board=31;threadid=8133;start=0#msg75085 date=1092257776]
For a sub or function that parses user commands, which do I use?[/quote]

Well Functions return data, Subs do not. Ask yourself, is this procedure going to return any data perhaps a true/false statement, using the write type can also help save memory :)
August 14, 2004, 6:04 AM
Grok
[quote author=ChR0NiC link=board=31;threadid=8133;start=0#msg75449 date=1092463485]
[quote author=Networks link=board=31;threadid=8133;start=0#msg75085 date=1092257776]
For a sub or function that parses user commands, which do I use?[/quote]

Well Functions return data, Subs do not. Ask yourself, is this procedure going to return any data perhaps a true/false statement, using the write type can also help save memory :)
[/quote]

Hmmm...

[code]Public Sub ReturnSomeData( ByRef DataSetToReturn As DATASET)
'modify DataSetToReturn
With DataSetToReturn
.Val1 = 1000
.Val2 = 2000
.Tm1 = Now
End With
End Sub[/code]
August 14, 2004, 8:12 PM
ChR0NiC
I meant for example

[code]
Private Function Blah(Data As String) As Boolean
If Data = 1 Then Blah = True
If Data = 2 Then Blah = False
End Function
[/code]

You can't do that with a sub.
August 15, 2004, 9:01 PM
Eli_1
[quote author=ChR0NiC link=board=31;threadid=8133;start=0#msg75639 date=1092603687]
I meant for example

[code]
Private Function Blah(Data As String) As Boolean
If Data = 1 Then Blah = True
If Data = 2 Then Blah = False
End Function
[/code]

You can't do that with a sub.
[/quote]

Ignoring how you forgot quotes. :P

[code]
Private Sub Blah(ByRef sData As String, ByRef bResult As Boolean)
bResult = IIf(sData = "1", True, False)
End Sub[/code]
August 16, 2004, 1:56 AM
ChR0NiC
To hell with you all then, fine !! >:( :P
August 16, 2004, 7:38 PM
Networks
[quote author=ChR0NiC link=board=31;threadid=8133;start=0#msg75449 date=1092463485]
[quote author=Networks link=board=31;threadid=8133;start=0#msg75085 date=1092257776]
For a sub or function that parses user commands, which do I use?[/quote]

Well Functions return data, Subs do not. Ask yourself, is this procedure going to return any data perhaps a true/false statement, using the write type can also help save memory :)
[/quote]

You took the question the wrong but I understand how you did my bad =p

I meant should I use a class or a module for my function/sub that parses commands.
August 19, 2004, 10:15 PM
Newby
Can you do this exact thing with a sub?! o.O

[code]Dim I As Integer
I = SomeFunctionName(ArgumentOne)[/code]
NOPE. You'd have to do.

[code]Dim I As Integer
Call SomeSubName(I, ArgumentOne)[/code]
Which looks unsexier.
August 20, 2004, 1:46 AM
Eli_1
[quote author=Newby link=board=31;threadid=8133;start=0#msg76353 date=1092966364]
Can you do this exact thing with a sub?! o.O

[code]Dim I As Integer
I = SomeFunctionName(ArgumentOne)[/code]
NOPE. You'd have to do.

[code]Dim I As Integer
Call SomeSubName(I, ArgumentOne)[/code]
Which looks unsexier.
[/quote]

There you go lieing to everyone again, Newby. The "Call" and two parenthasis are completely unnecessary. :P
August 20, 2004, 2:09 AM

Search