Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 22, 2016 Access vialotion | ||||
---|---|---|---|---|
| ||||
I have an array of buttons: class Window : MainWindow{ private Button[4] bArr; this(){ Button btn_1 = new Button(); Button btn_2 = new Button(); Button btn_3 = new Button(); Button btn_4 = new Button(); Button[4] bArr = [btn_1,btn_2,btn_3,btn_4]; } private void letButtonsFlash(){ for(int i = 0; i < 4; i++){ writeln(this.bArr[i].getName()); } } } i don't understand why i get an 'Access Violation' - Error in the 'for'-loop? |
June 22, 2016 Re: Access vialotion | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Wednesday, 22 June 2016 at 12:45:29 UTC, TheDGuy wrote:
> I have an array of buttons:
>
> class Window : MainWindow{
> private Button[4] bArr;
> this(){
> Button btn_1 = new Button();
> Button btn_2 = new Button();
> Button btn_3 = new Button();
> Button btn_4 = new Button();
> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
> }
> private void letButtonsFlash(){
> for(int i = 0; i < 4; i++){
> writeln(this.bArr[i].getName());
> }
> }
> }
>
> i don't understand why i get an 'Access Violation' - Error in the 'for'-loop?
Okay, i solved it, instead of:
Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
this:
bArr = [btn_1,btn_2,btn_3,btn_4];
|
June 22, 2016 Re: Access vialotion | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Wednesday, 22 June 2016 at 12:47:31 UTC, TheDGuy wrote:
> On Wednesday, 22 June 2016 at 12:45:29 UTC, TheDGuy wrote:
>> I have an array of buttons:
>>
>> class Window : MainWindow{
>> private Button[4] bArr;
>> this(){
>> Button btn_1 = new Button();
>> Button btn_2 = new Button();
>> Button btn_3 = new Button();
>> Button btn_4 = new Button();
>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>> }
>> private void letButtonsFlash(){
>> for(int i = 0; i < 4; i++){
>> writeln(this.bArr[i].getName());
>> }
>> }
>> }
>>
>> i don't understand why i get an 'Access Violation' - Error in the 'for'-loop?
>
> Okay, i solved it, instead of:
> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>
> this:
> bArr = [btn_1,btn_2,btn_3,btn_4];
I would expect DMD to output a warning here, because of the shadowing...but after 3 verifications, nothing. Dscanner does.
|
June 22, 2016 Re: Access vialotion | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheDGuy | On Wednesday, 22 June 2016 at 12:47:31 UTC, TheDGuy wrote:
> On Wednesday, 22 June 2016 at 12:45:29 UTC, TheDGuy wrote:
>> I have an array of buttons:
>>
>> class Window : MainWindow{
>> private Button[4] bArr;
>> this(){
>> Button btn_1 = new Button();
>> Button btn_2 = new Button();
>> Button btn_3 = new Button();
>> Button btn_4 = new Button();
>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>> }
>> private void letButtonsFlash(){
>> for(int i = 0; i < 4; i++){
>> writeln(this.bArr[i].getName());
>> }
>> }
>> }
>>
>> i don't understand why i get an 'Access Violation' - Error in the 'for'-loop?
>
> Okay, i solved it, instead of:
> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>
> this:
> bArr = [btn_1,btn_2,btn_3,btn_4];
I would also suggest you use a foreach loop to iterate over the buttons:
private void letButtonsFlash()
{
foreach(button; bArr)
{
writeln(button.getName());
}
}
//Also, question to anyone: should a ref be used here?
|
June 22, 2016 Re: Access vialotion | ||||
---|---|---|---|---|
| ||||
Posted in reply to vladdeSV | On Wednesday, 22 June 2016 at 13:24:47 UTC, vladdeSV wrote:
> On Wednesday, 22 June 2016 at 12:47:31 UTC, TheDGuy wrote:
>> On Wednesday, 22 June 2016 at 12:45:29 UTC, TheDGuy wrote:
>>> I have an array of buttons:
>>>
>>> class Window : MainWindow{
>>> private Button[4] bArr;
>>> this(){
>>> Button btn_1 = new Button();
>>> Button btn_2 = new Button();
>>> Button btn_3 = new Button();
>>> Button btn_4 = new Button();
>>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>>> }
>>> private void letButtonsFlash(){
>>> for(int i = 0; i < 4; i++){
>>> writeln(this.bArr[i].getName());
>>> }
>>> }
>>> }
>>>
>>> i don't understand why i get an 'Access Violation' - Error in the 'for'-loop?
>>
>> Okay, i solved it, instead of:
>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>>
>> this:
>> bArr = [btn_1,btn_2,btn_3,btn_4];
>
> I would also suggest you use a foreach loop to iterate over the buttons:
>
> private void letButtonsFlash()
> {
> foreach(button; bArr)
> {
> writeln(button.getName());
> }
> }
>
> //Also, question to anyone: should a ref be used here?
No, ref for classes is pointless. The ptr destination is always the same heap chunk, whatever you use the original pointer or not.
|
June 23, 2016 Re: Access vialotion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moro Greenhand | On 6/22/16 9:00 AM, Moro Greenhand wrote:
> On Wednesday, 22 June 2016 at 12:47:31 UTC, TheDGuy wrote:
>> On Wednesday, 22 June 2016 at 12:45:29 UTC, TheDGuy wrote:
>>> I have an array of buttons:
>>>
>>> class Window : MainWindow{
>>> private Button[4] bArr;
>>> this(){
>>> Button btn_1 = new Button();
>>> Button btn_2 = new Button();
>>> Button btn_3 = new Button();
>>> Button btn_4 = new Button();
>>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>>> }
>>> private void letButtonsFlash(){
>>> for(int i = 0; i < 4; i++){
>>> writeln(this.bArr[i].getName());
>>> }
>>> }
>>> }
>>>
>>> i don't understand why i get an 'Access Violation' - Error in the
>>> 'for'-loop?
>>
>> Okay, i solved it, instead of:
>> Button[4] bArr = [btn_1,btn_2,btn_3,btn_4];
>>
>> this:
>> bArr = [btn_1,btn_2,btn_3,btn_4];
>
> I would expect DMD to output a warning here, because of the
> shadowing...but after 3 verifications, nothing. Dscanner does.
Variable shadowing only is reported by the compiler for function locals. That is, variables cannot shadow other variables from the same function. It allows shadowing of variables outside the function. This is deliberate. Otherwise, someone may name a variable somewhere else the same as yours, and your perfectly working code is now flagged as an error.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation