CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Arrays in VB

  1. #1
    Join Date
    Jul 2000
    Posts
    67

    Arrays in VB

    Hello!

    I am trying to print some values from a recordset, and the way i do it is to read them into a 2D array first, and then trying to print variables from the array in a loop.
    I have pasted som of my code below, my question is: Why do "print #1, sData" print values, and why do not "Print #1, DataArray(0, C)" print anything but blank lines? Have i done anything wrong?

    Dim rsAddresses As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim sData As String
    Dim DataArray() As String
    ReDim DataArray(1, 0) As String
    Do Until rsAddresses.EOF
    sData = ""
    For Each fld In rsAddresses.Fields
    sData = sData & fld.Value & ", "
    DataArray(0, C) = fld.Name
    DataArray(1, C) = fld.Value

    ReDim Preserve DataArray(1, C + 1) As String
    C = C + 1
    Print #1, DataArray(0, C)
    Print #1, DataArray(1, C)
    Next
    rsAddresses.MoveNext
    Print #1, sData; 'prints address
    Loop

    Thankful for all suggestions.


  2. #2
    Join Date
    Aug 2000
    Location
    England
    Posts
    185

    Re: Arrays in VB

    Hello,

    I'm not 100% certain, but I think redim might destroy the current contents of the array...

    Andrew


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Arrays in VB

    First of all as Andrew_Fryer said Redim will destroy the value.

    Secondly:
    ReDim DataArray(1, 0) As String

    here you redim array 1 element in the first dimension an 0 elemenbts in the second dimension


    Do Until rsAddresses.EOF
    sData = ""
    For Each fld In rsAddresses.Fields
    sData = sData & fld.Value & ", "

    what the C element in the second dimension
    I think your data will not go anywhere

    DataArray(0, C) = fld.Name
    DataArray(1, C) = fld.Value


    Why do you want to throw rs data to the array and then to string and then print it? I think it is reasonable to print the recordset value directly

    Do while not ts.EOF
    Print #1, rs!Name & " " & rs!Value
    Loop

    Also be careful with the filed named 'Value', because value is reserved word in VB. Try it several different ways
    rs!Value
    rs.("Value").Value
    rs.(Field(2)).Value



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: Arrays in VB

    Note 1: Redim will destroy the values
    Note 2: Redim Preserve will not destroy the values
    Note 3: As Iouri says, using the recordest's fields directly, rather than putting to fields is more efficient
    Note 1: U r not getting correct results not because of these but
    u put value to C'th element, increment C & the view C'th element

    Eg: consider when, C = 0

    DataArray(1, C) = fld.Value
    ' This makes DataArray(1, 0) = Value1

    C = C + 1
    ' This makes C =1

    'Try to Print DataArray(1, 1)
    Print #1, DataArray(0, C)
    ' result is nothing getting printed
    ' if this is the case use the following
    Print #1, DataArray(0, C - 1)










    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

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

Featured