Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Networks on August 11, 2004, 03:56 PM

Title: Cls's vs. Mod's
Post by: Networks on August 11, 2004, 03:56 PM
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.
Title: Re:Cls's vs. Mod's
Post by: hismajesty on August 11, 2004, 03:58 PM
We discussed this in detail here (http://forum.valhallalegends.com/phpbbs/index.php?board=31;action=display;threadid=7516)
Title: Re:Cls's vs. Mod's
Post by: Stealth on August 11, 2004, 04:19 PM
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.
Title: Re:Cls's vs. Mod's
Post by: ChR0NiC on August 14, 2004, 01:04 AM
Quote from: Networks on August 11, 2004, 03:56 PM
For a sub or function that parses user commands, which do I use?

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 :)
Title: Re:Cls's vs. Mod's
Post by: Grok on August 14, 2004, 03:12 PM
Quote from: ChR0NiC on August 14, 2004, 01:04 AM
Quote from: Networks on August 11, 2004, 03:56 PM
For a sub or function that parses user commands, which do I use?

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 :)

Hmmm...

Public Sub ReturnSomeData( ByRef DataSetToReturn As DATASET)
   'modify DataSetToReturn
   With DataSetToReturn
   .Val1 = 1000
   .Val2 = 2000
   .Tm1 = Now
   End With
End Sub
Title: Re:Cls's vs. Mod's
Post by: ChR0NiC on August 15, 2004, 04:01 PM
I meant for example


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


You can't do that with a sub.
Title: Re:Cls's vs. Mod's
Post by: Eli_1 on August 15, 2004, 08:56 PM
Quote from: ChR0NiC on August 15, 2004, 04:01 PM
I meant for example


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


You can't do that with a sub.

Ignoring how you forgot quotes.  :P


Private Sub Blah(ByRef sData As String, ByRef bResult As Boolean)
bResult = IIf(sData = "1", True, False)
End Sub
Title: Re:Cls's vs. Mod's
Post by: ChR0NiC on August 16, 2004, 02:38 PM
To hell with you all then, fine !! >:( :P
Title: Re:Cls's vs. Mod's
Post by: Networks on August 19, 2004, 05:15 PM
Quote from: ChR0NiC on August 14, 2004, 01:04 AM
Quote from: Networks on August 11, 2004, 03:56 PM
For a sub or function that parses user commands, which do I use?

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 :)

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.
Title: Re:Cls's vs. Mod's
Post by: Newby on August 19, 2004, 08:46 PM
Can you do this exact thing with a sub?! o.O

Dim I As Integer
I = SomeFunctionName(ArgumentOne)

NOPE. You'd have to do.

Dim I As Integer
Call SomeSubName(I, ArgumentOne)

Which looks unsexier.
Title: Re:Cls's vs. Mod's
Post by: Eli_1 on August 19, 2004, 09:09 PM
Quote from: Newby on August 19, 2004, 08:46 PM
Can you do this exact thing with a sub?! o.O

Dim I As Integer
I = SomeFunctionName(ArgumentOne)

NOPE. You'd have to do.

Dim I As Integer
Call SomeSubName(I, ArgumentOne)

Which looks unsexier.

There you go lieing to everyone again, Newby.  The "Call" and two parenthasis are completely unnecessary.   :P