C# Passing arrays between class A and Class B

11,254

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);
}
Share:
11,254
Ivan
Author by

Ivan

Updated on June 04, 2022

Comments

  • Ivan
    Ivan almost 2 years

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

    In class B I have:

    public array getArray() {
        return 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].

  • Al Kepp
    Al Kepp about 13 years
    This is dirty. In object oriented world, you should never return your internal array directly. -1
  • Ivan
    Ivan about 13 years
    Well if i understood correctly foreach(var item in ClassB.GetArray()) { combobox1.Items.Add(item); } with reference to classb?
  • JaredPar
    JaredPar about 13 years
    @Al the user appears to be fairly new to the language and hence I'm opting for the most straight forward solution.
  • Ivan
    Ivan about 13 years
    Can you show a sample of IEnumerable interface because i googled but never got a hit the gives a straight answer
  • Al Kepp
    Al Kepp about 13 years
    Ivan, if you have internal array, you can return it as IEnumerable. Look to method GetValues() in my first example. That's easiest way. I am going to show another possibility (making whole class B enumerable) in the third example.
  • Al Kepp
    Al Kepp about 13 years
    The same as above: This is dirty. In object oriented world, you should never return your internal array directly. -1
  • Al Kepp
    Al Kepp about 13 years
    The same as above: This is dirty. In object oriented world, you should never return your internal array directly. -1
  • Al Kepp
    Al Kepp about 13 years
    The same as above: This is dirty. In object oriented world, you should never return your internal array directly. -1
  • John Alexiou
    John Alexiou about 13 years
    @AlKepp you do when you care about speed, and not have to copy values 10,000 times a second. Not everyone is building Banking code, that needs this level of security. Spare me the OOP idealism, and answer realistically the question asked.
  • John Alexiou
    John Alexiou about 13 years
    -1. Your code assumes class A is a wrapper (can have only one collection/array that it represents) or it returns the array (masked as IEnumarable<> which can still corrupt the original array and is against OOP principles (per your comments). You can always do int[] list = (int[])GetValues()
  • knight0323
    knight0323 about 13 years
    Actually the most straight forward code would be a property: public Object[] MyArray { get; private set; } in class B.
  • JaredPar
    JaredPar about 13 years
    @knight0323 I'd avoid that because it's using a more advanced language feature.
  • knight0323
    knight0323 about 13 years
    Without knowing more about Ivan's code this sample smells of needless complexity.
  • Al Kepp
    Al Kepp about 13 years
    @ja72: My code doesn't assume that there is just one array in the class. Ivan just asked me to show how to make the whole class enumerable, and I showed the simplest way. Your other complaint is much more interesting. Yes, this is a classic question: If the provided enumerator can be cast back to array type, is it correct or not? In fact inheritance should not be used as a protection technique. If you want to physically hide the fact that it can be cast back, I can provide another example with yield command.
  • Al Kepp
    Al Kepp about 13 years
    OK, I think everyone here has some piece of truth.
  • John Alexiou
    John Alexiou about 13 years
    @AlKepp - The yield is an acceptable solution, and I agree that inheritance should not be used as a protection technique, and you downvoted my answer with the excuse the pure OOP protects better (which is baloney actually). Please reconsider your comments and votes and be more consistent.
  • Al Kepp
    Al Kepp about 13 years
    @ja72: I thank you for your valuable comment. But still I think my solution is a bit better than yours. Although technically both are the same "insecure", my one transparently says "It's IEnumerable, so consider it as read-only." And I also add a note from my brother: He says that he always do Clone() on arrays, and that it saved his career a few times already. :-)