Board index » delphi » Using arrays of objects

Using arrays of objects

Greeting everyone,

I'm developing a Neural Network project in Delphi and there's a retina (
matrix of neurons ). I tried doing that:

NeuronsArray : Array[1..10][1..10] of TNeurons

in the public declaration, but it seems that the Array is always with nil.
And if I try Array[x][y].Create(Self) I get an Acess Violation Exception.
What I'm doing wrong and how should I proceed?

Thanks in advance,
Eduardo Melo.

 

Re:Using arrays of objects


Eduardo da Fonseca Melo wrote:

Quote

> Greeting everyone,

> I'm developing a Neural Network project in Delphi and there's a retina (
> matrix of neurons ). I tried doing that:

> NeuronsArray : Array[1..10][1..10] of TNeurons

> in the public declaration, but it seems that the Array is always with nil.
> And if I try Array[x][y].Create(Self) I get an Acess Violation Exception.
> What I'm doing wrong and how should I proceed?

> Thanks in advance,
> Eduardo Melo.

Try writing:

NeuronsArry[x][y]:=TNeuron.Create(Self);
--
                 VVVV
Hilsen -----ooO-(+)(+)-Ooo----- LARS
                /(())\
               ( ~~~~ )
                \::::/

Re:Using arrays of objects


Quote
Lars Nielsen wrote in message <34C89A9D.12398...@kom.auc.dk>...
>Eduardo da Fonseca Melo wrote:

>> Greeting everyone,

>> I'm developing a Neural Network project in Delphi and there's a retina (
>> matrix of neurons ). I tried doing that:

>> NeuronsArray : Array[1..10][1..10] of TNeurons

>> in the public declaration, but it seems that the Array is always with
nil.
>> And if I try Array[x][y].Create(Self) I get an Acess Violation Exception.
>> What I'm doing wrong and how should I proceed?

>> Thanks in advance,
>> Eduardo Melo.

>Try writing:

>NeuronsArry[x][y]:=TNeuron.Create(Self)

If you're not using OP classes, then the alternative to
using the Create that Lars points out is to do a simple
assignment, as
var
  aTNeuron: TNeuron;
  x,y: 1..10;
begin
aTNeuron.field1 := something;
// etc
NeuronsArry[x][y]:= aTNeuron;

--
Grace + Peace | Peter N Roth | Engineering Objects Int'l
  Author: Round Robin, a Delphi scheduling component
Visit our website at http://www.inconresearch.com/eoi
"Random numbers are too important to be left to chance." - anon

Re:Using arrays of objects


Eduardo da Fonseca Melo wrote:

Quote
> Greeting everyone,

> I'm developing a Neural Network project in Delphi and there's a retina (
> matrix of neurons ). I tried doing that:

> NeuronsArray : Array[1..10][1..10] of TNeurons

> in the public declaration, but it seems that the Array is always with nil.
> And if I try Array[x][y].Create(Self) I get an Acess Violation Exception.
> What I'm doing wrong and how should I proceed?

> Thanks in advance,
> Eduardo Melo.

This is a bit off the subject of you post.  I assume that what you're trying
to do will be quite computationally intensive, and as such you may want to
consider making a couple of small changes to how you've structured your
array.  This is especially true if you're using anything less than a Pentium
II.

I'd recommend constructing your type something like this:

    TNeuronArray=array[0..15] of TNeuron;
    TNeuronMatrix=array[0..15] of TNeuronArray;
    PNeuronArray=^TNeuronArray;

This gives two advantages:

    1) Keeping the dimensions as a power of 2 reduces the computation time
required to calc the actual address of individual members.

    2) By declaring a local variable and assigning the address of a row of the
matrix you can reduce the overhead associated with the matrix substantially.
It looks like this:

procedure DoSomething(Matrix:TNeuronMatrix);
var
    MatrixRowJ:PNeuronArray;
    j,k:integer;
