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

  • SEARCH
  • Home
  • 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 9147475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:59:49+00:00 2026-06-17T10:59:49+00:00

Lets say I have some known library to load data from an .xls file

  • 0

Lets say I have some known library to load data from an .xls file and returns a DataTable populated with data from the first sheet in any excel workbook. Also a Log function that prints out messages in the absence of a working debugger.

DataTable dtFoo = null;
DataTable dtBar = null;
DataTable dtChaz = null;

String[] files = new String[]{ "file1.xls", "file2.xls", "file2.xls" };
DataTable[] dts = new DataTable[] { dtFoo, dtBar, dtChaz };

for(int i = 0; i < 3; i++)
{
    dts[i] = SomeLibrary.LoadFromFile(files[i]); //Returns a new DataTable
    Log((dts[i] == null) + " " + dts[i].Rows.Count)
}

Log((dts[0] == null) + " " + (dtFoo == null));
Log((dts[1] == null) + " " + (dtBar == null));
Log((dts[2] == null) + " " + (dtChaz == null));

Log Output:

  False 40
  False 455
  False 34
  False True
  False True
  False True

Clearly I’m missing something important when working with reference type variables that I haven’t been able to figure out. Why are my DataTable variables still null after the loop completes?

  • 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-06-17T10:59:50+00:00Added an answer on June 17, 2026 at 10:59 am

    This line:

    DataTable[] dts = new DataTable[] { dtFoo, dtBar, dtChaz };
    

    … doesn’t alias the variables with the array elements, as you seem to think it does. It just creates a new array, and sets the initial values to the values of dtFoo, dtBar and dtChaz respectively (all of which are null). The array and the variables are entirely separate afterwards.

    If you want to set the variables later, you’ll need:

    dtFoo = dts[0];
    dtBar = dts[1];
    dtChaz = dts[2];
    

    Of course this doesn’t have to be with DataTables. Simple example:

    string x = "initial";
    string[] array = new string[] { x };
    array[0] = "modified";
    
    Console.WriteLine(x); // "initial"
    x = array[0];
    Console.WriteLine(x); // "modified"
    

    Note that this is when you change the value of the array element. If instead you modify the object that the value refers to, that’s a different matter:

    StringBuilder builder = new StringBuilder("123");
    StringBuilder[] array = new StringBuilder[] { builder };
    builder[0].Append("456");
    Console.WriteLine(builder); // 123456
    

    See my article on reference types and value types for more details. Basically you need to differentiate between references and the objects they refer to.

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

Sidebar

Related Questions

Lets say we have a native library that works with data like this: double
I need some help with calculating cumulative distribution. lets say I have data like
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say I have multiple commands like 5000 which updates some column and row
Lets say I have one row with three columns - some buttons on left
Lets say I have a simple 1D array with 10-20 entries. Some will be
Lets say I have if (textbox.Text != null && textbox.Text.Length > someNum) { //some
I'm planning to try contours on some image. Lets say I have three object
Let's say I have the following data.table in R : library(data.table) DT = data.table(x=rep(c(b,a,c),each=3),
We're facing an interesting topic. Lets say we have a special-functions.c file, basically a

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.