Results 1 to 8 of 8

Thread: Array value problem

  1. #1

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Array value problem

    Hi
    I am trying to extract the values from a table and populate a virtual table, which is working fine, however I then want to take those values and populate an array, unfortunately I keep getting the values 1 and 3 for some reason, instead of the values in the actual table,
    I would be grateful for any advice.

    _product.versionid = _productversionid ' get the select record version id
    _dsDataSet = _product.ProductSQL(Cls_Products.ProductDetails.ProductChildFormulas) 'select the child formula id's
    Dim dt As New DataTable("formulatable") 'create a virtual table
    dt.Columns.Add("versionchildid") 'create a virtual column to hold child formula id's
    For Each dr As DataRow In _dsDataSet.Tables("product").Rows 'select all the child id's
    dt.Rows.Add(New Object() {dr("childproductversionid")}) 'and add them to the table
    Next 'move to the next record
    If dt.Rows.Count > 0 Then 'if any rows are returned, we can create an array to query the formula version table
    Dim dataList As New ArrayList
    For Each dr As DataRow In dt.Rows 'for each row in table add the value to the array

    dataList.Add(dr.Item(0))
    MsgBox(dataList.Add(dr.Item(0)).ToString)
    Next

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Array value problem

    Hi,

    I,m wondering why

    MsgBox(dataList.Add(dr.Item(0)).ToString)

    works???

    If you must use MsgBox then shouldn't it be

    MsgBox(dr.Item(0).ToString) ??

    Have you put a breakpoint on

    dataList.Add(dr.Item(0))

    to check the various values at that point?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array value problem

    ArrayList.Add returns the index at which the new item was added. This code:
    VB Code:
    1. dataList.Add(dr.Item(0))
    2. MsgBox(dataList.Add(dr.Item(0)).ToString)
    is adding the same value to the ArrayList twice. You are calling MsgBox every second time so you will get the indexes 1, 3, 5, 7, etc. May I suggest reading the help topics for properties, methods, etc. rather than assuming they work a certain way, particularly if they appear to not be working as you expect. From the help:
    Return Value
    The ArrayList index at which the value has been added.
    Could have saved yourself some time and heartache.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: Array value problem

    yeah you are right.
    I was only using the message box, so I could see what the return values were..

    I have got the table and array working now, like so....

    Dim dataList As New ArrayList 'create an array to hold values
    For Each dr As DataRow In dt.Rows 'get the value from each row in the table
    dataList.Add(dr.Item(0)) 'add the value to array
    Next

    But I still want to check I have the correct values in the array, before I go onto the next bit! is there a way of checking this?
    And do I really need to create a table in the first place? could I not just pass the values from the dataset into an array?? (somehow)

    Thanks

  5. #5

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: Array value problem

    Ok, found by inserting a break point.
    Bit new to all this, used to ASP but now trying out VB.Net for past couple months.

    I notice taxes that you like Virginia!
    I was there last year, James Town, York Town, Richmond, Blue Ridge Mountains etc etc, had really good time.

    Anyway

    Thanks for help.
    Last edited by soluga; Jan 24th, 2006 at 07:10 AM.

  6. #6

    Thread Starter
    Addicted Member soluga's Avatar
    Join Date
    Dec 2005
    Location
    Manchester (Uk)
    Posts
    211

    Re: Array value problem

    Hi
    New Problem now..

    When I call my class, I want to loop through the array, calling a sql select for each value like so....

    For Each childproductversionid As String In list
    _sqlString = "Select * From productversion " & _
    " Where productversionid = '" & childproductversionid & "'"
    Next

    But I am only getting the final record!
    Would be grateful for any advice.
    Thanks

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Array value problem

    Quote Originally Posted by soluga
    Ok, found by inserting a break point.
    Bit new to all this, used to ASP but now trying out VB.Net for past couple months.

    I notice taxes that you like Virginia!
    I was there last year, James Town, York Town, Richmond, Blue Ridge Mountains etc etc, had really good time.

    Anyway

    Thanks for help.
    I've just come back after spending a month with family in North Western Virginia (Culpeper area). Well within sight of the Blue Ridge Mountains. Out in the wilds, no broadband
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array value problem

    Quote Originally Posted by soluga
    Hi
    New Problem now..

    When I call my class, I want to loop through the array, calling a sql select for each value like so....

    For Each childproductversionid As String In list
    _sqlString = "Select * From productversion " & _
    " Where productversionid = '" & childproductversionid & "'"
    Next

    But I am only getting the final record!
    Would be grateful for any advice.
    Thanks
    This is a new question and should have been asked in a new thread.

    What exactly do you expect to happen? Think about what you want to happen and what exactly that code inside the For loop does and I think you'll see that they are not the same thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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