Small problem using the Val function
Small problem using the Val function
Small problem using the Val function
Small problem using the Val function
Small problem using the Val function
Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function
Small problem using the Val function Small problem using the Val function
Small problem using the Val function
Go Back  Xtreme Visual Basic Talk > > > Small problem using the Val function


Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2009, 01:09 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default Small problem using the Val function


I have the program written and it is so close to functioning correctly, but a the end where I am trying to add the totals for the costs.

'Establish the total
Total = Val(lodging) + Val(regFee)

How do you check to see if the variable is empty and numeric?

I know the Val function is not the best to use, but in this tiny program, it should not affect anything.

Thanks! And I am glad to have found a site that is very active with helpful users, I hope to be around for awhile, and hopefully help many as I get better.
Reply With Quote
  #2  
Old 11-29-2009, 01:48 PM
AtmaWeapon's Avatar
AtmaWeaponSmall problem using the Val function AtmaWeapon is offline
Fabulous Florist

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

The Val() function is unable to do either of the functions you want. I hate it with a passion and evangelize against its use.

I'm going to assume that lodging and regFee are String variables. There's many way you can check for empty and numeric strings, but the easiest is also the one that's most useful.

First, we start with the Parse() method of every numeric data type (for example, there's an Integer.Parse() and a Double.Parse().) This method takes a string as a parameter and converts that string to the appropriate data type. If the conversion can't happen, an exception will be thrown.

Building off of that is the TryParse() method. It takes two parameters: the string to convert and a ByRef value of the appropriate data type. It also returns a Boolean that indicates if the conversion happened properly. Here's an example:
Code:
Dim lodgingValue As Double
If Integer.TryParse(lodging, lodgingValue) Then
    ' Parsing was successful, lodgingValue is set.
Else
    ' Parsing failed, lodgingValue is not set.
End If
The TryParse() methods will return false if the string is empty or not numeric.
__________________
.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
  #3  
Old 11-29-2009, 01:54 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

The assignment I am working on is to build a Workshop Selector that allows you to select a workshop, a location, display and add the cost in a label.

This is what I had at my first post.

Code:
Public Class Form1

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

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    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 = lstWorkshops.SelectedIndex()
        If lstWorkshops.SelectedItem Is Nothing Then
            MsgBox("Please make sure you select a workshop")
        ElseIf workshop = lstWorkshops.Items.Item(0) Then
            days = 3 And regFee = 595
        ElseIf workshop = lstWorkshops.Items.Item(1) Then
            days = 3 And regFee = 695
        ElseIf workshop = lstWorkshops.Items.Item(2) Then
            days = 3 And regFee = 995
        ElseIf workshop = lstWorkshops.Items.Item(3) Then
            days = 5 And regFee = 1295
        ElseIf workshop = lstWorkshops.Items.Item(4) Then
            days = 1 And regFee = 395
        End If
        'Location Configuration
        location = lstLocation.SelectedIndex()
        If lstLocation.SelectedItem Is Nothing Then
            MsgBox("Please make sure you select a location")
        ElseIf location = lstLocation.Items.Item(0) Then
            lodging = days * 95
        ElseIf location = lstLocation.Items.Item(1) Then
            lodging = days * 125
        ElseIf location = lstLocation.Items.Item(2) Then
            lodging = days * 110
        ElseIf location = lstLocation.Items.Item(3) Then
            lodging = days * 100
        ElseIf location = lstLocation.Items.Item(4) Then
            lodging = days * 92
        ElseIf location = lstLocation.Items.Item(5) Then
            lodging = days * 90
        End If
        'Establish the total
        Total = Val(lodging) + Val(regFee)
        'Transfering totals to lbCost
        lstCosts.Items.Add(Format$(Total, "currency"))
    End Sub

    Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs)

    End Sub
End Class
So I should change it to

Code:
 'Declare selected values from both listboxes
        Dim workshop, location, days, regFee, lodging As Double
        Dim Total As Integer
        'Workshop Configuration
        workshop = lstWorkshops.SelectedIndex()
        If lstWorkshops.SelectedItem Is Nothing Then
            MsgBox("Please make sure you select a workshop")
I am honestly try to grasp this, as I thought the Val function would be the quickest way, and am unfamiliar with the Parse function.
Reply With Quote
  #4  
