• Welcome to Valhalla Legends Archive.
 

[VB6] Returning Objects with Public Functions (in Module)

Started by Sorc_Polgara, June 28, 2004, 08:22 PM

Previous topic - Next topic

Sorc_Polgara

I want to be able to return a TextStream object... I want to put it in a module and I want it to be like a Public Function.

I'm using a TextStream object frequently and repeating the same code over and I thought that I could make it function to save some time.

I've tried making a Public Function but I can't get it to work.  In order to make the TextStream object (tst) that I want, a FileSystemObject (fso) must be set.

Here is my code in the module


Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As TextStream
   Dim fso As FileSystemObject
   Dim tst As TextStream
   
   Set fso = New FileSystemObject
   Set tst = fso.OpenTextFile(FileName, IOMode, Create)
   CreateTST = tst
End Function


It works fine when I insert the code into the Sub that I want it to be used in.

This is the call I make


Dim tst As TextStream
tst = CreateTST(strFileName, 1, False)


the argument "1" in the function call is the numerical representation for constant ForReading which must be stated inorder to use OpenTextFile.

I think it should work!  but all I get is the "Run-time error '5':  Invalid Procedure call or argument"

Please help!  How can I return a TextStream/FileSystemObject!

Stealth

Try returning it as a Variant into a TextStream object:


Dim objTS as TextStream

objTS = CreateTST(myFilename, 1, True)

'// ...

Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant
- Stealth
Author of StealthBot

Sorc_Polgara

Quote from: Stealth on June 29, 2004, 08:34 AM
Try returning it as a Variant into a TextStream object:


Dim objTS as TextStream

objTS = CreateTST(myFilename, 1, True)

'// ...

Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant


I changed it, but I get a different run-time error, "Run-time error '438':  Object doesn't support this property or method"

Here is the code calling to the function...

Dim tst1 As TextStream
tst1 = CreateTST(strFileName, 1, False)


... the function in the module


Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant
   Dim fso As FileSystemObject
   Dim tst As TextStream
   
   Set fso = New FileSystemObject
   Set tst = fso.OpenTextFile(FileName, IOMode, Create)
   CreateTST = tst
End Function


When I debug it the following is highlighted in the function above,


CreateTST = tst


Is there another way to return an object like a FileSystemObject?

Stealth

Perhaps create it ahead of time and pass it ByRef?
- Stealth
Author of StealthBot

Adron

You could also try adding "Set" like this:


Set tst = CreateTST(strFileName, 1, False)


Your assignments without "Set" may be trying to assign to the default property.