开发者

C# Passing arrays between class A and Class B

开发者 https://www.devze.com 2023-02-07 13:57 出处:网络
I am trying to get array[i] from class B to my class A. In class B I have: public array getArray() { re开发者_开发技巧turn array[i];

I am trying to get array[i] from class B to my class A.

In class B I have:

public array getArray() {
    re开发者_开发技巧turn array[i];
}

How can I get the full array to make this work?

combobox1.items.add(getArray());

I am trying this, but it does not work. Can you please answer with a code sample that explains two methods in different classes that send and get array[i].


You need to give class A access to the entire array and not just a single value. For example

class B {
  Object[] _array;
  public Object[] GetArray() {
    return _array;
  }
}

Now class A can consume all of the elements and fully populate the combo box

foreach (object element in classBInstance.GetArray()) {
  combobox1.Items.Add(element);
}


Well your example you have i but its not initiated anywhere, you would need to pass it like this:

public array getArray(int i)
{
    return array[i];
}



combobox1.items.add(getArray(5));


Your question is not very clear, so I will try to answer based on what I understood to be your problem.

class A 
{
    string[] array;
    public string[] GetArray() { return array; }
}

then you can write code like this to fill a combobox

A items = new A();
// fill in array in A
comboBox1.DataSource = items.GetArray();

or

foreach(string item in items)
{
    comboBox1.Items.Add( item );
}

I do set lists using the DataSource member all the time, especially when going from enums to strings.

Example:

comboBox1.DataSource = System.Enum.GetNames(typeof(System.DayOfWeek));


You have to iterate through your array:

foreach(var item in GetArray())
{
     combobox1.Items.Add(item);
}


Although other answers are technically working, in my opinion one should never return an internal array directly, as it goes against good practices in object oriented programming. So I would either implement IEnumerable interface in that class or at least return IEnumerator or IEnumerable which would make the returned collection read-only.

Example:

class ClassB {
  int[] array;
  public IEnumerable<int> GetValues() { return array; }
}

class ClassA {
  void DoTheJob(ClassB source) {
    foreach(int item in source.GetValues()) {
      combobox.Items.Add(item);
    }
  }
}

Another clean solution is to add "Do with values" functionality to class B. Then we simply provide delegate. Example:

class ClassB {
  int[] array;
  public void DoWithValues(Action<int> action) {
    foreach(int item in array) action(item);
  }
}

class ClassA {
  ComboBox combobox;
  void DoTheJob(ClassB source) {
    source.DoWithValues(item => combobox.Items.Add(item));
  }
}

The next (third) example shows how to make whole ClassB enumerable with foreach:

static void Main(string[] args) {
  ClassB b = new ClassB();
  foreach(int i in b) Console.Write(i + " ");
}

class ClassB : IEnumerable<int> {

  int[] array;

  public ClassB() {
    array = new int[20];
  }

  public IEnumerator<int> GetEnumerator() {
    return (array as IEnumerable<int>).GetEnumerator();
  }

  IEnumerator IEnumerable.GetEnumerator() {
    return array.GetEnumerator();
  }
}

update: As ja72 shown, this simple technique which just publishes enumerator of internal array, doesn't physically hide the array, as the resulting IEnumerator can be cast back to int[].

update #2: My brother always use Clone() to return a copy of array. It is slowest, but he said it saved his career a few times (he works on a larger project in a team).

0

精彩评论

暂无评论...
验证码 换一张
取 消