🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.language.delphi.dotnet
9 messages
9 total messages Started by Stephen Boyd Wed, 18 Jun 2008 21:07
Arrays
#38
Author: Stephen Boyd
Date: Wed, 18 Jun 2008 21:07
21 lines
1517 bytes
I have come across a stumper.  I have a class which contains an array. 
Like this:

   TEsCalcColorArray = array[0..7] of TColor;
   TEsCalcColors = class(TPersistent)
   public
     FCalcColors   : TEsCalcColorArray;
   end;

At some point later, I instantiate an instance of the class and try to 
access the array member.  Like this:

   FColors := TEsCalcColors.Create;
   FColors.FCalcColors[0] := clRed;

I get an exception essentially telling me that I am trying to access a 
null pointer.  If I look at FCalcColors with the debugger, it is in fact 
null.

What gives?  This is a static array.  If the containing object exists, 
then the array should exist.  Yet clearly in this case it doesn't.
Re: Arrays
#39
Author: Dennis Passmore
Date: Wed, 18 Jun 2008 21:51
17 lines
1240 bytes
How did you declare FColors ?? this code works just fine for me


type
  TEsCalcColorArray = array[0..7] of TColor;
  TEsCalcColors = class(TPersistent)
  public
    FCalcColors   : TEsCalcColorArray;
  end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FColors: TEsCalcColors; // ????
begin
  FColors := TEsCalcColors.Create;
  FColors.FCalcColors[0] := clRed;
end;
Re: Arrays
#40
Author: Rudy Velthuis [TeamB]
Date: Wed, 18 Jun 2008 21:55
59 lines
3169 bytes
Stephen Boyd wrote:

> I have come across a stumper.  I have a class which contains an
> array. Like this:
> 
>   TEsCalcColorArray = array[0..7] of TColor;
>   TEsCalcColors = class(TPersistent)
>   public
>     FCalcColors   : TEsCalcColorArray;
>   end;
> 
> At some point later, I instantiate an instance of the class and try
> to access the array member.  Like this:
> 
>   FColors := TEsCalcColors.Create;
>   FColors.FCalcColors[0] := clRed;
> 
> I get an exception essentially telling me that I am trying to access
> a null pointer.

That doesn't make sense. Could it be that FColors is a field of a class
that was not instantiated yet, and that the assignment to FColors
failed?

This works:

  program Project114;

  {$APPTYPE CONSOLE}

  uses
    SysUtils, Classes, Graphics;

  type
    TEsCalcColorArray = array[0..7] of TColor;
    TEsCalcColors = class(TPersistent)
    public
      FCalcColors: TEsCalcColorArray;
    end;

  procedure Test;
  var
    FColors: TEsCalcColors;
  begin
    FColors := TEsCalcColors.Create;
    FColors.FCalcColors[0] := clRed;
    FColors.Free;
  end;

  begin
    Test;
    Readln;
  end.

-- 
Rudy Velthuis [TeamB]        http://www.teamb.com

"Why don't you write books people can read?"
 -- Nora Joyce to her husband James (1882-1941)
Re: Arrays
#41
Author: Stephen Boyd
Date: Thu, 19 Jun 2008 14:22
12 lines
1429 bytes
Rudy Velthuis [TeamB] wrote:
> 
> That doesn't make sense. Could it be that FColors is a field of a class
> that was not instantiated yet, and that the assignment to FColors
> failed?
> 

The code that I excerpted is running in the constructor of a class. 
FColors is a member of the class being instantiated.  According to the 
debugger FColors is NOT nil, as I would expect if the FColors := 
TEsColors.Create statement failed.  I would also have expected an 
exception to be thrown in that case.
Re: Arrays
#42
Author: Wayne Niddery (TeamB)
Date: Thu, 19 Jun 2008 14:41
13 lines
1869 bytes
"Stephen Boyd" <sboydlns@gmail.com> wrote in message 
news:485a6ba9$1@newsgroups.borland.com...
> The code that I excerpted is running in the constructor of a class. 
> FColors is a member of the class being instantiated.  According to the 
> debugger FColors is NOT nil, as I would expect if the FColors := 
> TEsColors.Create statement failed.  I would also have expected an 
> exception to be thrown in that case.

Let's see the code that is instantiating the containing class.

-- 
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca) 
Re: Arrays
#43
Author: Marc Rohloff [TeamB]
Date: Thu, 19 Jun 2008 15:12
9 lines
1444 bytes
On Wed, 18 Jun 2008 17:51:35 -0400, Dennis Passmore wrote:

> How did you declare FColors ?? this code works just fine for me

This doesn't work for me. Did you test this under .NET?

-- 
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
Re: Arrays
#44
Author: Marc Rohloff [TeamB]
Date: Thu, 19 Jun 2008 15:25
14 lines
1524 bytes
On Wed, 18 Jun 2008 17:07:41 -0400, Stephen Boyd wrote:

>    TEsCalcColorArray = array[0..7] of TColor;
>    TEsCalcColors = class(TPersistent)
>    public
>      FCalcColors   : TEsCalcColorArray;
>    end;

You need to create an 'empty' constructor that only calls inherited.
http://qc.codegear.com/wc/qcmain.aspx?d=25580

-- 
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com
Re: Arrays
#45
Author: Stephen Boyd
Date: Thu, 19 Jun 2008 16:05
17 lines
1629 bytes
Marc Rohloff [TeamB] wrote:
> On Wed, 18 Jun 2008 17:07:41 -0400, Stephen Boyd wrote:
> 
>>    TEsCalcColorArray = array[0..7] of TColor;
>>    TEsCalcColors = class(TPersistent)
>>    public
>>      FCalcColors   : TEsCalcColorArray;
>>    end;
> 
> You need to create an 'empty' constructor that only calls inherited.
> http://qc.codegear.com/wc/qcmain.aspx?d=25580
> 
That fixed it.  Thank You.  That was driving me nuts!  I thought that 30 
years of programming had finally destroyed what little mind I have left. ;)

You would think that the good folks at CodeGear would have fixed that by 
now and issued an update.
Re: Arrays
#46
Author: Rudy Velthuis [TeamB]
Date: Thu, 19 Jun 2008 16:58
14 lines
2312 bytes
Marc Rohloff [TeamB] wrote:

> You need to create an 'empty' constructor that only calls inherited.
> http://qc.codegear.com/wc/qcmain.aspx?d=25580

Aargh! I had not noticed this was .dotnet. Indeed, you are right.

This issue does not arise in Win32.
-- 
Rudy Velthuis [TeamB]        http://www.teamb.com

"Every day I get up and look through the Forbes list of the 
 richest people in America. If I'm not there, I go to work."
 -- Robert Orben
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads