0

I'm pretty new to C# and got a small project to extend. The code is a mess and I'm trying to improve it somehow but seem to reach some limits of the language.

Basically I'm reading sensor values from up to four sensors and show some of them in labels. Since the loop is always the same I wanted to iterate instead of rewriting the same code four times with just different GUI elements to store values in. My research showed me that I could put my labels in an array manually but the Visual Studio editor will overwrite this each time I change anything in the GUI. So my approach was to create an array of references to the elements like

ref GroupBox[] gbChannel;

but it's not possible in C#. If I try to assign it directly

gbChannel[0] = gbChannel0;

C# informs me that I have to check if the object is null before doing this. Anyways,

if(gbChannel1!=null) gbChannel[0] = gbChannel0;

leads to the same result.

Is there any convinient way to achieve what I want to do?

datmw
  • 53
  • 1
  • 5
  • `if(gbChannel1!=null) gbChannel[0] = gbChannel0;` - is the `gbChannel1` just a typo or does your actual code look the same? – Michal Hosala Feb 20 '15 at 13:34
  • 6
    No offense, but I think you've reached *the limits of your C# knowledge*, rather than *the limits of the language*. If you post more code, and elaborate on the problems you're having, I'm pretty sure we can be of help – Willem van Rumpt Feb 20 '15 at 13:34
  • I'm guessing you're using WinForms and putting code into the designer generated `InitializeComponent` method. The one which has a comment saying "do not modify the contents of this method with the code editor". – kjbartel Feb 20 '15 at 13:37
  • 2
    Also, use a `List<>`, not an array. – 3Dave Feb 20 '15 at 13:38

4 Answers4

0

I think you have to initialize your array;

GroupBox[] gbChannel = new GroupBox[x];
Galma88
  • 2,398
  • 6
  • 29
  • 50
0

If you are unsure of size, you could have a generic list like:

List<Groupbox> gbChannels = new List<Groupbox>();

and then just add each element using gbGhannels.Add for each label you iterate through

ozidom
  • 159
  • 1
  • 5
0

I would be curious to see more of your implementation code. It looks like you are not initializing your array properly... There are several ways to go about doing this. One has been mentioned by Galma88 if you know the size of your array.

GroupBox[] gbChannel = new GroupBox[x];

Another way would be as follows:

GroupBox[] gbChannel = {gbChannel0, gbChannel1, gbChannel2, gbChannel3};

Most of this depends on the scope of the variables involved. ozidom's post is a good idea to think about too as generics can be very flexible too.

z1n1ly
  • 13
  • 5
0

So my approach was to create an array of references to the elements like

 ref GroupBox[] gbChannel;

GroupBox is a reference type, you you already have an array of references. The ref keyword is only allowed to indicate that an argument to a method should be passed by reference. It is rarely needed, as most functions should be designed to output values rather than mutate inputs.

If I try to assign it directly

gbChannel[0] = gbChannel0; 

C# informs me that I have to check if the object is null before doing this.

That is because you don't have an array yet - you just have a variable that could hold an array. you have to create the array with new:

gbChannel = new GroupBox[4];

or you can use an array initializer:

gbChannel = new [] {gbChannel0, gbChannel1, gbChannel2, gbChannel3};

the Visual Studio editor will overwrite this each time I change anything in the GU

Then you're doing it in the wrong spot. You're probably adding your code to InitializeComponent, which is reserved for the designer to put in the code that coresponds to the UI elements placed there. Add your code to an event handler like Form_Load that the designer won't touch,

Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240