Search the Community
Showing results for tags 'arrays'.
Found 2 results
-
arrays Help with design pattern
Tntman posted a topic in Algorithms, Data Structures and Class Design
I am using delphi fmx for windows 10. I have listbox that will have list of messages. every message is inserted in that list box as a fmx frame. I decided to use this as a frame because I can get more nicer GUI look. I am simulating creating and deleting messages ( Frames ) by clicking on buttons. So I am basically making front end of the application and I want to make it good working before i actually hook it to internet. I am trying to practice oop and good design patterns so that is why i want to share with you how i did all this so you can give me suggestions how to improve. So I have listbox, when i click button1 i am triggering this code that will add 10 messages ( 10 frames ) to a listbox. for i := 0 to 10 do begin randNumb := Random(9900); messageList[randNumb] := TListBoxItem.Create(Self); messageList[randNumb].Parent := ListBox1; messageList[randNumb].Selectable := False; TFrame1.Create(Self).Name := IntToStr(randNumb); with TFrame1(FindComponent(IntToStr(randNumb))) do begin Parent := messageList[randNumb]; //Frame ID is public int property in Frame unit, i will need it later when I am deleting message ( frame ) frameId := randNumb; Label1.Text := 'Message body'; LabelName.Text := 'John Doe'; Country.Images := ImageList1; end; MessageList represents public property -> MessageList : Array [0 .. 9900] of TListBoxItem; I set array size from 0 to 9900... randNumb presents random number ( index ) that will be given to every messsage ( frame ) Property frameId have same value as name of the frame that i am creating dynamically. When I want to delete message ( frame ) I first click on the frame. When I click on the frame I am triggering OnClick event that is located in Frame unit, after that I am deleting message ( frame ) by using "DisposeOf". I have noticed that i also have to call "DisposeOf" not just on frame, i have to call it on MessageList also. So i am also doing MessageList[frameId].DisposeOf; and frame is deleted , also item in MessageList is deleted. ********************** Everything so far that I wrote is working but it does not feel right... 1. I want to add new message ( frame ) with an ID that is incremented by 1 compared to biggest index in my array. SO if i have 10 messages , when I add next message I want that message index to be 10+1 =11.. 2.Lets say that i have 10 messages , and i delete message with index of 5. What will happen with other message indexes ? is my MessageList array going to shrink or it will just leave empty space where is index 5 ? Will message that had index 6 before deleting message 5 go and drop down for one place and have index 5 ? will message with index 7 drop on 6th place and so on... ? 3.I could possibly make MessageList array with unknown length and everytime when i want to add new message i can check length of MessageList array and resize it for +1 and add that item on the end.. but still i have feeling that this is not a way to make this ... I was reading this : https://stackoverflow.com/questions/433221/how-do-i-declare-an-array-when-i-dont-know-the-length-until-run-time Third message says " If your array is no longer static, you might want to consider using a TList, a TStringList or one of the many container classes in the Contnrs unit. They may better represent what you are doing, provide additional capabilities you might need, e.g. sorting or name/value pairs, they dynamically grow as you need them, and have been very well optimized. " So my conclusion about this is that I have to find some correlation between TList or TStringList that i have to create and MessageList : Array [0 .. 9900] of TListBoxItem that I already created because my list of messages ( Frames ) will grow and shrink dynamically and I dont want to manually figure out how to do it.. I am not sure, maybe my whole concept is wrong from beginning Thank you for your help -
I have a small project in XE7, which is newish to me, where I have to get rid of an Access Violation error on exiting the application. From the error log, I know something is amiss vis-a-vis the arrays I'm trying to clean up. I have one global constant static arrays of strings, two global static arrays of extended and two global static arrays of strings. I have tried to use the setLength method for the cleaning up of the arrays, the finalize method, freeAndNil, free and setting the arrays to nil. My attempts sometimes survive compiling, sometimes don't. Researching the issue on the internet seems mostly to focus on dynamic array clearing. Not much about static arrays and not a solution that I could find. I've tried placing the clearing attempts in OnCloseQuery and disastrously in OnClose. At this point, I'm stumped. Is there a definitive way to do this? GM My creation code in the main form looks like this: var ... ar1: Array [1..100] of string; ar1Total: Array [1..100] of extended; ar2: Array [1..100] of string; ar2Total: Array [1..100] of extended; const TonerColors: TArray<string> = ['Cyan', 'Magenta', 'Yellow','Black'];