Problem accessing array of objects
-
The problem I'm facing is as follows: I have two classes: cBiquad and cFilter. The cBiquad class has five properties that have get/set capabilities. In the cFilter constructor, I have defined an array of cBiquad objects as follows: cBiquad[] arrBiquads = null; for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i] = new cBiquad(); } Now in one of the methods in the cFilter class - SetParameters(), no arguments - I need to set the five properties of the cBiquad objects. However, when I attempt to do the following: for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i].Property1 = Prop1Value; //etc } I receive the following error message: The name 'arrBiquads' does not exist in the class or namespace 'DSP.cFilter' What should I do to get around this?
-
The problem I'm facing is as follows: I have two classes: cBiquad and cFilter. The cBiquad class has five properties that have get/set capabilities. In the cFilter constructor, I have defined an array of cBiquad objects as follows: cBiquad[] arrBiquads = null; for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i] = new cBiquad(); } Now in one of the methods in the cFilter class - SetParameters(), no arguments - I need to set the five properties of the cBiquad objects. However, when I attempt to do the following: for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i].Property1 = Prop1Value; //etc } I receive the following error message: The name 'arrBiquads' does not exist in the class or namespace 'DSP.cFilter' What should I do to get around this?
put this statement
cBiquad[] arrBiquads = null;
outside of the constructor, instead of inside it. If it's inside the constructor it will only be visible to code also in constructor.public cFilter() { arrBiQuads = new cBiQuad[mnBiquads]; for ( i = 0; i < mnBiquads; i++ ) arrBiquads[i] = new cBiquad(); }
Hope it works better now.. -
The problem I'm facing is as follows: I have two classes: cBiquad and cFilter. The cBiquad class has five properties that have get/set capabilities. In the cFilter constructor, I have defined an array of cBiquad objects as follows: cBiquad[] arrBiquads = null; for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i] = new cBiquad(); } Now in one of the methods in the cFilter class - SetParameters(), no arguments - I need to set the five properties of the cBiquad objects. However, when I attempt to do the following: for ( i = 0; i < mnBiquads; i++ ) { arrBiquads[i].Property1 = Prop1Value; //etc } I receive the following error message: The name 'arrBiquads' does not exist in the class or namespace 'DSP.cFilter' What should I do to get around this?
If you define the array of cBiquad objects in the constructor it's only a local variable and no longer accessible as soon as the constructor scope is left. You have to define a class variable for the array. You will also have to initialize your array before you create it's members.
cBiquad[] arrBiquads = new cBiquad[mnBiquads];
for ( i = 0; i < mnBiquads; i++ )
{
arrBiquads[i] = new cBiquad();
}