Old 11-29-2009, 02:13 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I am also unsure if the Val function is causeing the problem as the error is

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

When the program gets to this point.

Code:
ElseIf workshop = lstWorkshops.Items.Item(0) Then
Reply With Quote
  #5  
Old 11-29-2009, 02:40 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

This code is in error here:
Code:
        workshop = lstWorkshops.SelectedIndex()
        If lstWorkshops.SelectedItem Is Nothing Then
First of all, why assign the selected index before you check to see if something was actually selected? It's OK in this case, since you won't get any errors, but, in the future, your error handling should seek to verify that no errors exist in your manipulated code before you assign it to anything that you intend to use.
Code:
        ElseIf workshop = lstWorkshops.Items.Item(0) Then
            days = 3 And regFee = 595
You assigned an index of an item for workshop... but now you're comparing the index to the contents inside the listbox. This is wrong.
If you had [abc, def, ghi] in the listbox and the user selected item 1 (def)...
you would be comparing 1 to abc, def, and ghi at this point. This is not going to work...
you need to compare 1 to 0, 1, and 2, the indices available in the listbox.
Thus, workshop = 0
workshop = 1
workshop = 2
should be your If statements.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #6  
Old 11-29-2009, 02:45 PM
EmptyVessel's Avatar
EmptyVessel EmptyVessel is offline
Junior Contributor
 
Join Date: Nov 2008
Posts: 315
Default

Perhaps I can save Atmaweapon some typing.

Looks like you do not have an understanding of the fundamental variable types.

All your variables are being declared as numeric.
Val() is a legacy VB6 function that converts a string variable to a numeric variable

Also your using the legacy vb6 Format() function instead of the native VB.NET .ToString() method.
__________________
-MT-
Reply With Quote
  #7  
Old 11-29-2009, 03:15 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

Here is the attached project folder.

I am unsure if I am making the program requirements clear, or it may be my lack of understanding this. I have completely read all of my chapters and your posts, but it is still unclear to me.

I am using VBExpress 2008

Quote:
You assigned an index of an item for workshop... but now you're comparing the index to the contents inside the listbox. This is wrong.
If you had [abc, def, ghi] in the listbox and the user selected item 1 (def)...
you would be comparing 1 to abc, def, and ghi at this point. This is not going to work...
you need to compare 1 to 0, 1, and 2, the indices available in the listbox.
I was trying to make it where if the first choice is selected, in this case, Handling Stress, then the value would be set to $595 and days set to three. Then if choice 1 was selected on location, Austin, it would take days and multiply it by 95 and display a dollar amount in the cost box. After finished picking all workshops and locations, the Calculate Total button would sum up the cost and move it to the total costs label.
Attached Files
File Type: zip Workshop Selector.zip (75.1 KB, 2 views)
Reply With Quote
  #8  
Old 11-29-2009, 04:06 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I have the program running now without errors by changing the way I declared variables.

Code:
'Declare selected values from both listboxes
        Dim workshop, location As String
        Dim lodging As Integer  'Cost per day for stay
        Dim days As Integer     'Number of days a workshop lasts
        Dim regFee As Integer   'Cost of registration fee for workshop
        Dim Total As Integer    'Cost of regFee + days x lodging cost
However, the calcuation is still not working as it shows $0.00 no matter what I choose.
Reply With Quote
  #9  
Old 11-29-2009, 04:32 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Oh, also:
days = 3 And regFee = 595
that's not going to work.
And is used if you want to verify two boolean conditions are met in an If or similar statement... not to make two assignments in one line.
Make those statements and similar ones into two statements on their own line.
Also, if you are still comparing the workshop to the items of the listbox, then none of the comparisons will work.
If you have Option Strict turned on, it will reveal invalid conversions that you are making.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #10  
Old 11-29-2009, 04:42 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I turned option strict on, but did not notice anything different.

I see what you mean about comparing workshop to the items, how do I go about correcting that, to make it where if 2 is selected it will move its value to the variable and move to location groupbox?

I also moved the calculations to separate lines. And understand what I was doing there.
Reply With Quote
  #11  
Old 11-29-2009, 04:49 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

