VB project help

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1467 Views - Last Post: 07 April 2013 - 06:35 PM Rate Topic: -----

#1 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

VB project help

Posted 05 April 2013 - 10:08 AM

Hope this is in the right section

Hello-

To start Im not asking to have my homework done for me, but to help point me in the right direction. I have only been in my programming class for 3 weeks and I'm still very fresh, so forgive me if I get some of the terminology wrong. Also, I have read another thread on this site where someone was asking the same question but I had a hard time following it.

I have a project where I have to take a name Smith, John and reverse it to John Smith and it needs to be carried out by a function procedures. I cant figure out how to do it. Is it a substring thing I have to do? Also I have to take the fist two letters of the first name and the last three digits of the zip code to make an invoice number. Does this also have to do with substrings?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: VB project help

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,808
  • Joined: 12-June 08

Re: VB project help

Posted 05 April 2013 - 10:11 AM

Moving to VB.NET section.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,026
  • Joined: 12-December 12

Re: VB project help

Posted 05 April 2013 - 10:19 AM

If "Smith, John" is a single string then you might use Split() to split it at the space, and possibly Replace() to remove the comma.

And, yes, Substring() for the other question.

http://msdn.microsof...#code-snippet-1
Was This Post Helpful? 0
  • +
  • -

#4 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 05 April 2013 - 10:26 AM

Good stuff...thanks for the lead
Was This Post Helpful? 0
  • +
  • -

#5 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 06 April 2013 - 08:46 PM

This is what I have been working on but i get an error saying array cannot be converted to string.But if I have x as a string i get an error saying 1 dimensional array of string cannot be converted to a string...im lost
Function NameSplit() As String
        Dim x, y, z As Array
        x = txtName.Text.Split(CChar(","))

        Return x

Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,091
  • Joined: 02-July 08

Re: VB project help

Posted 06 April 2013 - 09:48 PM

The () after a variable name means this datatype is an array of that type.
Also could be written Dim x As String()

Dim x() As String = txtName.Text.Split(","c)

x(0) = first element
x(1) = second element
No comma no worry about.
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: VB project help

Posted 06 April 2013 - 10:11 PM

The problem is that your Function says it returns a string, but you are trying to return an array of unspecifired type.
Try this...

    Function NameSplit() As String()
        Dim x(), y(), z() As String
        x = txtName.Text.Split(CChar(","))
        Return x
    End Function


This says that the Function NameSplit returns a String array.
x(), y() and z() are Dim'd here as arrays (the parentheses), of String (the As clause).
The Return statement returns a String array, as stated in the Function Definition.
Was This Post Helpful? 1
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,091
  • Joined: 02-July 08

Re: VB project help

Posted 06 April 2013 - 10:26 PM

Why are you creating the y and z arrays, they are not used?
Was This Post Helpful? 0
  • +
  • -

#9 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 06 April 2013 - 10:32 PM

la3ry...ok it worked with out error..However, when I put NameSplit() into the lst.Invoice.Items.Add is get a squiggly blue line, as if it were not recognized

View Post_HAWK_, on 06 April 2013 - 10:26 PM, said:

Why are you creating the y and z arrays, they are not used?


I haven't used them yet. I figured that y would be for the last name and z for the first name last name configuration

Thanks for the first reply,I tried what you said but I got the same errors

Im just testing x for now just to see if I can get the first name in the right place so I can got to bed :sleep1:
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: VB project help

Posted 06 April 2013 - 11:25 PM

View Postjbok, on 06 April 2013 - 11:32 PM, said:

la3ry...ok it worked with out error..However, when I put NameSplit() into the lst.Invoice.Items.Add is get a squiggly blue line, as if it were not recognized

It all depends on what you are trying to put into the control, and what type that control requires. If you show us the code that puts it in the control, we could help you better.

Click on the link in my signature abuut Option Strict. In there, you'll find out how to tell what the squiggly line means, and how to correct it.

Quote

View Post_HAWK_, on 06 April 2013 - 10:26 PM, said:

Why are you creating the y and z arrays, they are not used?

