Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 7th, 2012, 04:02 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Program works but ends with error.

I am using VB 2010 Express, Win7.
I am a 68 year old retired school teacher who has not programmed for over six years...last was VB 2. I am trying to write code to upgrade a former progam but have run into trouble. As the title says, the program segment works, but ends with the following errors:

Quote:
Argument out of range exception was unhandled.
Index and Length must refer to a location within the string. Parameter name: Length.
Trouble shooting tips:
Make sure the arguments to this method have valid values.
The Code:
Quote:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'Declares a Numeric Variable.
Dim N As Integer
' Returns the length of the string in the Text Box assigns it to "N".
N = Len(TextBox1.Text)
' Declares an Array to hold String data, adjusting string lenght for 0-9.
Dim SecretinPut(N - 1) As String

For Index As Integer = 0 To N
' "Index" will start with the first char. of the first word, store in Array
SecretinPut(Index) = (TextBox1.Text.Substring(Index, 1))
' Just to keep track of values...puts in a message box on screen as pgm. runs.

MessageBox.Show(SecretinPut(Index))
'Output to a diskfile, appends anything already there.
My.Computer.FileSystem.WriteAllText("C:\Holding.tx t", SecretinPut(Index), True)

Next Index


End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class
The red font indicates the code that generates the error.

Can anyone tell me what is wrong/how to fix it?

Last edited by dahermit; March 7th, 2012 at 04:14 PM.
Reply With Quote
  #2 (permalink)  
Old March 7th, 2012, 06:50 PM
Friend of Wrox
Points: 1,075, Level: 12
Points: 1,075, Level: 12 Points: 1,075, Level: 12 Points: 1,075, Level: 12
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: , , United Kingdom.
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

This is a simpler way of doing. Lists are better to use than arrays, especially typed ones. Nicer to look at, more semantic.

Code:
Dim arrayChar As List(string)

For i as Integer = 0 to Textbox1.text.length

    arrayChar.add(Textbox1.text.substring(i, 1))
    Messagebox.show(arrayChar.Item(i).ToString)

Next

'Doing the file writing after, means that if you do hit an error, its not left up to garbage collector to do the work, ideally you don't want the file open cause this will cause errors in the disk obviously. Better to do it separately so it can be handle properly

'use a few try catch statements for extra safety if necessary

Dim p As New FileStream("C:\NewText.txt", {and the appropriate write flags, don't know them off the top of my head})
Dim pt As New TextWriter(p)

For each charString In arrayChar

    pt.writeline(charString)

next

pt.close 'this flushes the file so writes to disk
__________________
Apocolypse2005, I'm a programmer - of sorts.
Reply With Quote
  #3 (permalink)  
Old March 7th, 2012, 09:20 PM
Registered User
Points: 11, Level: 1
Points: 11, Level: 1 Points: 11, Level: 1 Points: 11, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Arrays vs. Lists.

Thank you for you code suggestions.
I am more comfortable with using arrays for a few reasons. I am more familiar with them, only having heard of lists a week or so ago when I bought my first VB 2010 books. Also, arrays seem to be more adept at manipulation via For/Next loops (Is that true?). The program segment I listed above does not fore tell the manipulation of the characters that will occur further in the program and at his point I am still struggling with the VB 2010 language and want to just get the program working; not so much concerned with the elegance of the coding. I am discovering that VB 2010 is several times more complicated than VB 2, QBASIC, Pascale et. Al., were. I feel sorry for those who have to learn it as their first programming language.
Thank you again for your help.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bound Vs unbound controls on front ends Richard_AU General .NET 2 January 9th, 2007 07:36 PM
Error but it works Mitch PHP Databases 2 June 4th, 2006 09:25 AM
how to update a database when asp.net session ends connect2sandep ASP.NET 1.0 and 1.1 Professional 3 October 30th, 2005 03:46 PM
Update progress until another process ends? Kyle Ketterer Pro VB.NET 2002/2003 1 July 4th, 2004 09:23 PM
Program works, but no data being displayed bebe XML 0 October 14th, 2003 01:44 PM



All times are GMT -4. The time now is 09:36 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.