Results 1 to 3 of 3

Thread: [RESOLVED] Simple yet important question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. If myRichTextBox.Lines Is myRichTextBox.Lines Then
    2.     'The same object is used both times.
    3. Else
    4.     'A new object is used each time.
    5. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width