[Gambas-user] GB3 Editor: how to elaborate Editor.Lines (and CSTR behaviour)

Tobias Boege taboege at ...626...
Sun Jun 29 20:06:27 CEST 2014


On Sat, 28 Jun 2014, Orionis wrote:
> Hi, I'm starting to investigate this control. This is my first question, but
> more will follow
> I have an Editor called 'edit' loaded with some text; I want to recover each
> single line to parse them.
> 
> This is an example of code:
> [code]
> Dim lineObj As Variant
> Dim sRow As String
> 
>    For cnt = 0 To edit.lines.Count - 1
>       lineObj = edit.Lines[cnt]
>       sRow = CStr(lineObj)
>    Next
> [/code]
> I get an "Type mismatch errror: wanted String, got  .Editor.Line instead" at
> line 'sRow =...'
> First question: why the CStr, which should accept a Variant, complies on
> this value?
> 

.Editor.Line is a virtual class which means that it does not represent a
distinct object but merely a state of another object. (This is totally
different from the "virtual" keyword in C++!) You cannot hold a reference
to virtual objects, so you cannot put them into Variant reliably either or
convert them, etc..

> Later;
> In the documentation I found this example:
> [code]
> Dim hEditor As Editor
> Dim hEditorLine As .Editor.Line
> hEditorLine = hEditor.Lines [ Line As Integer ]
> [/code]
> This is completely wrong from my test:
> *Dim hEditorLine As .Editor.Line* gives an "Unexpected . "
> Removing the initial dot (so, *As Editor.Line*) does not recognize the type
> 

Your interpretation is not the way in which the documentation should be
read. As I said above, virtual objects cannot be referenced, so you cannot
declare a variable of a virtual class type (as you have noticed). The
documentation syntax above just tells you what type the return value is
and thereby provides a link to its documentation.

I agree that the fact that it indeed declares a variable of that type (of
which you cannot declare variables actually) is a bit confusing but once
you've seen this pattern, you're in the know. Virtual class names, by
convention, start with the "." character.

> At the end: how can I get a single line as string from the Editor?
> 

Virtual objects mimic the way real objects work, i.e. they have properties
and methods. (Virtual objects are never single values because in this case,
there would not be a virtual class in the first place.)

In this particular case you get the text of the i-th line by:

  edit.Lines[i].Text

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list