How do I reinitialise a UDT in VB6? - vb6

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

Related

HP-UFT / VBscript - Setting an array value through a function with an array index reference

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

How to expose an array of UDTs through a property?

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

"That name is not valid" for my VBA function in Excel/Mac

A simple VBA function. When I try to use it in my worksheet, all I get, no matter what I do, is "That name is not valid". I'm out of ideas.
Sub FindABV(temperature)
With Worksheets("Sheet1")
.Range("C28").GoalSeek _
Goal:=temperature, _
ChangingCell:=.Range("B28")
End With
FindABV = .Range("B28").Value
End Sub
I've tried creating a new Module to put it in. No change.
And no error indications from the code editor.
The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.
See more here.
This said, you cannot set a procedure equal to something:
FindABV = .Range("B28").Value
because that name is not valid (you cannot say that a procedure is equal to a certain value, it doesn't make sense). You probably wanted to use a Function to return the value of that cell calculated by the Goal Seeker depending on the input temperature that you pass by the function:
Function FindABV(temperature)
With Worksheets("Sheet1")
.Range("C28").GoalSeek _
Goal:=temperature, _
ChangingCell:=.Range("B28")
End With
FindABV = .Range("B28").Value '<-- return the value
End Function
However, be careful: if =FindABV(temperature) lies on Sheet1.Range("B28"), you will have a circular reference because your function will try to have the value of itself.
Your code will not deliver the results you want. If you want to have the Function work for different values than the ones stored in B28 and C28 you'll have to write it more like this:
Public Function FindABV(goalCell As Range, changeCell As Range, temperature As Double)
goalCell.GoalSeek Goal:=temperature, ChangingCell:=changeCell
FindABV = changeCell
End Function
But this doesn't matter in any case because GoalSeek actually changes the value in the ChangingCell which Excel will not do if it is called from within a Function.

Returning empty UDT From a function

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.

How to clone an object in VB6

I am trying to automatically clone an object without having to instantiate a new one and manually copy every single variable.
I remember back in the day (when I did VB6 everyday) I came up with a method of cloning objects using the PropertyBag, which was pretty cool. But I've lost the code and don't remember how to do it anymore.
Does anyone remember or have another method?
Is this what you were looking for? Article is copied below for posterity.
Serialize Data Using a PropertyBag
You can serialize your data quickly by placing it into a PropertyBag object, then reading the PropertyBags Contents property. This property is really a Byte array that is a serial representation of the data in your PropertyBag object. You can use this byte array for many purposes, including an efficient means of data transmission over DCOM:
Private Function PackData() As String
Dim pbTemp As PropertyBag
'Create a new PropertyBag object
Set pbTemp = New PropertyBag
With pbTemp
'Add your data to the PB giving each item a
'unique string key
Call .WriteProperty("FirstName", "John")
Call .WriteProperty("MiddleInitial", "J")
Call .WriteProperty("LastName", "Doe")
'Place the serialized data into a string
'variable.
Let PackData = .Contents
End With
Set pbTemp = Nothing
End Function
To retrieve the serialized data, simply create a new PropertyBag object and set the serialized string to its Contents property. Convert the string into a byte array before assigning it to the Contents property:
Private Sub UnPackData(sData As String)
Dim pbTemp As PropertyBag
Dim arData() As Byte
'Convert the string representation of the data to
'a Byte array
Let arData() = sData
'Create a new PropertyBag object
Set pbTemp = New PropertyBag
With pbTemp
'Load the PropertyBag with data
Let .Contents = arData()
'Retrieve your data using the unique key
Let m_sFirstName = .ReadProperty("FirstName")
Let m_sMiddleInitial = _
.ReadProperty("MiddleInitial")
Let m_sLastName = .ReadProperty("LastName")
End With
Set pbTemp = Nothing
End Sub
Mike Kurtz, McKees Rocks, Pa.
Method I have used in the past is putting all instance variables on a UDT. As long as you keep the UDT up to date you can copy a class' data with a single method/statement.
Given a "Person" class here's a simple example:
Private Type tPerson
ID As Long
FirstName As String
LastName As String
End Type
Private m_Person As tPerson
Public Sub InitPerson(ID As Long, FirstName As String, LastName As String)
m_Person.ID = ID
m_Person.FirstName = FirstName
m_Person.LastName = LastName
End Sub
Friend Sub SetData(PersonData As tPerson)
m_Person = PersonData
End Sub
Public Function GetClone() As Person
Dim p As New Person
p.SetData m_Person
Set GetClone = p
End Function
Public Property Get FirstName() As String
FirstName = m_Person.FirstName
End Property
To try the code:
Dim p As New Person
p.InitPerson 1, "MyName", "MyLastName"
Dim p2 As Person
Set p2 = p.GetClone
MsgBox p2.FirstName
If you maintain all instance varianbles inside the UDT instead of declaring them seperately you can have simple Clone method that needs very little maintenance.
Another advantage is you can put a UDT to a file handle for quick serialization to disk.
Public Sub Save(filePathName As String)
Dim f As Integer
f = FreeFile()
Open filePathName For Binary Access Write Lock Read Write As #f
Put #f, , m_Person
Close #f
End Sub
A poor men's serialization solution really :-)
Also read: Persisting a Component's Data.
Every object in my application's framework has a Read and Store method. What I do is pass them a stream that writes to a bytearray and store the byte array. While this doesn't eliminate having to handle every property you only have to deal with this once for reading and once for writing.
An alternative is to use a property bag like Darrel Miller says but you still have to deal with each property separately. If you already have read and store then my suggestion should save some time.

Resources