Some Explanation with redim statement

Thread Solved

Join Date: Dec 2008
Posts: 292
Reputation: firoz.raj is an unknown quantity at this point 
Solved Threads: 1
firoz.raj firoz.raj is offline Offline
Posting Whiz in Training

Some Explanation with redim statement

 
0
  #1
Jul 30th, 2009
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.
  1. Public Function LoadData() As Boolean
  2. Dim codeString As String
  3. On Error GoTo LoadData_Error
  4.  
  5. Dim Obj As IProducts '// The classinterface
  6. Dim rs As ADODB.Recordset '// Recordset to hold the class.Getall call
  7. Dim inxCode As Integer '// Index / offset for recordset id (code) field
  8. Dim i As Integer '// Counter for the array
  9. Dim strItem As String '// String we add to combo box
  10. Dim J As Integer '// Used for iterating columns of the recordset
  11.  
  12. '// Assume function fails
  13. LoadData = False
  14. i = 0
  15. Select Case ClassName
  16.  
  17. Case ValidClassNames.cnProducts
  18. Set Obj = New Products
  19. inxCode = ProductFields.fldProductID
  20. Obj.FilterStr = mFilter
  21.  
  22. Case Else
  23. Err.Raise Number:=10000, description:="Programming error. Unknown class type."
  24. Exit Function
  25. End Select
  26.  
  27. cmbCodes.Clear
  28. Obj.QueryType = m_queryType
  29. Set rs = Obj.GetAll
  30.  
  31. If blnDataReturned(rs) Then
  32. If IncludeBlank Then
  33. [B][COLOR="Red"]ReDim mClassCodes(0)[/COLOR][/B]
  34. cmbCodes.AddItem BLANK
  35. mClassCodes(i) = CStr(BLANK_ID)
  36. i = i + 1
  37. End If
  38. mDontFireClickEvent = True
  39. Do Until rs.EOF
  40. strItem = ""
  41. strItem = rs.Fields(1).Value
  42. cmbCodes.AddItem strItem
  43. ReDim Preserve mClassCodes(i)
  44. codeString = IIf(IsNull(rs.Fields(inxCode).Value), NULL_STRING, rs.Fields(inxCode).Value)
  45. codeString = codeString & ";" & m_queryType
  46. mClassCodes(i) = codeString
  47. i = i + 1
  48. rs.MoveNext
  49. Loop
  50.  
  51. If cmbCodes.ListCount > 0 Then cmbCodes.ListIndex = 0
  52. End If
  53. mDontFireClickEvent = False
  54. Call CloseRecordset(rs)
  55. 'Set rs = Nothing
  56. '// Now, if we have a design time default, set it!
  57. If DefaultCode <> NO_DEFAULT Then
  58. Id = DefaultCode
  59. End If
  60.  
  61. '// All's well - return True
  62. LoadData = True
  63.  
  64. LoadData_Done:
  65. Exit Function
  66.  
  67. LoadData_Error:
  68. Call Process_Error(MODULE_NAME, "LoadData")
  69. Resume LoadData_Done
  70.  
  71. End Function
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Some Explanation with redim statement

 
0
  #2
Jul 30th, 2009
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
  1. '
  2. ' Declare Dynamic Array
  3. '
  4. Dim AnArray() As Integer
  5.  
  6. '
  7. ' Now allocate the size
  8. '
  9. ReDim AnArray(2) As Integer
  10.  
  11. '
  12. 'Now An Array has bound 0 to 2
  13. '
  14.  
  15. AnArray(0) = 100
  16. AnArray(1) = 300
  17. AnArray(2) = 400
  18.  
  19. '
  20. ' Change the size
  21. '
  22.  
  23. ReDim AnArray(10) As Integer
  24.  
  25. '
  26. ' Now AnArray size is changed but all the values in that array are lost
  27. '
  28. ' To preserve the existing values use Preserve keyword
  29. '
  30. ' Change the previous code as
  31.  
  32. 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.
Last edited by selvaganapathy; Jul 30th, 2009 at 10:34 am.
KSG
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 292
Reputation: firoz.raj is an unknown quantity at this point 
Solved Threads: 1
firoz.raj firoz.raj is offline Offline
Posting Whiz in Training

Re: Some Explanation with redim statement

 
0
  #3
Aug 1st, 2009
'
Still doubt kindly explain this also.
If blnDataReturned(rs) Then        If IncludeBlank Then            ReDim mClassCodes(0)            cmbCodes.AddItem BLANK            mClassCodes(i) = CStr(BLANK_ID)            i = i + 1        End If
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 12
Reputation: PoisonedHeart is an unknown quantity at this point 
Solved Threads: 0
PoisonedHeart PoisonedHeart is offline Offline
Newbie Poster

Re: Some Explanation with redim statement

 
0
  #4
Aug 18th, 2009
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...^_^...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC