Results 1 to 10 of 10

Thread: UDT Question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    UDT Question

    How do I remove a UDT that the program creates?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: UDT Question

    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

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: UDT Question

    I assume you mean 'empty' it(?)
    Code:
    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
    If you have a UDT Array then
    Code:
    Erase aUDT
    should do it
    Last edited by Doogle; Jan 10th, 2013 at 06:20 PM.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: UDT Question

    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
    Last edited by jmsrickland; Jan 10th, 2013 at 07:27 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: UDT Question

    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.

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: UDT Question

    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.

  7. #7
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166

    Re: UDT Question

    This is how I handle them:
    (TChart and TTemp are the same and both UDT's)

    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 personally like them over collections. Just treat them like an array and are faster than collections.

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: UDT Question

    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
    Attached Files Attached Files
    Last edited by Max187Boucher; Jan 10th, 2013 at 11:37 PM.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: UDT Question

    Notwithstanding the advice you've been given, you could 'logically' delete an element by adding a flag into the UDT itself.
    Code:
    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
    Then, to delete a given element
    Code:
    Employees(n).Deleted = True
    You'd need to check the flag whenever you access an element e.g.
    Code:
    For I = 0 To UBound(Employees)
        If Not Employees(I).Deleted Then
            'etc
        End If
    Next I
    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.

  10. #10
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: UDT Question

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width