Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > A bit Confused. small program ?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-08-2008, 03:21 PM
rollingdice rollingdice is offline
Newcomer
 
Join Date: Nov 2008
Posts: 13
Default A bit Confused. small program ?


I'm doing extra credit in my vb class and i've merely finished the program but for some reason there's something missing but I don't know what i'm doing wrong.

THe program is suppose to allow someone to choose a workshop from the "workshop listbox", and a location from the "location list box". You'll understand it alot more if you just download it i've attached it so you can understand what i'm trying to do easier. Anyways, here's my code. I'm not asking for the answer i'm asking for a hint in the right direction.
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
lbWork.SetSelected(0, False)
lbLoc.SetSelected(0, False)
lblTotalCost.Text = ""
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'Declare selected values from both listboxes
Dim workshop, location, days, regFee, lodging As Integer
Dim Total As Integer
'Workshop Configuration
workshop = lbWork.SelectedIndex()
If lbWork.SelectedItem Is Nothing Then
MsgBox("Please make sure you select a workshop")
ElseIf workshop = lbWork.Items.Item(0) Then
days = 3 And regFee = 595
ElseIf workshop = lbWork.Items.Item(1) Then
days = 3 And regFee = 695
ElseIf workshop = lbWork.Items.Item(2) Then
days = 3 And regFee = 995
ElseIf workshop = lbWork.Items.Item(3) Then
days = 5 And regFee = 1295
ElseIf workshop = lbWork.Items.Item(4) Then
days = 1 And regFee = 395
End If
'Location Configuration
location = lbLoc.SelectedIndex()
If lbLoc.SelectedItem Is Nothing Then
MsgBox("Please make sure you select a workshop")
ElseIf location = lbLoc.Items.Item(0) Then
lodging = days * 95
ElseIf location = lbLoc.Items.Item(1) Then
lodging = days * 125
ElseIf location = lbLoc.Items.Item(2) Then
lodging = days * 110
ElseIf location = lbLoc.Items.Item(3) Then
lodging = days * 100
ElseIf location = lbLoc.Items.Item(4) Then
lodging = days * 92
ElseIf location = lbLoc.Items.Item(5) Then
lodging = days * 90
End If
'Establish the total

Total = Val(lodging) + Val(regFee)
'Transfering totals to lbCost
lbCost.Items.Add(Format$(Total, "currency"))
End Sub
End Class
Attached Files
File Type: zip Workshop Selector.zip (30.5 KB, 85 views)
Reply With Quote
  #2  
Old 11-09-2008, 08:46 AM
shawnplr shawnplr is offline
Newcomer
 
Join Date: Nov 2008
Posts: 9
Default

I hope this helps.

Code:
        Dim workshop As String = lbWork.SelectedIndex()
        If lbWork.SelectedItem Is Nothing Then
            MsgBox("Please make sure you select a workshop")
        ElseIf workshop = "Handling Stress" Then
            days = 3 And regFee = 595

        Dim Location As String = lbLoc.SelectedIndex()
        If lbLoc.SelectedItem Is Nothing Then
            'Fixed this line as well you had workshop again instead of location
            MsgBox("Please make sure you select a location")
        ElseIf location = "Auston" Then
            lodging = days * 95

Last edited by shawnplr; 11-09-2008 at 08:50 AM. Reason: typo
Reply With Quote
  #3  
Old 11-09-2008, 08:48 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,505
Default

You didn't ask a question; you just said there was something missing and you wanted help. Unfortunately, it could be you got the application as-is (I hope not, because anyone using Val probably isn't qualified to teach VB .NET) and need to fill in the blanks.

Perhaps if you could ask a more specific question than "What's missing" we could help.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 11-11-2008, 09:58 AM
rollingdice rollingdice is offline
Newcomer
 
Join Date: Nov 2008
Posts: 13
Default

actually I used a case statement to gather which item is selected in a list. Now i'm just working on storing the listbox as an array.

Whats wrong with using the val() function.
Reply With Quote
  #5  
Old 11-11-2008, 11:33 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,505
Default

You're trying to convert a string to an integer. Presumably you want to ensure that the text is actually a number before converting it; it might not matter on this project, but some day in the future it will. Here's my list of problems with Val:
  • Val("38sldmejflemd") is an error in my eyes since it's not a valid number, yet it returns 83.
  • Val only returns Double, which means if you want an integer you need to do a type conversion before you use it.
  • Val ignores whitespace and formatting rules. Val("123 45th street") returns 12,345. Val("12,345") returns 12.
  • Live in a country that uses different symbols for a decimal point? (Some European countries use ",", so "three and a half" would be "3,5".) Tough luck, Val can't help you.
  • Error checking is practically impossible. Val("asdf") returns 0. What if 0 is a valid value? How can you tell the difference between when the user inputs "0" and when the user inputs "asdf"?

On the other hand, you could use the .NET Parse methods to avoid allof these problems. For example:
  • Dim value As Integer = Integer.Parse("38asldfineoasl") throws an exception; you could alternatively use TryParse to determine that the parsing failed. This means you know when the user put the wrong input into your application and you can display an appropriate error message.
  • You can choose the data type you want to avoid an extra type conversion.
  • Formatting happens according to the local culture and optional formatting rules you indicate. "123 45th street" will throw an exception, but "12,345" can be parsed as 12345:
    Code:
            Dim value As Integer
            value = Integer.Parse("12,345", Globalization.NumberStyles.Number)
  • .NET formatting methods take an optional culture that specifies the formatting rules that should be used. If you need to localize to a culture that uses a different decimal point, just use the current culture:
    Code:
            Dim value As Double
            Dim cInfo As New CultureInfo("FR-fr") ' Use French culture rules for formatting
            value = Double.Parse("12,345", Globalization.NumberStyles.Number, cInfo)
    Now the value is parsed as twelve and three hundred forty five thousandths, rather than twelve thousand three hundred forty five.
  • Using Parse means invalid values throw an exception. Using TryParse means the function returns false. This means you can add error handling to tell the user what they did wrong:
    Code:
        Private Sub Calculate()
            Dim value As Integer
    
            If Not Integer.TryParse(TextBox1.Text, value) Then
                MessageBox.Show("The input must be an integer.")
                Return
            End If
    
            ' do calculations
        End Sub

The .NET formatting and parsing methods are more powerful, flexible, and intuitive than Val.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 11-11-2008, 12:08 PM
rollingdice rollingdice is offline
Newcomer
 
Join Date: Nov 2008
Posts: 13
Default

Wow, Thank you for that last post.
I'm currently just starting to learn VB from my professor and I showed him what you posted and he explained a bit more to me
He said whenever I use the Val() statement we do two things
Check if it's empty first if variable = "", and then check to see if its numeric IE. IsNumeric(). That way I won't have to worry about parsing strings and such... for at least this project.

Though, I really like the time you put into explaining that for me because i can use all the constructive criticism i need.

I finished the program though, I guess a listbox is already an array and you can use a for loop to add the items of a listbox.

Thank you for the posts though i appreciate it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->