Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of TImage

Status
Not open for further replies.

umyeah

Programmer
May 25, 2004
6
US
I have a screen with ten Images on a form which are Image1, Image2, etc. However, since I do not always fill them all, I want to be able to access them using a integer variable (i.e Image[curnoofimages + 1]). If I did this I could just copy the array off to each image at the end to the rountine.

However, when I try to do anything with the array of TImage, I get access violations. Is there a way to prevent this or is there a way to get each TImage of the form itself set up into an array?

As an additional question, is it possible to get an array of commands so I can access them by an integer variable (i.e. command := Subrountine1())? For this I figure I can at least call a subroutine with a case statement that executes the commands but with the Image I can't think of a way to have a subroutine's return value telling me which Image to use, so I end up with 10 case statements that are 20 long in the same rountine.

I hope that this all made sense and I hope I am just missing the obvious as usual.
 
Never mind I set up an array of TCanvas which was all I really needed since that is what is being drawn to.

Sorry
 
You should really start a new thread for each question [smile]

Have a look at TImageList (it's on the Win32 tab) to implement an array of TImage objects. TImageList is used by several other VCL components so its worth getting to know and its not difficult to use.

If you get access violations when using your array of TImage then its likely because you haven't created each TImage object properly in the array.

When posting questions, it is a good idea to show the minimum amount of code that demonstrates the problem you are experiencing. Quite often the very act of producing this minimum code will help you solve your problem yourself and enhance your understanding of Delphi.

It is possible to have an array of commands that can be called by a variable. Delphi uses the words function and procedure where you use command.

You need to be familiar with procedural types.

I didn't really understand what you were trying to do but suppose you want to call one of three functions (that return a TImage) and you want to select the functions from an array of functions using an integer.

Define the procedural type for the function:
Code:
type
  TImageFunction = function ( n: integer ): TImage;
Now declare an array of this procedural type:
Code:
var
  ImageF: array [ 1..3 ] of TImageFunction;
Somewhere in the code you will have the 3 functions:
Code:
function Tom(n: integer): TImage;
begin
  result := TImage.Create(nil);
end;

function Dick(n: integer): TImage;
begin
  result := TImage.Create(nil);
end;

function Harry(n: integer): TImage;
begin
  result := TImage.Create(nil);
end;
Somewhere, perhaps in an OnCreate event handler, you will assign the functions to the array:
Code:
  ImageF[1] := Tom;
  ImageF[2] := Dick;
  ImageF[3] := Harry;
You can now call any one of these functions by using an integer variable:
Code:
  n := Random(3) + 1;  // Randomly select a function
  ImageF[n];           // Call the function
Whilst using procedural types can often seem a neat solution to some programming problems you may find that using OO technology such as polymorphism can be even more elegant. Case statements and large if...then..else constructs frequently indicate that polymorphism should be considered.


Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top