URGENT : Exaplanation of Type

Posted on 2006-05-26
Medium Priority
Last Modified: 2010-04-30
Hi all

I am looking for a detailed explanation of the "Type" - I need to know and understand more about the actual type declaration ... how long it lasts - when and how it shold be destoyed - what effects it has on memory and so on ...  for example I use the following type in my program and want to understand how it survives in memory - can it cause memory leaks and things such as  - how it should be properly declared - how shold it be properly destroyed so dont leave it in memory and so on ... perhaps what its disadvantages are, what the advantages are ... basically everything I should know ...

Public Type EmployeeInfo
    eName As String
    eLogIn As String
    eNumber As Long
    eEmailAddress As String
    eAuthority As Long
    eHourly As Boolean
    eEmployeeType As Long
    eFolder As String
End Type

Links to articles appreciated and well structured answers will be appreicated more ...
Question by:dwe0608
  • 4
  • 3
  • 2
  • +1
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 16774124
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 16774134

Author Comment

ID: 16774136
hi - MT for the response ... nice article, but it doesnt tell me how the memory is handled nor how or when the UDT is destroyed ... this is part of the problem I am experiencing ... every article I have found tells me how to use them, but not how the UDT is destroyed or is to be destroyed ....

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 29

Expert Comment

ID: 16775054
to destroy the UDT just call ZeroMemory API which is defined as RtlZeroMemory. This will write zeros to the memory location of the UDT structure.

Option Explicit

' 4 byte structure
Private Type MyUDT
    b1  As Byte
    b2  As Byte
    b3  As Byte
    b4  As Byte
End Type

Private Declare Sub RtlZeroMemory Lib "Kernel32" ( _
        ByRef Destination As Any, _
        ByVal Length As Long)
Dim m_udt As MyUDT

Private Sub Form_Load()
    With m_udt
        .b1 = 2
        .b2 = 4
        .b3 = 6
        .b4 = 8
    End With
    'value is 2
    Debug.Print m_udt.b1
    'Write zeros to structure
    RtlZeroMemory m_udt, Len(m_udt)
    'value is 0
    Debug.Print m_udt.b1
End Sub
LVL 13

Expert Comment

ID: 16776654

i think you want to take a look at this:


Author Comment

ID: 16777906
Mark_freeSoftware, the article is excellent .... but just one note, it talks about the way a UDT is in memory, how we can interact with it, but it STILL DOESNT tell me how the UDT should be destroyed ... we can see that the UDT's are held in memory, and are really long pointers - to either a long value or a long pointer to the memory which holds a string... the long pointer is valid until it goes out of scope for whatever the reason ... when it goes out of scope, as I understand it, the long pointer is destroyed ... if the pointer is a numerical value thats ok ... but if its a string - the string memory will be left behind until windows decides it wants to reclaim the memory ....

For me to assist windows to reclaim the memory, should I :

   1.  Set my UDT to nothing (ie Set UDT = nothing) or
   2. RtlZeroMemory m_udt, Len(m_udt)

Are they the same ?
LVL 13

Accepted Solution

Mark_FreeSoftware earned 1500 total points
ID: 16778914

1.  Set my UDT to nothing (ie Set UDT = nothing)
this removes the reference to it.
the memory is not filled with zero's, but it can be overwritten with anything

it is "reclaimed" but not cleared

2. RtlZeroMemory m_udt, Len(m_udt)
clears the contents of the udt, but as far as i know it does not destroy the reference to it (so the memory is still reserved)

see this example of rtlzeromem wher first is an string with XXXX printed, and than an empty string, but the string is still there!


Author Comment

ID: 16779102

1.  Set my UDT to nothing and it throws an error
2. check out the following code (based on the url you sent me to)

Option Explicit

Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)

Private Type UDT
 eNumber As Long
 eString As String
End Type

Private Sub Form_Load()

    Dim newUDT As UDT
    MsgBox "WE're here"
    newUDT.eNumber = 1000
    newUDT.eString = String(25, "X")

    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim TestString As String
    'Make sure Visual Basic repaint the form automatically
    Me.AutoRedraw = True
    'Create a buffer string
    'TestString = String(25, "X")
    Me.Print "This is our initial string : " + newUDT.eString
    Me.Print "This is our initial number : " & newUDT.eNumber
    'Fill the buffer-string with A's
    FillMemory ByVal newUDT.eString, Len(newUDT.eString), Asc("A")
    Me.Print "This is our string after FillMemory: " + newUDT.eString
    'Empty the string
    ZeroMemory ByVal newUDT.eString, Len(newUDT.eString)
    Me.Print "This is our string after ZeroMemory: " + newUDT.eString
    ' take the string
    newUDT.eString = "x"
    newUDT.eNumber = 0
    Me.Print "This is our string after making it a single char value: " + newUDT.eString
    Me.Print "This is our number again number : " & newUDT.eNumber

    ' set it to nothing
    'Set newUDT = Nothing ' throws an error
    'Me.Print "This is our string after Setting it to nothing: " + newUDT.eString

End Sub

have a play and let me know what you think ...  
LVL 13

Expert Comment

ID: 16779122

if you want to use it like this, it is exactly the same as with normal variables,

after the end sub the variable is destroyed

LVL 13

Expert Comment

ID: 16779133

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question