Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:45:51+00:00 2026-05-25T23:45:51+00:00

Can anyone explain exactly why Method 1 in the following code does not alter

  • 0

Can anyone explain exactly why Method 1 in the following code does not alter the DataTable where the other 2 methods do?

It’s obviously some kind of referencing issue, but why exactly?

CODE AT IDEONE HERE

Imports System
Imports System.Data

Public Class Test

        Public Shared Sub Main()
                'Build Table
                Dim dt as New DataTable
                dt.Columns.Add("ID",GetType(String))
                dt.Columns.Add("Name",GetType(String))

                'Populate Table
                Dim dr as DataRow
                dr = dt.NewRow()
                dr("ID")="Man" : dr("Name") = "mike" : dt.Rows.Add(dr)
                dr = dt.NewRow()
                dr("ID")="Man" : dr("Name") = "ian" : dt.Rows.Add(dr)
                dr = dt.NewRow()
                dr("ID")="Man" : dr("Name") = "rob" : dt.Rows.Add(dr)
                dr = dt.NewRow()
                dr("ID")="Woman" : dr("Name") = "ann" : dt.Rows.Add(dr)
                dr = dt.NewRow()
                dr("ID")="Woman" : dr("Name") = "sam" : dt.Rows.Add(dr)

                output(dt)      'Output Table

                Dim drFilters() as DataRow = dt.Select("ID='Man'")      'Select all Man

                'Method 1 does not change dt
                'dr = dt.NewRow()
                'dr("ID")="cowman" : dr("Name")="bugle"
                'drFilters(1)=dr

                'Method 2 does change dt
                dr = drFilters(1)
                dr("ID")="cowman" : dr("Name")="bugle"

                'Method 3 does change dt
                'drFilters(1)("ID")="cowman" : drFilters(1)("Name")="bugle"

                output(dt)    'Output final table
        End Sub

        Public Shared Sub output(dt as DataTable)
                for each dr as DataRow in dt.Rows
                        Console.WriteLine(dr("ID") + vbTab + dr("Name"))
                Next
                Console.WriteLine("")
        End Sub

End Class

Apologies for the stupid test data 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:45:52+00:00Added an answer on May 25, 2026 at 11:45 pm

    When you call drFilters(1)=dr, you have replaced the DataRow reference in the filter collection, which is a separate collection from the row references in the DataTable itself. dt.Rows is a list of references to DataRow objects, as is drFilters(), but changing an entry in one list of references does not in any way affect the other list of references.

    dt.Rows
    0 => dt.Rows(0)
    1 => dt.Rows(1)
    2 => dt.Rows(2)
    3 => dt.Rows(3)
    4 => dt.Rows(4)
    
    drFilter()
    0 => dt.Rows(0)
    1 => dt.Rows(1)
    2 => dt.Rows(2)
    

    After the call to drFilters(1)=dr, the collections look like this

    dt.Rows
    0 => dt.Rows(0)
    1 => dt.Rows(1)
    2 => dt.Rows(2)
    3 => dt.Rows(3)
    4 => dt.Rows(4)
    
    drFilter()
    0 => dt.Rows(0)
    1 => dr
    2 => dt.Rows(2)
    

    Note that dt.Rows is left unchanged. However, when you retrieve the object drFilters(1) and make changes to that, you have a reference directly to dt.Rows(1), so changes to its properties are reflected in the output of dt.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone please explain exactly how the following code works, line by line. I'm
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
can anyone explain me these codes what are they doing exactly? I could not
Can anyone explain why I get the following errors when compiling the code shown
Can anyone explain, what exactly happens in the following line? java.lang.System.out.print(string + i); It
Can anyone explain what the target oldconfig does exactly in the Linux kernel makefile?
Can anyone explain exactly how the Strategy Pattern relates to Inversion of Control?
Can anyone explain the difference between the types mentioned above and some sample usage
Can anyone explain what the jquery documentation is exactly referring to with this statement:
Can anyone explain me briefly what exactly is component programming in .NET and why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.