I haven't used them yet. I figured that y would be for the last name and z for the first name last name configuration

Well, first thing you should realize is that none of those three arrays will be visible outside the Function. They are Dim'd inside the function, and the only one you can use is the one you return.

I would suggest calling the Function something like fixName, and have it return the name in the format you want. So "Smith, John" will return "John Smith". If you want the first name or the last name separately later, you can always Split it using a space as a delimiter.

Additionally, if you only ever need to reformat a name in one place, say during a button subroutine, there's really no need for a Function. Functions are indeed handy, but they are best used for repetetive operations that are called from different subroutines or functions, and that will take arguments that will be inputs that the Function operates on.
Was This Post Helpful? 0
  • +
  • -

#11 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 07 April 2013 - 07:42 AM

View Postlar3ry, on 06 April 2013 - 11:25 PM, said:

View Postjbok, on 06 April 2013 - 11:32 PM, said:

la3ry...ok it worked with out error..However, when I put NameSplit() into the lst.Invoice.Items.Add is get a squiggly blue line, as if it were not recognized

It all depends on what you are trying to put into the control, and what type that control requires. If you show us the code that puts it in the control, we could help you better.

Click on the link in my signature abuut Option Strict. In there, you'll find out how to tell what the squiggly line means, and how to correct it.

Quote

View Post_HAWK_, on 06 April 2013 - 10:26 PM, said:

Why are you creating the y and z arrays, they are not used?

I haven't used them yet. I figured that y would be for the last name and z for the first name last name configuration

Well, first thing you should realize is that none of those three arrays will be visible outside the Function. They are Dim'd inside the function, and the only one you can use is the one you return.

I would suggest calling the Function something like fixName, and have it return the name in the format you want. So "Smith, John" will return "John Smith". If you want the first name or the last name separately later, you can always Split it using a space as a delimiter.

Additionally, if you only ever need to reformat a name in one place, say during a button subroutine, there's really no need for a Function. Functions are indeed handy, but they are best used for repetetive operations that are called from different subroutines or functions, and that will take arguments that will be inputs that the Function operates on.


Ok..I'll mess with it and see what happens. The reason im using a function is because my instructions say so. Thanks for you help again. Our instructor sucks, only checks in every few days and then doesn't even answer our questions. Your not just helping me but five other students:)
Was This Post Helpful? 0
  • +
  • -

#12 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 07 April 2013 - 04:07 PM

I found some helpful info in my text book. It's not exactly what what I need but I want to see if it worked and put the first name in the proper place in the list box. Im getting and error stating "make sure arguments have valid values" over the return at the bottom. What would make this a valid value?

'formula for output
        Dim chair As Double = 350
        Dim sofa As Double = 925
        Dim sales_tax As Double = 0.05
        Dim a, b, c, x As Double

        ' for name seperation
        Dim firstname As String
        firstname = txtName.Text
        txtName.Text = NameSplit(firstname)


        a = chair * CDbl(txtNumChair.Text)
        b = sofa * CDbl(txtNumSofa.Text)
        c = CDbl(sales_tax * (a + B)/>)
        x = a + b + c

        lstInvoice.Items.Clear()
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("Invoice Number:")
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("Name:" & NameSplit(firstname))
        lstInvoice.Items.Add("Address:" & vbTab & txtAddy.Text)
        lstInvoice.Items.Add("City:" & vbTab & txtCity.Text)
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("Number of Chairs:" & vbTab & txtNumChair.Text)
        lstInvoice.Items.Add("Number of Sofas:" & vbTab & txtNumSofa.Text)
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add("")
        lstInvoice.Items.Add(vbTab & "Cost:" & vbTab & FormatCurrency(a + B)/>)
        lstInvoice.Items.Add("Sales Tax:" & vbTab & Format(FormatCurrency(c)))
        lstInvoice.Items.Add(vbTab & vbTab & "----------------")
        lstInvoice.Items.Add("Total Cost:" & vbTab & FormatCurrency(x))

    End Sub
    Function NameSplit(ByVal firstname As String) As String

        Dim firstcomma As Integer

        firstcomma = txtName.Text.IndexOf(",")

        Return txtName.Text.Substring(0, firstcomma)

    End Function

