I am hoping someone here can give me a helping hand. I thought my coding was going to be simple today, but the Gods of Jinx had other plans for me.
I am hoping someone here can give me a helping hand. I thought my coding was going to be simple today, but the Gods of Jinx had other plans for me.
I have some XE7 code that goes like:
function TScriptConstructor.FindValues( const expression: string; const line: string ): TArray;
var
regex: TRegEx;
matches: TArray;
begin
regex:= TRegEx.Create( expression, [roIgnoreCase] );
matches := regex.Split( line );
Result := matches;
end;
So far Ok, but I can't seem to do anything with this TArray when I get it back in other functions. I type matches. within the function itself waiting for code completion to kick in and nothing. I cannot seem to find anything helpful when I google it, so I don't normally post issues here, but here goes ....
Minimally, I need to be able to get the count of this array and the string items on the array.
Can someone help I would be very so grateful.
Tony
I have some XE7 code that goes like:
function TScriptConstructor.FindValues( const expression: string; const line: string ): TArray
var
regex: TRegEx;
matches: TArray
begin
regex:= TRegEx.Create( expression, [roIgnoreCase] );
matches := regex.Split( line );
Result := matches;
end;
So far Ok, but I can't seem to do anything with this TArray
Minimally, I need to be able to get the count of this array and the string items on the array.
Can someone help I would be very so grateful.
Tony
TArray does not have any methods. It's a dynamic array.
ReplyDeleteStefan, thanks: How do I use it then ? Or let me ask it differently how do I get regex.Split to work so that I can get to the Count and items ?
ReplyDeleteTArray = array of string
ReplyDeleteWhat do you want to do with it? Iterate over the array?
for s in matches do writeln(s);
Ask for the length:
length(matches)
What else do you want to do?
for item in items do ...
ReplyDeleteor
for i := 0 to High(items) do ...
That's all I want to do ! Thank you ! It basically similar in use to an array of string then ( except for the for .. in ... bit ).
ReplyDeleteThanks guys !! I appreciate those replies. Feel a bit foolish though ;-)
ReplyDeleteWondering why you are not using TList or TStringList? Seems easier to work with.
ReplyDeleteTStringList or TList is what I want to return it as ideally rather than TArray
ReplyDeleteThe drawback of returning a TStringList or TList is remembering who's responsibility it is to free it.
ReplyDeleteI tend to have a function TSomeClass.ReturnStrings(const sList: TStrings):Integer which require an initialized list instead.
Dennis Langthjem Returning objects from functions is never easier to work with because then the creator is a different place than the one responsible for freeing. Dynamic arrays are just perfect for this case.
ReplyDeleteAs a "general" rule I always try to make the 'caller' responsible for that one. Although it gets into a grey area sometimes when you are working out who the 'caller' / 'consumer' actually is ;-)
ReplyDeleteI wouldn't always convert dynamic array into an list object, but i do have need for it in a number places within the class itself and the additional methods would help
ReplyDeleteStefan Glienke For that reason I hide most of my objects behind interfaces. So my function signature would look like this:
ReplyDeletefunction FindValues(const expression: string; const line: string): IList;
I get your points about freeing responsibility. I just always have a local instance, which I assign from the function, and then free as usual. That way it's just a matter of where my local instance gets it's data from. Alternatively I would have my local instance as a var param in a procedure. But in Delphi we sadly have a wast array of options, each with pros and cons. Working daily with D2010, where a lot of the code goes as far back as D3.
ReplyDeleteOh, and my suggestion was based on the extra builtin methods in those objects, to work easier than with the TArray<>.
ReplyDeleteChristopher Wosinski So do I (I wonder where that IList came from ^^). But as always depends on the situation. If you just need to return a number of elements to iterate over creating an object is unnecessary overhead.
ReplyDeleteDennis Langthjem If we could write helpers for TArray now that would be nice
Stefan Glienke Agreed.
ReplyDeleteOn the face of it, creating an instance of a class just to hold an array of values is massively over the top. Learn to use arrays when arrays are suitable, and classes when classes are suitable.
ReplyDeleteI think this debate has possibly gone a little bit off topic ;-) I have the answer I needed; which is that a TArray type works in much the same way as an array of string and that is fine for what I need.
ReplyDeleteEveryone is making a lot of comments on how I should pass values around inside my class and whether Interfaces should be used or if I should pass it back as a TStringList or a TList or not. However, not one person has asked me what the class I am writing is for and what my end goal is. No one knows that better than me and I am still undecided as to whether to keep it as and TArray.
Thank you all for your input though, it has all been thought provoking and definitely a worthwhile debate.
The documentation is here: http://docwiki.embarcadero.com/Libraries/XE8/en/System.TArray which is always a good place to start
ReplyDeleteBookmarked it, cheers David. Will go and have a good read this afternoon and update my understanding. Thanks
ReplyDelete