Parameter Passing ByRef

Hi,

I have a function
GetCVSLines(string FileName, ArrayList Header, ArrayList Lines)

I can process the information and add them to the Arraylists and then when i try to referenece the Arraylist after the function has returned i do not have the same values in the lists.

What am i doing wrong.

Heres the code;

......
  {
      ArrayList Header = null;
      ArrayList Lines = null;

      GetCVSLines(Path_Copy, Header, Lines);

      DBG.DBG("Header Count = [" + Header.Count.ToString() + "]");  
      DBG.DBG("Line Count = [" + Lines.Count.ToString() + "]");
   }

    private void GetCVSLines(string FileName, ArrayList Header, ArrayList Lines)
    {
      const string fnct  = "GetCVSLines";
      DBG.DBG("function: " + fnct);
      //----------------------------------------
      // Process Steps
      //----------------------------------------
      //
      // 1. Clear ArrayLists
      // 2. Create Array List
      // 3. Read in All Lines
      //
      //----------------------------------------

      // 1. Clear ArrayLists
      Header = null;
      Lines = null;

      // 2. Create Array List
      Header = new ArrayList();
      Lines = new ArrayList();

     
      // 3. Read in All Lines
      System.IO.StreamReader dbfFile;      
      try
      {
        dbfFile = new System.IO.StreamReader(FileName);
      }
      catch (Exception ex)
      {
        DBG.DBG("-------------------------------------");
        DBG.DBG(" Error creating streamreader.");
        DBG.DBG("-------------------------------------");
        DBG.DBG(ex.Message);
        DBG.DBG("-------------------------------------");
        return;
      }
     
      string sLine;
      int iCounter;
      iCounter = 0;
      do
      {
        sLine = dbfFile.ReadLine();
        iCounter++;                        
        if(sLine != null)
        {
          if(iCounter < 6)
          {
            Header.Add(sLine);
          }
          else
          {
            Lines.Add(sLine);
          }
        }
      }while(sLine !=null);

      dbfFile.Close();

      DBG.DBG("Header Count = [" + Header.Count.ToString() + "]");  
      DBG.DBG("Line Count = [" + Lines.Count.ToString() + "]");


    }

cossy74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AaronReamsCommented:
// Change these...
ArrayList Header = new ArrayList();
ArrayList Lines = new ArrayList();
GetCVSLines(Path_Copy,ref Header,ref Lines);


private void GetCVSLines(string FileName,ref ArrayList Header,ref ArrayList Lines)
{
...
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AaronReamsCommented:
Also you don't need the following in the function if you pass in the ArrayLists by reference because you already allocated the objects prior to calling the function....

      // 1. Clear ArrayLists
      Header = null;
      Lines = null;

      // 2. Create Array List
      Header = new ArrayList();
      Lines = new ArrayList();

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.