can anybody explain me.the use of redim statement and preserve
keyword over here.kindly let me know the idea .any help would be
Highly appreciated.

Public Function LoadData() As Boolean
Dim codeString As String
    On Error GoTo LoadData_Error

    Dim Obj As IProducts                '// The classinterface
    Dim rs As ADODB.Recordset           '// Recordset to hold the class.Getall call
    Dim inxCode As Integer              '// Index / offset for recordset id (code) field
    Dim i As Integer                    '// Counter for the array
    Dim strItem As String               '// String we add to combo box
    Dim J As Integer                    '// Used for iterating columns of the recordset

    '// Assume function fails
    LoadData = False
    i = 0
    Select Case ClassName

        Case ValidClassNames.cnProducts
            Set Obj = New Products
            inxCode = ProductFields.fldProductID
            Obj.FilterStr = mFilter

        Case Else
            Err.Raise Number:=10000, description:="Programming error. Unknown class type."
            Exit Function
    End Select

    cmbCodes.Clear
    Obj.QueryType = m_queryType
    Set rs = Obj.GetAll

    If blnDataReturned(rs) Then
        If IncludeBlank Then
            [B]ReDim mClassCodes(0)[/B]
            cmbCodes.AddItem BLANK
            mClassCodes(i) = CStr(BLANK_ID)
            i = i + 1
        End If
        mDontFireClickEvent = True
        Do Until rs.EOF
            strItem = ""
            strItem = rs.Fields(1).Value
            cmbCodes.AddItem strItem
            ReDim Preserve mClassCodes(i)
            codeString = IIf(IsNull(rs.Fields(inxCode).Value), NULL_STRING, rs.Fields(inxCode).Value)
            codeString = codeString & ";" & m_queryType
            mClassCodes(i) = codeString
            i = i + 1
            rs.MoveNext
        Loop
        
        If cmbCodes.ListCount > 0 Then cmbCodes.ListIndex = 0
    End If
    mDontFireClickEvent = False
    Call CloseRecordset(rs)
    'Set rs = Nothing
    '// Now, if we have a design time default, set it!
    If DefaultCode <> NO_DEFAULT Then
        Id = DefaultCode
    End If

    '// All's well - return True
    LoadData = True

LoadData_Done:
    Exit Function

LoadData_Error:
    Call Process_Error(MODULE_NAME, "LoadData")
    Resume LoadData_Done

End Function

Recommended Answers

All 3 Replies

Hi,

ReDim statement is used to change the size of a dynamic array in procedure level.

Preserve keyword is used to preserve the data in an existing array when you change the size.
Ex

'
' Declare Dynamic Array
'
Dim AnArray() As Integer

'
' Now allocate the size
'
ReDim AnArray(2) As Integer

'
'Now An Array has bound 0 to 2
'

AnArray(0) = 100
AnArray(1) = 300
AnArray(2) = 400

'
'  Change the size
'

ReDim AnArray(10) As Integer

'
' Now AnArray size is changed but all the values in that array are lost
'
' To preserve the existing values use Preserve keyword
'
' Change the previous code as

ReDim Preserve AnArray(10) As Integer

In your code if IncludeBlank flag is set, you are change the size of mClassCodes array to 1 ( 0 to 0).

For the other data of Class Codes, you are changing the size one by one with preserving the existing values.

'
Still doubt kindly explain this also.

If blnDataReturned(rs) Then        If IncludeBlank Then            [b]ReDim mClassCodes(0)[/b]            cmbCodes.AddItem BLANK            mClassCodes(i) = CStr(BLANK_ID)            i = i + 1        End If

To: selvaganapathy

Is it ok, if i will just use the

ReDim Preserve AnArray(10) As Integer

does this statement will automatically change the size of the array? so that I will not have to type in the :

ReDim AnArray(10) As Integer

above it? Thanks in advance...^_^...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.