943,752 Members | Top Members by Rank

Ad:
Jul 30th, 2009
0

Some Explanation with redim statement

Expand Post »
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.
vb Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 7
Solved Threads: 1
Posting Pro in Training
firoz.raj is offline Offline
415 posts
since Dec 2008
Jul 30th, 2009
0

Re: Some Explanation with redim statement

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
VB Syntax (Toggle Plain Text)
  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.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Aug 1st, 2009
0

Re: Some Explanation with redim statement

'
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
Reputation Points: 7
Solved Threads: 1
Posting Pro in Training
firoz.raj is offline Offline
415 posts
since Dec 2008
Aug 18th, 2009
0

Re: Some Explanation with redim statement

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...^_^...
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
PoisonedHeart is offline Offline
57 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how toconvert vb file into exe
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Enter Key use in my Project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC