[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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() + "]");


    }

0
cossy74
Asked:
cossy74
  • 2
1 Solution
 
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
 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now