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

Comments

  1. TArray does not have any methods. It's a dynamic array.

    ReplyDelete
  2. Stefan, 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 ?

    ReplyDelete
  3. TArray = array of string

    What 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?

    ReplyDelete
  4. for item in items do ... 

    or 

    for i := 0 to High(items) do ...

    ReplyDelete
  5. 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 ).

    ReplyDelete
  6. Thanks guys !! I appreciate those replies. Feel a bit foolish though ;-)

    ReplyDelete
  7. Wondering why you are not using TList or TStringList? Seems easier to work with.

    ReplyDelete
  8. TStringList or TList is what I want to return it as ideally rather than TArray

    ReplyDelete
  9. The drawback of returning a TStringList or TList is remembering who's responsibility it is to free it.
    I tend to have a function TSomeClass.ReturnStrings(const sList: TStrings):Integer which require an initialized list instead.

    ReplyDelete
  10. 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.

    ReplyDelete
  11. As 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 ;-)

    ReplyDelete
  12. I 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

    ReplyDelete
  13. Stefan Glienke For that reason I hide most of my objects behind interfaces. So my function signature would look like this:

    function FindValues(const expression: string; const line: string): IList;

    ReplyDelete
  14. 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.

    ReplyDelete
  15. Oh, and my suggestion was based on the extra builtin methods in those objects, to work easier than with the TArray<>.

    ReplyDelete
  16. Christopher 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.

    Dennis Langthjem If we could write helpers for TArray now that would be nice

    ReplyDelete
  17. On 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.

    ReplyDelete
  18. I 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.

    Everyone 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.

    ReplyDelete
  19. Bookmarked it, cheers David. Will go and have a good read this afternoon and update my understanding. Thanks

    ReplyDelete

Post a Comment