• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

CallByName with Class Functions

I'm having some trouble figuring this out, or even finding out if it's possible.

I have a VB6 app that runs without an interface, however, I'm trying to make some changes and be able to pass in the procedure I want called and then use the CallByName Function to execute it.

The problem that I'm having is that I'm using the Singleton Method for my for Class Modules, and the procedure that I want to run is in that class module.

So if I were to run the code directly from VB it would be like this;

oEmails.DailyErrorLogs "argument 1","argument2"

No matter what syntax I use, I can't figure out a way of executing this when it's within a class module, and I'm using the singleton method.

Any thoughts, this is really getting annoying.

Thanks.

- Anthony
0
ALaRiva
Asked:
ALaRiva
  • 12
  • 11
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I have a VB6 app..."
"I'm using the singleton method..."

How exactly are you achieving that?...

What is "oEmails"?  Is that the Class name or is that a variable that references an instance of a Class?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Is this similar to what you are doing?

' ------------------------------
'  Form1
' ------------------------------
Private Sub Command1_Click()
    Dim c As New Class1
    Dim procedureName As String
    Dim a As String
    Dim b As String
   
    procedureName = "Foo"
    a = "Hello "
    b = "World!"
    MsgBox CallByName(c, procedureName, VbMethod, a, b)
End Sub

' ------------------------------
'  Class1
' ------------------------------
Public Function Foo(ByVal arg1 As String, ByVal arg2 As String) As String
    Foo = arg1 & arg2
End Function
0
 
ALaRivaAuthor Commented:
oEmails is the singleton that references an instance of the Class

DailyErrorLogs is the Method that I am trying to call

- Anthony
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"oEmails is the singleton that references an instance of the Class"

Please explain how you are making "oEmails" a singleton...what do you mean by that?
0
 
ALaRivaAuthor Commented:
OK, I've narrowed down the problem to the Arguments parameter of the CallByName Function.

The problem I'm having is that I'm passing in the parameters in the CommandLine of the Executable.

So I'm passing in xyz,abc

If this were hardcoded, it would be called like this:

CallByName oEmails,"DailyErrorLogs",VBMethod,"xyz","abc"

I've tried single, double, and even triple double-quotes, but all of them seem to fail.

Any thoughts?

In regards to your question on oEmails, it's a function that returns an existing (or creates, if none exists) an instance of the class.  But that's not the problem, the problem is with the arguments parameter.

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So something like?...

' ------------------------------
'  Form1
' ------------------------------
Option Explicit

Private Sub Command1_Click()
    Dim c As New Class1
    Dim procedureName As String
    Dim args As String
    Dim vals As Variant
   
    procedureName = "Foo"
    args = "xyz,abc"
    vals = Split(args, ",")

    MsgBox CallByName(c, procedureName, VbMethod, CStr(vals(0)), CStr(vals(1)))
End Sub

' ------------------------------
'  Class1
' ------------------------------
Public Function Foo(ByVal arg1 As String, ByVal arg2 As String) As String
    Foo = arg1 & arg2
End Function
0
 
ALaRivaAuthor Commented:
Yes, similar to that, however, the number of arguments will vary depending on the procedure.

Any thoughts?

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you change DailyErrorLogs() to receive a ParamArray then your problem is solved as you just have to pass in the return of the Split() funciton.

Otherwise you have no choice but to make some kind of switch structure that lists them all out individually based on the procedurename....
0
 
ALaRivaAuthor Commented:
ok, Thanks.

That was a final option, but looks like it's the only option.

Thank You.  I will award points once I give it a quick test.

- Anthony
0
 
ALaRivaAuthor Commented:
would you happen to have an example of using the ParamArray for this?

I've tried it a couple of different ways, but for some reason, it's not reading them as individual arguments, and it would be a waste of coding to have to run another splitting procedure within each function.

Thanks.

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry...I was mistaken about how to use ParamArray (it's been awhile since I used it).

You would still have to list them out individually to use a ParamArray.

How about just setting the parameter to DailyErrorLogs() as a Variant so it can receive an Array?

' ------------------------------
'  Form1
' ------------------------------
Option Explicit

Private Sub Command1_Click()
    Dim c As New Class1
    Dim procedureName As String
    Dim args As String
    Dim vals As Variant
   
    procedureName = "Foo"
    args = "abc,xyz"
    vals = Split(args, ",")
    MsgBox CallByName(c, procedureName, VbMethod, vals)
End Sub

' ------------------------------
'  Class1
' ------------------------------
Public Function Foo(ByVal args As Variant)
    Dim i As Integer
    For i = LBound(args) To UBound(args)
        Debug.Print i, args(i)
    Next i
    Foo = Join(args, " ")
End Function

0
 
ALaRivaAuthor Commented:
Yup, I ended up going with an Array()

Everything is working fine now.

Thanks for all of the help.

- Anthony
0
 
ALaRivaAuthor Commented:
ok, a quick little problem with this.

How do I handle the Object Parameter of the CallByName() Function?

For this one it's oEmails, however, each one is different, so how would I handle this?

Is there a way of returning an object based a string?

Thanks.

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"Is there a way of returning an object based a string?"

You could use a Collection object and add each object using its name as the key.  Then you can pull them back out "by name"...
0
 
ALaRivaAuthor Commented:
Going that route would eliminate the "dynamic-ness" (yes, I made that up, lol)

OK, so here's what I need to do, this might help explain a little easier.

I have a function called oEmails() that returns an instance of the class object that I need to use in the CallByName() Function

However, oEmails() function isn't the only one.

So how would I go about that?

Obviously the CallByName() Function comes to mind, however, what would the Object Parameter for that be?  I've tried ME within a class module, but that returns the error that the object doesn't support the method that I'm trying to call.

So it's sort of a catch 22.

Or is there an object that I can use that would globally encompass all of the objects/methods within the application?

FYI, oEmails(), and all other functions that return class objects are public functions.

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can't use CallByName() with a global module.  It must be an Object, such as a Class or a Form.
0
 
ALaRivaAuthor Commented:
Yes, I know.  The function that I call (in this example, oEmails()) returns the Class Object.

But as I said, I need to some how, have this function run from a string.  For Example, I have oEmails as a string "oEmails", so I need to run that in the object parameter of the CallByName() Function.

Any thoughts?

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So "oEmails" needs to be in an Object...either a Class or a Form...where is it now?
0
 
ALaRivaAuthor Commented:
oEmails is a function that returns a Class Object.

As I said, CallByName comes to mind for this as well, but what would be the object parameter???

- Anthony
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I understand that oEmails is a function.

What is oEmails itself contained by?!

You want to call oEmails via the CallByName() function no?  So you need to place that function in a Class/Form so you can have a reference to what object is containing it...

Just like my Foo() function in Class1.
0
 
ALaRivaAuthor Commented:
oh, sorry, I was missing the point.

oEmails is contained by a Global Module.  So is there a way of using that as the object?  Or would I have to convert all of that into a class module???  I'd prefer not to.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Unfortunately you can't use CallByName() with a Global Module.  You need to convert it to a Class Module...
0
 
ALaRivaAuthor Commented:
ok, Thank You, I'll just go with that.

Thanks

- Anthony
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now