begin
    For j:=0 to NumRows-1 do
    begin
        MatrixRowJ:=@Matrix[j];
        for k:=0 to NumCols-1 do
            {do something with MatrixRowJ[k] }
    end;
end;

Bob Lee

Re:Using arrays of objects


I'm having the same difficulty as you here is my simplified object.
TSquareObject = class
    private
      Current_Rainfall_YTD : Integer;
      Avg_Rainfall_100yrAVG : Integer;
    public

    end;

var
  Form1: TForm1;
  Grid: array [0..3] of TSquareObject;
  i: integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  for i := 0 to 3 do
    Grid.[i] := TSquareObject.create(self);

end;

I get this compile error msg
  Pas(44): Record, object or class type required.
  pas(44): Too Many actual parameters

I'm new to doing objects and arrays myself. I'm finding that resources on
object programming few and scarce.

In article <34C89A9D.12398...@kom.auc.dk>, lnl...@kom.auc.dk says...

Quote

>Eduardo da Fonseca Melo wrote:

>> Greeting everyone,

>> I'm developing a Neural Network project in Delphi and there's a retina (
>> matrix of neurons ). I tried doing that:

>> NeuronsArray : Array[1..10][1..10] of TNeurons

>> in the public declaration, but it seems that the Array is always with nil
>.
>> And if I try Array[x][y].Create(Self) I get an Acess Violation Exception.
>> What I'm doing wrong and how should I proceed?

>> Thanks in advance,
>> Eduardo Melo.

>Try writing:

>NeuronsArry[x][y]:=TNeuron.Create(Self);
>--
>                 VVVV
>Hilsen -----ooO-(+)(+)-Ooo----- LARS
>                /(())\
>               ( ~~~~ )
>                \::::/

Re:Using arrays of objects


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 26 Jan 1998 07:52:27 GMT, todle...@onramp.net (Todd Lewis) wrote:

Quote
>I'm having the same difficulty as you here is my simplified object.
[snip snip snip]
>var
>  Form1: TForm1;
>  Grid: array [0..3] of TSquareObject;
>  i: integer;
>procedure TForm1.Button1Click(Sender: TObject);
>begin
>  for i := 0 to 3 do
>    Grid.[i] := TSquareObject.create(self);

>end;

The two main problems are this:
Grid.[i]

Grid is an array, and if you wanted to access the ith element of Grid,
that would be Grid[i].

TSquareObject.create(self);

The (AOwner : TComponent) parameter is only used (in most instances)
for Delphi components. You have no special constructor (Create) for
TSquareObject, so Grid[i] := TSquareGrid.Create; works.

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBNMxbpAdZfH4UxP3mEQLvtACg9JKZ0qvNG6iEiYkMp1grJmUMnk0AoLxN
5FqoREyhcsBwF7CydqGDSJI0
=J4CS
-----END PGP SIGNATURE-----

"I never set out to be weird. It was always the other people
 who called me weird."
-- Frank Zappa --

http://www.sinnerz.com/tmessiah/
KeyID: 4096/1024/0x14C4FDE6
Fingerprint: 1263 DBFD F2C4 77C6 87F2 A94A 0759 7C7E 14C4 FDE6

     -export-a-crypto-system-sig -RSA-3-lines-PERL
#!/bin/perl -sp0777i<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsj
$/=unpack('H*',$_);$_=`echo 16dio\U$k"SK$/SM$n\EsN0p[lN*1
lK[d2%Sa2/d0$^Ixp"|dc`;s/\W//g;$_=pack('H*',/((..)*)$/)

Re:Using arrays of objects


Quote
Todd Lewis wrote:
> I'm having the same difficulty as you here is my simplified object.

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   for i := 0 to 3 do
>     Grid.[i] := TSquareObject.create(self);
> end;

    {Try this line. The period before indexing an array is unnesarry
    and is likely causing the error.}
  Grid[i] := TSquareObject.create(self);

Other Threads