Issue with Resizing the Array

Hi,

  In one of my method1 i declared an array as element 1.
  the same array i want use as input parameter in another method2.
  I am doing some operations in method 2 for that array like resizing to 2 elements.
  After finishing the operations in method2 then again i am coming to the method1 but my array is still showing as element 1 only.
  The code ls like the following.
class1
{
   private void method1
   {
      var array1 = new String[1] ;
       method2(array1);
    }
    private void method2(String[] array1)
    {
       var time1 = "time";
       var time2 = "time";
       var time3 = "time1";
       array1[array1.count() - 1] = time1;
       if(time1 = time2)
       Array.resize(ref array1,array1.count()+1)
       array1[array1.count() - 1] = time3;
     }  
}

So in the above code after resizing the array1 in method2.
I mean after finishing operations in method2 when it comes to method1 it is showing the length as only 1 i mean array.count() is only 1.

Please tell me the possible solution.
I want both elements.
I think you understand the question.

Please let me know if you don't understand question i will explain clearly.

Thanks in advance.
 
chpkAsked:
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.

dericstoneCommented:
You should add the ref keyword to the definition and call of method2. That defines the argument array1 as something that is expected to change within method2.
class1
{
   private void method1
   {
      var array1 = new String[1] ;
       method2(ref array1);
    } 
    private void method2(ref String[] array1)
    {
       var time1 = "time";
       var time2 = "time";
       var time3 = "time1";
       array1[array1.count() - 1] = time1;
       if(time1 = time2)
       Array.resize(ref array1,array1.count()+1)
       array1[array1.count() - 1] = time3;
     }  
}

Open in new window

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
paulofonsecajrCommented:
Try to force the return value to array1 like this:

class1
{
   private void method1
   {
      var array1 = new String[1] ;
       array1 = method2(array1);
    }
    private String[] method2(String[] array1)
    {
       var time1 = "time";
       var time2 = "time";
       var time3 = "time1";
       array1[array1.count() - 1] = time1;
       if(time1 = time2)
       Array.resize(ref array1,array1.count()+1)
       array1[array1.count() - 1] = time3;
       return array1;
     }  
}

It seems to work for you.
0
dericstoneCommented:
Here's the running project code. This assumes a Form has a TextBox control called textBox1. Hope it helps.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SpecialClass
{
  public partial class Form1 : Form
  {
    class1 myclass = new class1();

    public Form1()
    {
      InitializeComponent();

      myclass.method1();
      foreach (string s in myclass.array1)
        this.textBox1.Text += s;
    }
  }
  public class class1
  {
    public string[] array1 = new String[1];

    public void method1()
    {
      method2(ref array1);
    }
    private void method2(ref String[] array1)
    {
      var time1 = "time";
      var time2 = "time";
      var time3 = "time1";
      array1[array1.Length - 1] = time1;
      if (time1 == time2)
        Array.Resize<string>(ref array1, array1.Length + 1);
      array1[array1.Length - 1] = time3;
    }
  }
}

Open in new window

0
lloydswayzeCommented:
If you don't add "ref", as already mentioned, you only modify a copy of it not the real array. The copy gets destroyed when returning.
0
chpkAuthor Commented:
Thank you so much guys.

This is why i really like Experts - Exchanges.
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
.NET Programming

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.