I have the following function that works fine:
Function GetAgentEmailWorksheet(AgentObjectId As String)
Dim specific_agent As clsAgent
Set specific_agent = New clsAgent
specific_agent.AgentSheetName = "agentsFullOutput.csv"
Dim id_array() As Variant
id_array = specific_agent.AgentIDArray
Dim email_array() As Variant
email_array = specific_agent.AgentEmailArray
GetAgentEmailWorksheet = vlook_using_array(AgentObjectId, id_array, email_array)
End Function
When, however, I change the last line to:
GetAgentEmailWorksheet = vlook_using_array(AgentObjectId, specific_agent.AgentIDArray, specific_agent.AgentEmailArray)
I get the following error:
Compile error:
Type mismatch:array or user-defined type expected
and it hi-lights AgentIDArray (or AgentEmailArray if I sub out the first parameter.
Why?
EDIT
here's the fuction vlook_using_array:
Function vlook_using_array(target_string As String, _
input_array() As Variant, _
output_array() As Variant)
Dim rows_dim As Long
Dim cols_dim As Integer
For rows_dim = 1 To UBound(input_array, 1)
For cols_dim = 1 To UBound(input_array, 2)
If input_array(rows_dim, cols_dim) = target_string Then
vlook_using_array = output_array(rows_dim, cols_dim)
End If
Next cols_dim
Next rows_dim
End Function
Here are the clsAgent properties:
Public Property Get AgentClientsArray() As Variant
AgentClientsArray = get_column_array(AgentClientsCol)
End Property
Public Property Get AgentIDArray() As Variant
AgentIDArray = get_column_array(1)
End Property
Public Property Get AgentEmailArray() As Variant
AgentEmailArray = get_column_array(AgentEmailCol)
End Property
Here's the function that's in the class module:
Private Function get_column_array(col_num As Integer) As Variant
' create a range out of the used range (of the sheet) in the column specified
' used to create array properties in the class
Dim total_rows As Long
total_rows = Worksheets(Me.AgentSheetName).UsedRange.rows.Count
Dim target_range As Range
With Worksheets(Me.AgentSheetName)
Set target_range = .Range(.Cells(1, col_num), .Cells(total_rows, col_num))
End With
Dim target_arr() As Variant
target_arr = target_range
get_column_array = target_arr
End Function
There's a difference between this:
Public Property Get AgentIDArray() As Variant
AgentIDArray = get_column_array(1)
End Property
and
Public Property Get AgentIDArray() As Variant()
AgentIDArray = get_column_array(1)
End Property
The first (which you have in your class) returns a Variant which happens to be an array, the second an array of variants.
So, the return value of your AgentIDArray is not consistent with the parameter types expected by vlook_using_array
Related
I made a class in VBScript and used it in asp classic to instatiate an object:
This is my class:
<%
Class RBAC
Public dateTimeValue
Public userIdValue
Public fileIdValue
Public actionValue
Public Property Get DateTime()
'Gets the propery value
DateTime = dateTimeValue
End Property
Public Property Set DateTime(value)
'Sets the property value
dateTimeValue = value
End Property
Public Property Get UserId()
'Gets the propery value
UserId = userIdValue
End Property
Public Property Set UserId(value)
'Sets the property value
userIdValue = value
End Property
Public Property Get FileId()
'Gets the propery value
FileId = fileIdValue
End Property
Public Property Set FileId(value)
'Sets the property value
fileIdValue = value
End Property
Public Property Get Action()
'Gets the propery value
Action = actionValue
End Property
Public Property Set Action(value)
'Sets the property value
actionValue = value
End Property
Public Sub Insert()
sqlMethods = "INSERT INTO RBAC ([DateTime],[UserId],[FileId],[Action]) VALUES ("+dateTimeValue+","+userIdValue+","+fileIdValue+","+actionValue+",)"
Conn.Execute(sqlMethods)
End Sub
End Class
%>
And here I instantiate an object and set it's properties:
Dim RbacObject
Set RbacObject = New RBAC
Set RbacObject.DateTime = Now
Set RbacObject.UserId = Cstr(Session("cgsid"))
sqlFileId = "SELECT int_fileid FROM tbl_SecFiles where str_filename = '"&split(Request.ServerVariables("SCRIPT_NAME"),"/cgs/")(1)&"'"
Set RS = Conn.Execute(sqlFileId)
Set RbacObject.FileId = RS("int_fileid")
Set RbacObject.Action = "<"&stringMethods&"><old>"&enabled_profiles_old&"</old><new>"&enabled_profiles_old&siteid&",</new></"&stringMethods&">"
RbacObject.Insert
The problem is that only FileId gets a value rest of the fields are empty, even if I set them an value. What am I doing wrong?
Set is used for assigning objects to variables. So
Set RbacObject = New RBAC
is correct, but all other statements like
Set RbacObject.DateTime = Now
are not. Use
RbacObject.DateTime = Now
instead.
Set RbacObject.FileId = RS("int_fileid")
is a borderline case: fileIdValue will contain a Field object, but evaluate to its .Value when used in a 'non-object context' (like IO or computations).
You shouldn't run dubious code with/under On Error Resume Next.
Demo:
copy con 10.vbs
Class C
Public V
End Class
Set O = New C
Set O.V = "don't do this at home."
^Z
cscript 10.vbs
... 10.vbs(5, 1) Microsoft VBScript runtime error: Object required: 'O.V'
Demo II (to prove that 'it works' if you don't use Set for assignment of non-objects, and indicate that there must be other error hidden by the evil OERN if it 'still doesn't work'):
Class C
Public V
End Class
Set O = New C
On Error Resume Next
Set O.V = "don't do this at home."
WScript.Echo Err.Description
On Error GoTo 0
WScript.Echo "Set O.V => '" & O.V & "'"
O.V = "don't do this at home."
WScript.Echo "O.V => '" & O.V & "'"
output:
cscript 10.vbs
Object required
Set O.V => ''
O.V => 'don't do this at home.'
In addition to what Ekkehard.Horner said you need to define the setters for properties that don't take objects as
Public Property Let name(value)
not as
Public Property Set name(value)
I have been trying to implement a very simple tree structure in VBA for some basic text parsing needs. The relevant code follows.
Private pLeaves() As CTree
Private numLeaves As Integer
Private leavesLen As Integer
Private iterate As Integer
Private pParent As CTree
Public pValue As Object
Public Sub class_initialize()
ReDim pLeaves(10)
leavesLen = 10
numLeaves = 0
Set pParent = Nothing
iterate = 0
Set pValue = Nothing
End Sub
Public Sub Class_terminate()
'We'll leave it empty for now
'if it looks like it's not working right we might change it
End Sub
Public Property Get Parent() As CTree
Parent = pParent
End Property
Public Property Get Leaves() As CTree
Leaves = pLeaves
End Property
Private Property Set Parent(ByRef p As CTree)
Set pParent = p
End Property
Private Property Set value(ByRef value As Object)
Set pValue = value
End Property
Public Sub Add_Leaf(ByRef value As Object)
Dim n As Integer
n = numLeaves
If numLeaves >= leavesLen Then
ReDim Preserve pLeaves(leavesLen + 10)
leavesLen = leavesLen + 10
End If
Set pLeaves(n) = New CTree
Set pLeaves(n).Parent = Me
Set pLeaves(n).value = value
End Sub
Public Function DFFind_Leaf(value As Object) As CTree
Dim i As Integer
If pValue = value Then
Set DFFind_Leaf = Me
Return
End If
If numLeaves = 0 Then
Set DFFind_Leaf = Nothing
Return
End If
For i = 0 To numLeaves
Set DFFind_Leaf = pLeaves(i).DFFind_Leaf(value)
If DFFind_Leaf <> Nothing Then
Return
End If
Next i
Set DFFind_Leaf = Nothing
Return
End Function
When I try to call the Add_Leaf function with an object though I end up getting a
method or data member not found error from VBA on the line where I'm trying to
set pLeaves(n).Parent= me
Any idea what the reason for that might be?
Edit: ok I figured out how to fix this. Changing the set property Parent(...) from private
to public fixed the problem. So apparently I don't quite understand what private exactly does in vba. If someone wants to explain how to do this without essentially exposing the variables for anyone to set as they want I would be grateful.
Private tells the VBA interpreter that the scope of the variable is restricted to the class/module only. You can therefore access it only inside of the original class, but not from the outside. Publicon the other hand allows you to access the variable from the outside.
However, note that when working with classes, it is strongly discouraged to publicly expose any variables. Instead it is best practice to expose so called properties, using the SET PROPERTYand GET PROPERTY keywords.
I.e. instead of
Public Amount as Double
It is often better to use this approach:
Private mAmount as Double
Property Set Amount(amt as Double) 'Note that by default, properties are public
If amt > 0 Then
mAmount = amt
Else
mAmount = 0
End If
End Property
Property Get Amount as Double
Amount = mAmount
End Property
As you can see from this example, the advantage is that you can actually add additional verifications/modifications and therefore ensure the consistency.
In your example, you could consider to simply provide a method, i.e. a public sub, that adds a leave and takes care of all the settings, e.g.
Public Sub AddLeave(strName as String)
Dim objLeave as New CTree
objLeave.Init strName, Me
mLeaves.Add objLeave, strName
End Sub
Public Sub Init(strName as String, objParent as CTree)
Me.Name = strName
Set Me.Parent = objParent
End Sub
I'm trying to create a class module to build some basic db functionality in a workbook. The problem I'm running into is attempting to add a worksheet as a class member. I keep getting "Invalid use of property" as an error.
My class declaration:
Option Explicit
Private pboolLock As Boolean
Private pintColCount, pintRowCount As Integer
Private pWorksheet As Excel.Worksheet
'Lock bit properties:
Property Get boolLock() As Boolean
boolLock = pboolLock
End Property
Property Let boolLock(boollockval As Boolean)
pboolLock = boollockval
End Property
'Utility properties- no sets
Property Get ColCount() As Integer
ColCount = pintColCount
End Property
Property Get RowCount() As Integer
RowCount = pintRowCount
End Property
'Worksheet specific props
Property Set dpDefine(ByRef wks As Worksheet)
Set pWorksheet = wks
End Property
Property Get dpDefine() As Worksheet
dpDefine = pWorksheet
End Property
Different module: Class instantiation:
Sub tryClass()
Dim thisdp As New Cdatapage
Dim iansTest As String
iansTest = Sheets("typical datapage").Name
'this works, so reference is being passed:
MsgBox ("The name is " & iansTest)
'this doesn't work:
thisdp.dpDefine (Sheets("typical datapage"))
End Sub
Any suggestions? Thanks.
Its a Set property so you need to:
Set thisdp.dpDefine = Sheets("typical datapage")
Or if you change dpDefine to a Let you can;
thisdp.dpDefine = Sheets("xxx")
Given that we have a script
Option Explicit
Class CClass
Private m_date
Private Sub Class_Initialize()
m_date = CDate("1970-01-01 00:00:00")
End Sub
Public Function Foo()
Dim d : d = Date()
WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
End Function
Public Property Get Date()
Date = m_date
End Property
Public Property Let Date(p_date)
m_date = CDate(p_date)
End Property
End Class
Dim obj : Set obj = NEW CClass
Call obj.Foo()
How can class function CClass.Foo() call built-in VBScript function Date() without the property CClass.Date interfering?
My current solution is to introduce a dummy Date_() function which can be called instead. But that just seems wrong. I'm thinking there should be some way to specify that we want to call something outside the class scope.
I am almost positive that there is no way to do what you're asking in VBScript.
But even if you could figure out a way to do this, you really shouldn't. You need to choose names for your own functions that don't conflict with the names of built-in functions. Anything else is completely unmaintainable for a dynamic scripting language like VBScript.
Pick a different name for your Date property. Preferably something more descriptive: what kind of date does that property return? What does the date refer to? How is it likely to be used? Whatever you do, don't rename it to Date_—that's not any better.
You can call it from inside the class like:
Dim d : d = me.Date()
Me in VBScript is the same as you use This in Javascript for example
Too late to the party, but there is a solution using eval function.
see eval function docs for details
Class CClass
Private m_date
Private Sub Class_Initialize()
m_date = CDate("1970-01-01 00:00:00")
End Sub
Public Function Foo()
Dim d : d = eval("Date()")
WScript.Echo "d is " & FormatDateTime(d, vbGeneralDate)
End Function
Public Property Get Date()
Date = m_date
End Property
Public Property Let Date(p_date)
m_date = CDate(p_date)
End Property
End Class
Dim obj : Set obj = NEW CClass
Call obj.Foo()
I have created two classes as interface / implementation classes, and wish to pass a particular example of one class to a method in the other. The definitions are as follows...
Class BigInt...
Option Explicit
Public Sub dothing(ByRef passed_object As MyInt)
End Sub
and an implementation BigImplementation...
Option Explicit
Implements BigInt
Public Sub BigInt_dothing(ByRef passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub
The class I am planning to pass is...
Option Explicit
Public Property Get getprop() As Integer
End Property
Public Property Let letprop(ByVal myval As Integer)
End Property
implemented as MyImplementation thus...
Option Explicit
Implements MyInt
Private myval As Integer
Public Property Get myint_getprop() As Integer
myint_getprop = myval
End Property
Public Property Let myint_letprop(ByVal passed_int As Integer)
myval = passed_int
End Property
I am then driving this with the following snippet of code:-
Private Sub Command_Click()
Dim myobj As MyInt
Set myobj = New MyImplementation
Dim mybigobj As BigInt
Set mybigobj = New BigImplementation
myobj.letprop = 1
Call mysub(myobj)
mybigobj.dothing (myobj) ' Line with problem
End Sub
Private Sub mysub(ByVal passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub
When the execution reaches the line marked, I get run-time error 438 - Object doesn't support property or method. The call to the ordinary function mysub works perfectly. Does anyone know what I am doing wrong and what I need to do to fix this?
Use either
mybigobj.dothing myobj
or
Call mybigobj.dothing(myobj)
Putting extra parentheses around a reference evaluates its default property and passes it's value as actual argument.
mybigobj.dothing requires its parameter to be a MyInt
Public Sub BigInt_dothing(ByRef passed_obj As MyInt)
Dim i As Integer
i = passed_obj.getprop
End Sub
You're passing a MyImplementation
Set myobj = New MyImplementation
So possiblly something like (my vb is rusty)
mybigobj.dothing (myobj.myint_getprop())