|
-
Apr 15th, 2011, 06:08 PM
#1
Thread Starter
New Member
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
-
Apr 16th, 2011, 07:15 AM
#2
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
-
Apr 16th, 2011, 10:20 AM
#3
Lively Member
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."
-
Apr 16th, 2011, 12:02 PM
#4
Thread Starter
New Member
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.
-
Apr 16th, 2011, 04:45 PM
#5
Re: String manipulation!
try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim surName As String = getName(TextBox1.Text)
MsgBox(getRefString(surName, "12345"))
End Sub
''' <summary>
''' getName Function
''' </summary>
''' <param name="name">passed byRef, so reversed names are reflected in the original string</param>
''' <returns>surName</returns>
''' <remarks></remarks>
Private Function getName(ByRef name As String) As String
Dim names() As String = name.Split(New String() {", "}, StringSplitOptions.None)
Array.Reverse(names)
name = String.Join(" ", names)
Return names(names.GetUpperBound(0))
End Function
''' <summary>
''' getRefString Function
''' </summary>
''' <param name="lastName">surName</param>
''' <param name="zipCode">a string of numbers. must be >= 4 char length</param>
''' <returns>a string consisting of first two letters of the last name and the last four numbers of the zip code</returns>
''' <remarks></remarks>
Private Function getRefString(ByVal lastName As String, ByVal zipCode As String) As String
Return lastName.Substring(0, 2) & zipCode.Substring(zipCode.Length - 4)
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 16th, 2011, 07:19 PM
#6
Thread Starter
New Member
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!
-
Apr 16th, 2011, 07:50 PM
#7
Re: String manipulation!
if you have textbox1.text like this:
you can split it on ", " which will return an array:
vb Code:
Dim names() As String = textbox1.text.Split(New String() {", "}, StringSplitOptions.None)
which will give you the names() array with:
vb Code:
names(0) = "lastName"
names(1) = "firstName"
if you reverse this array, you get:
vb Code:
names(0) = "firstName"
names(1) = "lastName"
which you can join to create a string:
vb Code:
dim fullName as string = string.join(" ", names)
+ add to your listbox:
vb Code:
listbox1.items.add(fullName)
then to create your invoice ref No.:
vb Code:
'names(names.getupperbound(0)) is the last element of the names() array
dim refString as string = getRefString(names(names.getupperbound(0)), "12345")
vb Code:
Private Function getRefString(ByVal lastName As String, ByVal zipCode As String) As String
Return lastName.Substring(0, 2) & zipCode.Substring(zipCode.Length - 4)
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 17th, 2011, 10:54 AM
#8
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|