I told you how to fix the workshop problem in my first post... but what do you mean by
"move its value to the variable and move to location groupbox"?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #12  
Old 11-29-2009, 05:02 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I believe I have part of it working now. However, if I click Add Workshop, for Handling Stress In Austin, it displays $595 and should display, 595 + (3*95)

Code:
'Workshop Configuration
        workshop = lstWorkshops.SelectedIndex()
        If lstWorkshops.SelectedItem Is Nothing Then
            MsgBox("Please make sure you select a workshop")
        ElseIf workshop = 0 Then
            days = 3
            regFee = 595
        ElseIf workshop = 1 Then
            days = 3
            regFee = 695
        ElseIf workshop = 2 Then
            days = 3
            regFee = 995
        ElseIf workshop = 3 Then
            days = 5
            regFee = 1295
        ElseIf workshop = 4 Then
            days = 1
            regFee = 395
        End If
Quote:
Originally Posted by Iceplug View Post
I told you how to fix the workshop problem in my first post... but what do you mean by
"move its value to the variable and move to location groupbox"?
I need it so when thte user clicks the Calculate Total button, the total costs from the Lists of Costs are calculated and displayed in the empty label. Also I need the reset button the clear the List of Costs as well.
Reply With Quote
  #13  
Old 11-29-2009, 05:09 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I fixed the calculation problem. Thanks Iceplug, after re-reading the first post you made and tinkering, I finally got it and feel ignorant for not catching on the first time.

Now on to this calculate and reset button...
Reply With Quote
  #14  
Old 11-29-2009, 05:12 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

I have the Clear Button functioning correctly with the Listbox.Items.Clear()

Now on to the Calculate button...
Reply With Quote
  #15  
Old 11-29-2009, 05:16 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Well, your value of total currenly does not depend on the value of 'days'
Total = Val(lodging) + Val(regFee)
And since lodging and regFee are not strings, Val() is unnecessary
now, you also need to add the days * 95
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #16  
Old 11-29-2009, 05:24 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

Quote:
Originally Posted by Iceplug View Post
Well, your value of total currenly does not depend on the value of 'days'
Total = Val(lodging) + Val(regFee)
And since lodging and regFee are not strings, Val() is unnecessary
now, you also need to add the days * 95
I removed the Val() as you said, it was unnecessary. But since lodging has already computed the total cost including cost per day * days, does it need to be added in under 'Establish the total.?

Also, when doing the calculate button, how can I sum the totals and display in the empty label?

I thought
lstCost.Items.xxxx(?)
Reply With Quote
  #17  
Old 11-29-2009, 05:46 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

Oh, I missed that. But, did you notice that you were doing the same thing with location that didn't work when you were doing it with workshop?
And what totals are you summing?
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #18  
Old 11-29-2009, 05:49 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

Yes I noticed that and fixed it already.

When I run the program the user chooses one workshop and one location, then clicks add workshop, which enters the cost for it into the List of costs. They can then choose more and build the list of costs. When they click calculate total, I need it to sum the list of costs and display it in the lblTotalCost.
Reply With Quote
  #19  
Old 11-29-2009, 05:59 PM
Iceplug's Avatar
IceplugSmall problem using the Val function Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

To total the costs in the listbox, you need to do a For Each Loop on all of the items in the listbox.Items with a String.
For Each S In yourlistbox.Items
Convert each string into a Double using Double.Parse() (if you have low decimal places appear in the result, try Single instead of Double)
and store the result in a Double variable.
Accumulate the totals into your Double variable with +=
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #20  
Old 11-29-2009, 06:29 PM
CtownIntegra CtownIntegra is offline
Newcomer
 
Join Date: Nov 2009
Location: Louisiana, USA
Posts: 18
Default

The book I am using does not discuss For Each Loop.

It does discuss using a lopp to keep a running total by adding intCount+= 1 at the end of the calculation.

How can I use that variable to sum the number of times the calculation was used to sum that number of items in the list?
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
Small problem using the Val function
Small problem using the Val function
Small problem using the Val function Small problem using the Val function
Small problem using the Val function
Small problem using the Val function
Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function Small problem using the Val function
Small problem using the Val function
Small problem using the Val function
 
Small problem using the Val function
Small problem using the Val function
 
-->