• Welcome to Valhalla Legends Archive.
 

Cls's vs. Mod's

Started by Networks, August 11, 2004, 03:56 PM

Previous topic - Next topic

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.

hismajesty

We discussed this in detail here

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.
- Stealth
Author of StealthBot

ChR0NiC

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

Grok

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

ChR0NiC

#5
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.

Eli_1

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

ChR0NiC

To hell with you all then, fine !! >:( :P

Networks

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.

Newby

#9
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.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

Eli_1

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