A Simple VB problem - I think

Hierophant

Registered User.
Local time
Today, 23:07
Joined
Mar 14, 2003
Messages
28
Gday,
I've very little experience with VB, so am remedying that. I'm writing a fun little app that will let me document my data CD collection. Ie, you put a CD in, hit 'Document', and away you go. A DB searched will tell lots of interesting things, and should avoid those 30 minute flipping-thru-cd sessions trying to find something.

My problem is - I've written a simple recursive method that takes two arguments. However, VB syntax checker yells when I specify these two arguments in func(arg1, arg2) fashion - it says, "expected '='". Why is this?

Here is where I am calling the function..

Code:
    Dim tempInt As Integer
    For Each subDir In currDir.SubFolders
        tempInt = documentFolder(subDir, 0)
        [COLOR=Red]process (subDir, 0)[/COLOR]
    Next subDir

And here is the recursive function - see it requires a passed in int,
which is supplied literally initially as zero, then is found consequently
as a returned value from the documentFolder() func.

Code:
Function process(ByRef fld As Folder, ByVal par As Integer)
    Dim subFile As File
    Dim subDir As Folder
    
    'document each file in the folder
    For Each subFile In fld.Files
        'document this file
        [COLOR=Red]documentFile(subFile, par)[/COLOR]
    Next subFile
    
    'tempint contains parent ID
    Dim tempInt As Integer
    
    'document each folder in the folder, and then call process on it
    For Each subDir In fld.SubFolders
        'document, and return parent value to be fed into process
        tempInt = documentFolder(subDir)
        'recursively call process
        [COLOR=Red]process(subDir, tempInt)[/COLOR]
    Next subDir
    
End Function

Note that the documentFile(file, integer) function also goes red, and demands a '='.. but strangely the documentFolder(folder, integer) function doesn't spawn any complaints! It returns a value...

Ahh.. this should be an easy one guys. :) Syntax! (I should buy a book, probably)

Cheers, and hope to hear from you soon.

Hiero
 
A function returns a value so the function must appear on the right side of an operator. You have no = so the syntax is not correct.
 
That seems bizarre.
Surely if I have a function..

function func(ByVal a as Integer)
func = a*10
end function

Then I can call this as either
DIm b as integer

b = func(10)

But also,

func(10) - where I throw away the return value. Either of these cases surely should be fine?

It has something to do with the two arguments, as when I only define process as taking a Folder only, and accordingly only supply folders (no int) I have no problem.

Any other ideas? Or could you explain what yuo mean re func return vals?
 
Try putting:

Call process(etc, etc.)
 
I think a simple change would do the trick:
process=process(subDir, tempInt)

VB is complaining, as Mile rightly points out, because a function when returning a value must be assigned a value. (You can call a function that doesn't return a value and never assign a return value. I often do this when I have multiple events on a form requiring a call to the same sub procedure. I write the procedure as a function, then place =FunctionName() in the property sheet.)
 
Last edited:
Call works well - thanks for the replies, both of you.
I just thought VB would be smart enough to call the function and throw away the return value if I didn't involve an assignment operator.. Ahh well.

Cheers!

Hiero
 
I just thought VB would be smart enough to call the function and throw away the return value if I didn't involve an assignment operator
- Personally I like the idea that VBA doesn't assume that I know what I am doing.
 
Hierophant said:
Call works well - thanks for the replies, both of you.
I just thought VB would be smart enough to call the function and throw away the return value if I didn't involve an assignment operator.. Ahh well.

Cheers!

Hiero

Hi Hiero,

You should only use a function if you are wanting to return a value from it.
If you are simply wanting to trigger a set of commands then use a sub-routine.

By calling a function in this way, you're not really understanding what the purpose of a function is. Here is what a function should really do

Dim response As Boolean
Dim strFlavour As String

strFlavour = "Chocolate"
response = chkCocoaBeansNeeded(strFlavour)
If response = True
MsgBox "I need to add Cocoabeans"
Else
MsgBox "No cocoa beans required"
End If


Function chkCocoaBeansNeeded(str) As Boolean

Select Case str
Case Is = "Chocolate"
chkCocoaBeansNeeded = True
Case Else
chkCocoaBeansNeeded = False
End Select
End Function
 

Users who are viewing this thread

Back
Top Bottom