I have a class cDept which has a UDT defined.
public type udtEmp
Name as string
Id as long
end type
I have an array defined:
private m_Emps() as udtEmp
I want to expose the array through a property. I tried the following:
Public Property Get Employees() As udtEmp()
Employees= m_Emps
End Property
So far everything compiles. Now I instantiate the class and try to access the property.
dim myUdt as udtEmp
dim oDept as cDept
set oDept = new cDept
myUdt = oDept.Employees(1) ' -- error
I get an error stating Wrong number of arguments or invalid property assignment.
What am I missing?
(Not tested)
I think your property access is trying to use the '1' as an argument to the property (which has no arguments), thus the 'wrong number' error. Rather than trying to property get the array and then index access the array, will it work to have the property get (or a different one) return the desired array element?
Public Property Get Employees(ndx as long) As udtEmp
Employees= m_Emps(ndx)
End Property
Related
I have a class :
class Con {
private List<Ind> inds;
}
I am using Gson in the usual way to convert a JSON string to this class object. so in case, the JSON doesn't have the key inds present this variable inds is assigned a null value. Is there a way to assign inds an empty ArrayList instead?
My Thoughts:
One straightforward way could be once the Gson object is built. Go over all the null objects and assign them to the new ArrayList<>(). Is there a better approach?
public List<Ind> getInds() {
return inds;
}
Currently I am using the above getter in a code like : con.getInds().stream() which is causing NullPointerException.
I am not sure what would be a good way to resolve this. Instead of List Should I return an Optional or Should I modify this getter like
public List<Ind> getInds() {
inds==null?new ArrayList<>():inds;
}
The above will also resolve the NullPointerException. Not sure if there are pros and cons to using this approach. Although now there is no way to identify if the Json has a key with name inds or not. For the current code that I am writing this may not be required. But there is a meaning loss here certainly.
One solution to this would be to assign default values to the fields, for example:
class Con {
private List<Ind> inds = new ArrayList<>();
}
Gson will keep this default value; only if the field is present in the JSON data it will reassign the field value.
There are however a few things to keep in mind:
Your class needs a no-args constructor (implicit or explicit); otherwise Gson might create instances without invoking the initializer blocks of the class, and therefore the field will be null
If the field is present in JSON but has a JSON null value, then Gson will still set that as value
You cannot tell afterwards whether the field was present in JSON but had an empty JSON array as value, or whether it was missing
I have an issue with assigning/retrieving correct values to/from an array which is within a class in VBScript. Whenever I try to set the array's value through a stand-alone function, it just does not work and I keep getting the old array values.
Here is an example code:
Class NewClass
Public TestValues
Public Sub Init()
TestValues = array("value0", "value1", "value2")
End Sub
End Class
Class NewFunctions
Public Function GetValue(xRef)
GetValue = xRef(2)
print "Value within Function: " & xRef(2)
End Function
Public Sub SetValue(xRef, xValue)
xRef(2) = xValue
print "Value within Sub: " & xRef(2)
End Sub
End Class
Dim MyClass, MyFunction
Set MyClass = New NewClass
Set MyFunction = New NewFunctions
Now, when I try to set the index 2 of the given array MyClass.TestValues with the SetValue Sub, it claims the new value has been set, but when I then call GetValue for this array or print out the content of the MyClass.TestValues(2) directly, I am still getting the old values:
MyFunction.SetValue MyClass.TestValues, "newvalue2"
It returns: Value within Sub: newvalue2
But when I retrieve the value with GetValue:
MyFunction.GetValue MyClass.TestValues
It returns: Value within Function: value2, which is the old value.
When I, however, set the array directly:
Myclass.TestValues(2) = "newvalue2"
then calling with:
MyFunction.GetValue MyClass.TestValues
gives me correct result: Value within Function: newvalue2
I am not sure whether this is a general VBScript behavior and I am making a mistake in hoping to change array values in this 'dirty' manner or whether this is strictly HP-UFT (Unified Functional Testing) related, since this is where I could observe this.
I am not a profficient VBScripter either so I appreciate any help.
This is a documented behaviour
Argument in a Class
If the parameter is specified as ByRef, the argument is passed by
value if the variable sent as an argument is in a class
I'm creating XML documents with VBScript and MSXML DOM. In order to structure and simplify my code I'm using classes and thus methods (functions in VBS) too.
Here is a little function that troubles me:
function createAttribute(name, value)
dim doc
Set doc = CreateObject("Msxml2.DOMDocument.4.0")
dim attr
set attr= doc.createNode(2,name,"")
attr.NodeValue=value
createAttribute=attr
end function
The assignment createAttribute=attr, where I'm setting the return value of the function, causes the following error:
Object doesn't support this property or method
As the web resources on XML processing with VBS are rather sparse, I hope some of you can help me to understand whats going on here. Here are my questions:
What object does not support what property or method?
Can i pass objects of any given class as return values of VBS functions?
Can i pass an object of the class IXMLDOMAttribute as a return value to a VBS function?
I think the problem is that attr is an object, so you need to use set to apply the return value. Otherwise, you may simply be returning attr's default property value (if it has one):
set createAttribute = attr
You don't show how you use the return value, so I can't comment on that but it is possibly the source of the error.
I have written a function that returns a User Defined Type.
How can i return a empty UDT in case of any error from the function?
I tried setting function to 'Nothing',but it is throwing 'Object Required' error.
Thanks in advance.
If at all possible, use a Class/Object instead. Even doing something as simple as turning this type:
Public Type EmpRecord
FName As String
LName As String
HiredDate As Date
End Type
into a class can be done by adding a Class to your project called EmpRecord and including just this in it:
Public FName As String
Public LName As String
Public HiredDate As Date
Then you can return Nothing from a function that gets an error while retrieving the record.
In VB6, a user-defined type is a "value type", while a class is a "reference type". Value types are typically stored on the stack (unless they're a member of a class). Reference types are stored as a pointer on the stack pointing to a place in the heap where the actual instance data is stored.
That means a reference to a class can be Nothing (the pointer is zero), whereas a value type can't.
There are multiple ways to solve your problem:
Add a Boolean member to your user-defined type to indicate success or failure.
Create another "wrapper" UDT like (see below) and return it from your function.
Change your UDT into a class, which can be Nothing (as tcarvin said).
Write the function to return a Boolean and take a ByRef parameter. The results are written to the parameter passed in if the function result is True. (A lot of people don't like this, but it's a common solution you should be aware of.)
Wrapper:
Public Type Wrapper
Success As Boolean
Inner As YourOriginalUDT
End Type
Function with ByRef:
Function Foo(ByRef Result As YourOriginalUDT) As Boolean
...
If Success Then
Foo = True
Result.A = A
Result.B = B
Result.C = C
... etc. ...
End If
End Function
A UDT can't be empty.
You can either use a "dummy" unintialised UDT, or just set all it's members back to the default values.
I've got a loop, which is reading in a stack of XML files, for each one, it validates the data that was in the XML and loads it into some UDTs and then does some work on the data.
Then it gets back to the beginning of the loop and the UDTs still have data in from the previous XML. If that tag is defined in the new one, it overwrites, but if that tag isn't defined, then that element in the UDT is left alone.
But I can't reset the UDT by the technique I'd use for a variable (Let X = 0) unless I go through every single element of the UDT and reset the value. And doing it object-style (Set X as New UDT) doesn't work.
How do I do it?
Dim a new variable as the UDT and set the old one equal to the new variable.
For instance:
Dim XEmpty as UDT
X = XEmpty
Will reinitialise a variable X that is a UDT of type UDT.
You can use an empty utility function that simply returns the UDT
public function newTFoo() as TFoo
'//
end function
dim t as TFoo
t.x = 1234 ...
t = newTFoo()
'// t is reset