Results 1 to 8 of 8

Thread: String manipulation!

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    String manipulation!

    I'm doing some homework for my VB class where I have to write a program that creates an invoice for a customer. I'm having an issue with converting the name input from the format of (Last, First) within the same textbox to (First Last) in a listbox via a function. I have done everything but dealing with the strings and I'm at a loss.

    I'm also having to create an invoice number with the first two letters of the last name and also the last four numbers of the zip code in an address line such as
    City, State, Zip.
    Manipulating these strings is killing me.

    Below is the code that I have written, manipulation of the strings must be in one function for each the invoice and the and the name issue....
    Can anyone give some suggestions????

    Public Class question3


    Dim custinvoicenum As String





    Private Sub btnProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProcess.Click
    Dim custname As String 'Name of customer
    Dim custaddress As String 'customer's address
    Dim custcitystzip As String 'customer's city state and zipcode
    Dim orderedchairs As Integer 'number of chairs ordered by customer
    Dim orderedsofas As Integer 'number of sofas ordered by customer
    Dim custinvoicenum As String 'customer's invoice number
    Dim costoutput As Double 'cost before taxes
    Dim salestax As Double 'total amount of taxes paid
    Dim totalcost As Double 'total amount paid after taxes are added
    Dim taxrate As Double 'taxrate charged to cost before taxes
    Dim cname As String
    'tasks
    InputData(custname, custaddress, custcitystzip, orderedchairs, orderedsofas) 'task 0
    costoutput = cost_output(orderedchairs, orderedsofas) 'task 1
    salestax = sales_tax(costoutput, taxrate) 'task 2
    totalcost = total_cost(salestax, costoutput) 'task 3
    showtotal(custname, custaddress, custcitystzip, orderedchairs, orderedsofas, costoutput, salestax, totalcost) 'task 4
    custinvoicenum = invoice_number(custname, custcitystzip) 'task 5
    custname = cust_name(custname) 'task 6
    End Sub

    Sub InputData(ByRef custname As String, ByRef custaddress As String, ByRef custcitystzip As String, ByRef orderedchairs As Integer,
    ByRef orderedsofas As Integer)
    'Task 0 InputData
    custname = CStr(txtboxName.Text)
    custaddress = CStr(txtboxAddress.Text)
    custcitystzip = CStr(txtboxCityStateZip.Text)
    orderedchairs = CInt(txtboxChairs.Text)
    orderedsofas = CInt(txtboxSofas.Text)
    End Sub

    Function cost_output(ByVal orderedchairs As Integer, ByVal orderedsofas As Integer) As Double
    'Task 1: Compute cost before taxes
    Return ((orderedchairs * 350) + (orderedsofas * 925))
    End Function
    Function sales_tax(ByVal costoutput As Double, ByVal taxrate As Double)
    'Task 2: Compute total sales tax
    taxrate = 0.05
    Return (taxrate * costoutput)
    End Function
    Function total_cost(ByVal salestax As Double, ByVal costoutput As Double)
    'Task 3: Compute total cost to be billed to customer
    Return (salestax + costoutput)
    End Function
    Function invoice_number(ByVal custname As String, ByVal custcitystzip As String)
    'Task 5: Invoice number generation
    Return (custname.Substring(0, 2) & Right.ToString(custcitystzip))
    End Function
    Function cust_name(ByVal custname As String) As String


    End Function

    Sub showtotal(ByVal custname As String, ByVal custaddress As String, ByVal custcitystzip As String, ByVal orderedchairs As Integer,
    ByVal orderedsofas As Integer, ByVal costoutput As Double, ByVal salestax As Double, ByVal totalcost As Double)
    'Task 4 Display results in listbox
    lstboxOutput.Items.Clear()
    lstboxOutput.Items.Add("Invoice number: " & custinvoicenum)
    lstboxOutput.Items.Add("")
    lstboxOutput.Items.Add("Name: " & custname)
    lstboxOutput.Items.Add("Address: " & custaddress)
    lstboxOutput.Items.Add("City: " & custcitystzip)
    lstboxOutput.Items.Add("")
    lstboxOutput.Items.Add("Number of Chiars: " & orderedchairs)
    lstboxOutput.Items.Add("Number of Sofas: " & orderedsofas)
    lstboxOutput.Items.Add("")
    lstboxOutput.Items.Add(" Cost: " & FormatCurrency(costoutput))
    lstboxOutput.Items.Add("Sales Tax: " & FormatCurrency(salestax))
    lstboxOutput.Items.Add(" ----------------")
    lstboxOutput.Items.Add("Total Cost: " & FormatCurrency(totalcost))


    End Sub





























    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    'Clears the textboxes and listboxes on the form when the clear button is clicked
    txtboxAddress.Text = ""
    txtboxChairs.Text = ""
    txtboxCityStateZip.Text = ""
    txtboxName.Text = ""
    txtboxSofas.Text = ""
    lstboxOutput.Items.Clear()
    End Sub


    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
    'Closes the form when the quit button is clicked
    Me.Close()
    End Sub

    Private Function Left(ByVal custname As String, ByVal p2 As Long) As String
    Throw New NotImplementedException
    End Function




    End Class



  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Wink Re: String manipulation!

    Grand welcome Dear friend

    u have posted too much of codes , sorry no time to read the full
    but i think it is better to split the task in to portions.
    let us take first this
    Code:
    (Last, First) within the same textbox to (First Last) in a listbox via a function.
    please post it with some more detail , exactly what you are in need & trying for
    Last edited by make me rain; Apr 16th, 2011 at 07:16 AM. Reason: Spell mistake
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3
    Lively Member
    Join Date
    Sep 2009
    Location
    Florida
    Posts
    75

    Re: String manipulation!

    The first thing you should do is have the name entered in two separate parts. eg LastName and FirstName. Then when putting them in a textbox, simply concatenate them as FirstName & LastName (don't forget to put a space between them).
    "The most important quality a programmer must have is persistence."

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Re: String manipulation!

    Thanks, I appreciate the help. I had to go ahead and submit the assignment last night without the last two items correctly displaying in the list box. Either way as I am going to school for IT and I really need to know as much as I can for future classes.

    Can you elaborate a bit more on how to separate them?...are you saying within the string manipulation function....
    Dim firstname as string
    Dim lastname as string

    what would the code look like after that?

    The other problem is how would I discern the first name and last name with a space in the string via a string function? I don't have too much of a problem with anything other than strings so far, they are really kicking my butt.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: String manipulation!

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim surName As String = getName(TextBox1.Text)
    5.         MsgBox(getRefString(surName, "12345"))
    6.     End Sub
    7.  
    8.     ''' <summary>
    9.     ''' getName Function
    10.     ''' </summary>
    11.     ''' <param name="name">passed byRef, so reversed names are reflected in the original string</param>
    12.     ''' <returns>surName</returns>
    13.     ''' <remarks></remarks>
    14.     Private Function getName(ByRef name As String) As String
    15.         Dim names() As String = name.Split(New String() {", "}, StringSplitOptions.None)
    16.         Array.Reverse(names)
    17.         name = String.Join(" ", names)
    18.         Return names(names.GetUpperBound(0))
    19.     End Function
    20.  
    21.     ''' <summary>
    22.     ''' getRefString Function
    23.     ''' </summary>
    24.     ''' <param name="lastName">surName</param>
    25.     ''' <param name="zipCode">a string of numbers. must be >= 4 char length</param>
    26.     ''' <returns>a string consisting of first two letters of the last name and the last four numbers of the zip code</returns>
    27.     ''' <remarks></remarks>
    28.     Private Function getRefString(ByVal lastName As String, ByVal zipCode As String) As String
    29.         Return lastName.Substring(0, 2) & zipCode.Substring(zipCode.Length - 4)
    30.     End Function
    31.  
    32. End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    7

    Re: String manipulation!

    I must say, you are a rockstar! Everything is working now, but how would I put the information in a list box instead of a message box? I've tried to manipulate it a few ways but it always just comes up with an error.

    It looks like arrays are the next feat I will have to study up on. This stuff can be a bit stressful at times but when you start to understand a new concept then it's like there is a lightbulb that comes on.

    Thanks again!

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: String manipulation!

    if you have textbox1.text like this:

    lastName, firstName
    you can split it on ", " which will return an array:

    vb Code:
    1. Dim names() As String = textbox1.text.Split(New String() {", "}, StringSplitOptions.None)

    which will give you the names() array with:

    vb Code:
    1. names(0) =  "lastName"
    2. names(1) = "firstName"

    if you reverse this array, you get:

    vb Code:
    1. names(0) =  "firstName"
    2. names(1) =  "lastName"

    which you can join to create a string:

    vb Code:
    1. dim fullName as string = string.join(" ", names)

    + add to your listbox:

    vb Code:
    1. listbox1.items.add(fullName)
    then to create your invoice ref No.:

    vb Code:
    1. 'names(names.getupperbound(0)) is the last element of the names() array
    2. dim refString as string = getRefString(names(names.getupperbound(0)), "12345")

    vb Code:
    1. Private Function getRefString(ByVal lastName As String, ByVal zipCode As String) As String
    2.     Return lastName.Substring(0, 2) & zipCode.Substring(zipCode.Length - 4)
    3. End Function

  8. #8
    Lively Member
    Join Date
    Sep 2009
    Location
    Florida
    Posts
    75

    Re: String manipulation!

    I'm not sure why programming nowadays has to be made so complex. Simply set up two different text boxes, one for the first name and one for the last name. Then concatenate the names together such as: "firstname & ' ' & lastname" and put the result wherever you want.
    "The most important quality a programmer must have is persistence."

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