chpk
asked on
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
}
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.
ASKER
Thank you so much guys.
This is why i really like Experts - Exchanges.
This is why i really like Experts - Exchanges.
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.