[RESOLVED] Simple yet important question
Hello everyone.
I am currently working on my own "Colored textbox control" which inherits from the RichTextBox. What does the "Lines" property do? Does it generate the return array internally, or is it just passing back an array that is internally stored?
E.g. I use this:
Code:
Public Sub SelectLines(ByVal lineindex As Integer, Optional ByVal linecount As Integer = 1)
Dim lines() As String = Me.Lines
If lineindex > lines.Count - 1 Then lineindex = lines.Count - 1
If lineindex + linecount > lines.Count Then linecount = lines.Count - lineindex
Dim selstart As Integer = 0
Dim sellength As Integer = 0
For i As Integer = 0 To lineindex + linecount - 1
If i < lineindex Then selstart += lines(i).Length + 1 Else sellength += lines(i).Length + 1
Next
If sellength > 0 Then sellength -= 1
Me.SelectionStart = selstart
Me.SelectionLength = sellength
Me.Focus()
End Sub
Can I skip the generation of "lines" and just use Me.Lines(i) etc. everywhere?
The reason I ask is that I want to minimize the lag of the control, and I have to read all or a range of lines fairly commonly. :wave:
Re: Simple yet important question
The Lines property generates a new array each time it is accessed. This is easy to confirm for yourself:
vb.net Code:
If myRichTextBox.Lines Is myRichTextBox.Lines Then
'The same object is used both times.
Else
'A new object is used each time.
End If
Re: Simple yet important question
Aw yeah forgot that value returning properties return a reference to the array, thanks jm. :)
That'll be storing the lines in a variable and updating it on each text change. :D