Was This Post Helpful? 0
  • +
  • -

#13 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

Re: VB project help

Posted 07 April 2013 - 05:06 PM

Hi,
Your first call to the (NameSplit) is set up to set the text in (txtName) to the name without the comma and then when you call the function the second time when adding it in the listbox the (txtName) textbox does not have a comma for it to find. I am guessing that is why you get an error.

I would get rid of these lines if you don`t need to set the textbox to the first name.
        Dim firstname As String
        firstname = txtName.Text
        txtName.Text = NameSplit(firstname)


and change this line to send the text from the textbox to the function.
        lstInvoice.Items.Add("Name:" & NameSplit(txtName.Text))


Your function should be using the variable (firstname) to get the substring not the textbox text.
    Function NameSplit(ByVal firstname As String) As String

        Dim firstcomma As Integer

        firstcomma = txtName.Text.IndexOf(",")

        Return firstname.Substring(0, firstcomma)

    End Function


Was This Post Helpful? 0
  • +
  • -

#14 jbok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 03-April 13

Re: VB project help

Posted 07 April 2013 - 05:24 PM

View PostIronRazer, on 07 April 2013 - 05:06 PM, said:

Hi,
Your first call to the (NameSplit) is set up to set the text in (txtName) to the name without the comma and then when you call the function the second time when adding it in the listbox the (txtName) textbox does not have a comma for it to find. I am guessing that is why you get an error.

I would get rid of these lines if you don`t need to set the textbox to the first name.
        Dim firstname As String
        firstname = txtName.Text
        txtName.Text = NameSplit(firstname)


and change this line to send the text from the textbox to the function.
        lstInvoice.Items.Add("Name:" & NameSplit(txtName.Text))


Your function should be using the variable (firstname) to get the substring not the textbox text.
    Function NameSplit(ByVal firstname As String) As String


I did what you said I got some results  :bananaman:/> now I just have to refine it a bit. Thanks!

        Dim firstcomma As Integer

        firstcomma = txtName.Text.IndexOf(",")

        Return firstname.Substring(0, firstcomma)

    End Function


Was This Post Helpful? 0
  • +
  • -

#15 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: VB project help

Posted 07 April 2013 - 05:27 PM

View Postjbok, on 07 April 2013 - 05:07 PM, said:

I found some helpful info in my text book. It's not exactly what what I need but I want to see if it worked and put the first name in the proper place in the list box. Im getting and error stating "make sure arguments have valid values" over the return at the bottom. What would make this a valid value?

Well, I've never had that error show up, but I can see that you don't really have a good grip on the way a Function works. Basically, you pass something to a Function, and assign the returned value to something. You have the call, and it's good. You pass firstname to the Function, and assign its return value to txtName.Text, which is fine.

However, within the Function, you manipulate the string you passed, then return a SubString of txyName.Text, which, while it might work, is not really the way to go about it.

First, you don't need to assign txtNamt.Text to firstname in order to send it to the Function. Your call could be

firstname = NameSplit(txtName.Text)

Then, in the Function (I have renamed the variable to clarify something for you; thatthe variable name within the Function is not a variable name outside the Function)...

    Function NameSplit(ByVal wholename As String) As String
        Dim firstcomma As Integer
        firstcomma = wholename.IndexOf(",")
        Return wholename.Substring(firstcomma + 1)
    End Function


Now, you are passing the complete name as a String. txtName.Text is a String, and in the Function, wholename is also a String (declared in the parameters of the Function), and in addition, it is well worth noting that it is a COPY of the String in txtName.Text (That's what ByVal means.. the value of the argument, rather than the argument itself.

Now all that's left is to return something, and from your variable name, I gather you want that to be the first name, extracted from txtName.Text, You want to return the SubString starting at firstcomma + 1, otherwise you get the comma returned as well.

Give that a try and see if it clears up your error.

Are you using Option Strict On?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2