Inserting Line

I am trying to make a simple code generator for my Database classes. I have the following code:
        Dim line As Integer = 0
        Me.OutputText.Lines(line).Insert(0, "Public Class CLASSNAME") 'i would have replaced the classname manually later
        line = line + 1
        Dim i As Integer = 0
        For i = 0 To i = (Me.DataGridView1.Rows.Count - 1)
            Me.OutputText.Lines(0).Insert(0, "Private _" + Me.DataGridView1.Rows(i).Cells(0).ToString + " as " + Me.DataGridView1.Rows(i).Cells(1).ToString)
            i = i + 1
            line = line + 1
        Next

What I am trying to do is have my datagrid's rows create a variable declare statement each on a seperate line in the OutputText textbox. What is wrong with this code? It says that the index is outside of the bounds of the array. And also, what can I do to make this more efficient? Thanks!
itprochrisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff CertainCommented:
One possible issue could be that your datagrid is not bound at this point; if that is the case, the rows collection is empty, and you'll throw an error. Other than that, what line does the error refer to?

Also, you're incrementing i twice, since the for loop does it for you. This will result in your hitting a row outside the datagrid.rows index bounds pretty quickly -- and is probably the root of your problem.

Here's a little cleaner version of your code.

        Dim line As Integer = 0
        Me.OutputText.Lines(line).Insert(0, "Public Class CLASSNAME") 'i would have replaced the classname manually later
        line += 1
        Dim i As Integer = 0
        Dim sb as New System.IO.Stringbuilder(128)
        With Me.DataGridView1
            For i = 0 To Me.DataGridView1.Rows.Count - 1
                sb.Append("Private _" + .Rows(i).Cells(0).ToString + " as " + .Rows(i).Cells(1).ToString & vbCrLf)
                line += 1
            Next
        End With
        Me.OutputText.Lines(0).Insert(0, sb.ToString())

Hope this helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itprochrisAuthor Commented:
Does this post each new "Private _whatever as whatever" on a new line?

Thanks!
Jeff CertainCommented:
Well, the vbCrLf constant is a "visual basic carriage return line feed" character -- equivalent to Chr(10) & Chr(13) -- which is a new line. :)

Now..... if your textbox is sized such that there's only one line visible... you'll have to resize it.

Also... is there a reason you're not Using something like a listbox to display the data?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

itprochrisAuthor Commented:
I am getting errors on the last line that says that the index is outside of the bounds of the array. Could it be because I am using VB.NET 2005 Express Beta?

Also, I am not using a listbox because I need to be able to copy the output.

Thanks
itprochrisAuthor Commented:
Nevermind, I figured out the problem .Thanks for the help!
Jeff CertainCommented:
If you figured out the problem, you ought to post the solution here... in case someone else has the same problem...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.