#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Maine
    Posts
    2
    Rep Power
    0

    Substring problems


    Alright, this has probably been asked a hundred times on this forum but I didn't know how to word it quite right to search it; so here's my problem:

    I am trying to make an invoice for a programming I designing for my VB class. I want it to take the first letters from the first and last name of one text box, and the last four characters of another text box (the last five characters of the text box should be a ZIP code if the user uses the program correctly)

    This is what I currently have right now and it keeps crashing. I don't know what I'm doing wrong and there's not much in the book about this. Sorry if this question has been asked a thousand times before but it's getting end of the semester and I would just like some help. It would be gladly appreciated, thank you.

    'Creating invoice
    Dim name As String = txtName.Text 'full name
    Dim firstname, lastname As String 'First and Last
    Dim zip As String = txtCity.Text 'Zip
    Dim n As Integer = name.IndexOf(",")
    Dim length As Integer = -4

    firstname.IndexOf(name)
    lastname = name.Substring(n, 1)
    length = zip.Length

    Dim invoice As String = firstname & lastname & CStr(length)

    lstResult.Items.Add(invoice)
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    Hello and welcome to devshed.

    Just to let you know you can wrap your code in [ Code ] tags and it will format it nicer.
    Code:
            'Creating invoice
            Dim name As String = txtName.Text       'full name
            Dim firstname, lastname As String       'First and Last
            Dim zip As String = txtCity.Text        'Zip
            Dim n As Integer = name.IndexOf(",")
            Dim length As Integer = -4
    
            firstname.IndexOf(name)
            lastname = name.Substring(n, 1)
            length = zip.Length
    
            Dim invoice As String = firstname & lastname & CStr(length)
    
            lstResult.Items.Add(invoice)
    A lot of it depends on the type of exception being thrown but I would guess it's related to the firstname.IndexOf(name) line. You're calling a function on firstName before firstName has been set to anything (although if IndexOf is a static method it might not matter.)

    -MBirchmeier
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Maine
    Posts
    2
    Rep Power
    0
    Opps, wrong code. I changed it and I meant to copy and pasted the new code. Here's the new code:

    Code:
    'Creating invoice
            Dim name As String = txtName.Text.ToUpper               'full name
            Dim firstname, lastname As String                       'First and Last
            Dim zip As String = txtCity.Text                        'Zip
            Dim n As Integer = name.IndexOf(",")
            Dim length As Integer = -4
    
            firstname = name.Substring(0)
            lastname = name.Substring(n, 1)
            length = zip.Length
    
            Dim invoice As String = firstname & lastname & CStr(length)
    
            lstResult.Items.Add(invoice)

IMN logo majestic logo threadwatch logo seochat tools logo