How do I remove a UDT that the program creates?
Printable View
How do I remove a UDT that the program creates?
You can't 'erase' (reclaim all the memory used by) a variable dimmed as a UDT unless the variable goes out of scope. You can reset all fields to their default states in a UDT with code like;
Code:Dim MyUDT as MyType
Dim MtUDT as MyType
'code to fill MyUDT eg...
MyUDT.name = "My Name"
'etc
'clear MyUDT
MyUDT = MtUDT
I assume you mean 'empty' it(?)
If you have a UDT Array thenCode:Private Type MyUDT
Something As String
SomethingElse As Integer
End Type
Private aUDT as MyUDT
Private Sub EmptyUDT()
Dim bUdt as MyUDT
aUDT = bUdt
End Sub
should do itCode:Erase aUDT
What I mean is that I have an array of UDTs and I want to remove one of the udts from the array
ReDim Employees(UBound(Employees) + 1)
Now after I have filled in the variables for this employee then later he leaves I want to remove this element of the array
Would it be Erase Employees n
where n is the index number of the UDT I want to remove
EDIT: Ok, I tried that and it doesn't work. I'm thinking it would be like removing an element from a list box where you simply do this:
List1.RemoveItem n
Arrays of UDTs are no different to arrays of other variables; you cannot remove an element without having to manually join the tails with code. Most variables can be stored in a Collection which is pretty much like an array; Collections support removale of elements without extra code, unfortunately you cannot store UDTs in Collections. So the solution may be to simulate the UDT in a Class and store instances of the Class in a Collection.
This is the main reason to avoid UDTs except where they must be used. Classes were meant to, among other things, replace the general use of UDTs.
Even if you declare a "public" UDT (i.e. as in one defined in a separare COM DLL) they get turned into a Class of sorts anyway. This allows you to pass them more freely as parameters, add them to Collections, etc. but at the cost of an extra DLL.
In a similar way arrays are a very primitive data structure based on fixed offsets within a blob of memory. That's where things like Collection and Dictionary objects come in.
Or in many cases you can use a fabricated ADO Recordset and avoid the need for a UDT or Class at all, while gaining capabilities such as sorting, filtering, and searching (with optional indexing to improve speed) along with persisting to disk and more.
This is how I handle them:
(TChart and TTemp are the same and both UDT's)
I personally like them over collections. Just treat them like an array and are faster than collections.Code:
Public Function FreeTChartMemory() As Integer
'Free the Unneeded Memory Used by the TChart Structure
'NOTE: Return value = -1 if no structures left, otherwise value
On Error GoTo Error_FreeTChartMemory
'-----------
Dim i As Integer
Dim iActiveChartCount As Integer
Dim TTemp() As TChartInfo '<Temporary Structure
'*******
'STARTUP
'*******
iActiveChartCount = -1
'****
'MAIN
'****
'Transfer the good Charts to the TTemp Array
For i = 0 To UBound(TChart)
If TChart(i).Active Then
iActiveChartCount = i
ReDim Preserve TTemp(iActiveChartCount)
TTemp(iActiveChartCount) = TChart(i)
End If
Next
'Active TChart structures saved, Erase all TChart structures
'To free memory
Erase TChart
'If none left
' a) quick exit
' b) otherwise copy TTemp Arrays to TChart
If iActiveChartCount = -1 Then
FreeTChartMemory = iActiveChartCount 'Return CRITICAL
Exit Function
Else
'Copy the TTemp structure back to the Original TChart structures
For i = 0 To iActiveChartCount
ReDim Preserve TChart(i)
TChart(i) = TTemp(i)
Next
Erase TTemp
End If
FreeTChartMemory = iActiveChartCount 'Return CRITICAL
'******
'WRAPUP
'******
Exit Function
Error_FreeTChartMemory:
With TError
.Type = ERR_CRITICAL
.Src = mstrModule & "FreeTChartMemory"
.Action = MsgAndLog
End With
Call DoError
End Function
i made this little example... have a look jmsrickland
it will generate a udt array list then you can delete which one you want
Code:Private Type UDT
Name As String
End Type
Private ArrUDT() As UDT
Private Sub Command1_Click()
Dim i As Integer
ReDim Preserve ArrUDT(25)
For i = 0 To 25
ArrUDT(i).Name = "Item" & i
List1.AddItem "Item" & i
Next
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim GoBack As Boolean
Delete_IUDT:
ArrUDT(List1.ListIndex).Name = "DeleteMe"
ReArrange_UDT:
For i = 1 To UBound(ArrUDT)
If GoBack = True Then
ArrUDT(i - 1).Name = ArrUDT(i).Name
Else
If ArrUDT(i).Name = "DeleteMe" Then GoBack = True
End If
Next
If GoBack = True Then ReDim Preserve ArrUDT(UBound(ArrUDT) - 1)
RePrint_UDT:
List1.Clear
For i = 0 To UBound(ArrUDT)
List1.AddItem ArrUDT(i).Name
Next
End Sub
Notwithstanding the advice you've been given, you could 'logically' delete an element by adding a flag into the UDT itself.
Then, to delete a given elementCode:Private Type MyUDT
Deleted As Boolean
Name As String
Section As String
YearsService As Integer
EmployeeID As String
End Type
Private Employees(9) As MyUDT
You'd need to check the flag whenever you access an element e.g.Code:Employees(n).Deleted = True
It's a bit wasteful of memory resources since you've got 'inactive' elements in the Array, it does have an advantage that you can 'un-delete' an element later on if you want to. (e.g. someone fired the wrong person). Most useful in 'What if' scenarios.Code:For I = 0 To UBound(Employees)
If Not Employees(I).Deleted Then
'etc
End If
Next I
just wondering if any of the last two examples were any good for you?
my example is exactly what you wanted
Doogle's example is not exactly what you asked for, but would be a bit smarter to use his example (if its for employee fire/hire)
unless you create another udt array for fire employees