Visual Basic... Is this possible?

Recommended Posts

DJS2    142
Hello, Im new to visual basic and am try to do something but im not even sure if its possible,
        Dim n As Integer = lbBasket.SelectedIndex
Dim name As String = lbBasket.Items(n)
Dim length As Integer = name.Length()
Dim m As Integer = name.IndexOf("$") Dim TotalBill As String TotalBill = name.Substring(m, length - (m + 1)) MessageBox.Show(TotalBill)  Pretty much I have a listbox and want to compute a value from each item and output a total. Is this even possible? As of now I am outputting 1 at a time (as long as the item is selected) but is there anyway to compute all the items without any even being selected? Thanks for any help. Share this post Link to post Share on other sites Omid Ghavami 1007 You can use a foreach on the items of the listbox. Something like:  For Each item As ListItem In lbBasket.Items ' Code to execute for each item, you may use the variable item to refer to the current item Next item Share this post Link to post Share on other sites DJS2 142 Thank you very much for your help and reply.  Dim n As Integer = lbBasket.SelectedIndexDim name As String = lbBasket.Items(n)Dim length As Integer = name.Length()Dim m As Integer = name.IndexOf("$")Dim TotalBill As StringFor Each item As String In lbBasket.Items TotalBill = name.Substring(m, length - (m + 1))MessageBox.Show(TotalBill)        Next item

Something like that? It seems like everything I try I get an error saying "value of -1 is not valid for index." It works just fine when I select an item one at a time though?

Share on other sites
Omid Ghavami    1007
You want something like this:

For Each item As String In lbBasket.Items    Dim length As Integer = item.Length()    Dim m As Integer = item.IndexOf("$") Dim TotalBill As String TotalBill = item.Substring(m, length - (m + 1)) MessageBox.Show(TotalBill)Next item This should open a message box containing the total bill for each item in the list, in a sequence. Share this post Link to post Share on other sites DJS2 142 Thank you so much, but i just have one last question! :) In Visual basic does 'for each' work just like a c++ for loop? the reason why I ask is because If I want to store information in a temp variable and then have it add each "cycle" I don't know if it will let me do that, at least I havnt figured it out yet.  public Total as double For Each item As String In lbBasket.ItemsDim length As Integer = item.Length()Dim m As Integer = item.IndexOf("$")Dim TotalBill As StringTotalBill = item.Substring(m, length - (m + 1))Total = Val(TotalBill)Total += TotalNext itemMessageBox.Show(Total)

I have no idea if that works in VB, Im just trying to "transfer" what I know from C++?

Share on other sites
Omid Ghavami    1007
Quote:
 In Visual basic does 'for each' work just like a c++ for loop?

Almost, VB has an indexed for loop which is equivalent to the C++ for loop, it's just called For. ForEach is slightly different, it doesn't tell you where in the list you currently are, it just feeds you the 'next' item in the list until all items have been processed.

As for adding the values you can do something like this:

        Dim Total As Double = 0        For Each item As String In ListBox1.Items()            Dim length As Integer = item.Length()            Dim m As Integer = item.IndexOf("$") Dim TotalBill As String ' Note that I changed startingIndex to m + 1, since you can't convert a string containing '$' to an Integer.            TotalBill = item.Substring(m + 1, length - (m + 1))            Total += TotalBill        Next item        MessageBox.Show(Total)

Share on other sites
rk_theone    100
yeah, for each works pretty much like loops ,
one thing more... u can't do += or -= or ++ in vb.

Share on other sites
id0001    122
you can do +=, -=, *=, /=
but not ++

Share on other sites
Omid Ghavami    1007
Quote:
 Original post by rk_theoneyeah, for each works pretty much like loops ,one thing more... u can't do += or -= or ++ in vb.

You can't do ++ or --, but += and -= works in VB.NET [wink]

EDIT: Too slow :P

Share on other sites
benryves    1999
You are using a hard-coded search for a dollar-sign, which can (and will) cause problems if people that do not use dollars use the software. (I usually run into the opposite problem, with software hard-coded to use pound signs breaking when the user has their PC set up incorrectly to the US locale).

You can parse a string with a currency sign in it into a Decimal via Decimal.Parse - for example:

Dim Amount As Decimal = Decimal.Parse("\$1.50", NumberStyles.Currency)

(As a Brit, the above throws an exception for me - I'd need to change that to "£1.50"). [smile]

Now you have that, you might want to convert it back into a string that represents that currency. This can be done with format strings, for example:

Console.WriteLine(Amount.ToString("C"))' or, alternatively:Console.WriteLine("The amount is {0:C}.", Amount)