Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 7th, 2008, 07:00 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please help the newbie!!

I am new to .net and needing to complete and assignment. I am having issues formatting results. here is the code I have input so far:


Public Class frmMain

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click



        Dim payrate As Decimal
        Dim grosspay As Decimal
        Dim federaltax As Decimal
        Dim sstax As Decimal
        Dim statetax As Decimal
        Dim netpay As Decimal





        TextBox3.Text = payrate.ToString("C2")
        Label9.Text = grosspay.ToString("C3")
        Label10.Text = federaltax.ToString("P0")
        Label11.Text = sstax.ToString("P0")
        Label12.Text = statetax.ToString("P0")
        Label13.Text = netpay.ToString("C3")


    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

        Label9.Text = (Val(TextBox2.Text)) * (Val(TextBox3.Text))grosspay.ToString("C3")
        Label10.Text = (Val(Label9.Text)) * (0.2)
        Label11.Text = (Val(Label9.Text)) * (0.08)
        Label12.Text = (Val(Label9.Text)) * (0.02)
        Label13.Text = (Val(Label9.Text)) - (Val(Label10.Text)) - (Val(Label11.Text)) - (Val(Label12.Text))

    End Sub
End Class

I have been trying different orders etc. but I keep getting all 0's when I click the "Calculate" button.

Can anyone help, I really want to learn what I need to do...I have more like this to complete tonight.

Thanks for the help!!!


Shelly
 
Old October 7th, 2008, 08:42 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Have you thought about changing the NAMES of all those silly numbered labels, so that they reflect their actual contents???

YOu can do that all in VS by just clicking on the label in the designer and then changing its name in the properties.

Such as change Label9 to LabelGrossPay or GrossPayLabel???

Anyway...

It's possible that your root problem is that once you have formatted a value into a string in the label, the VAL( ) function is unable to convert it back to a number.

In other words, the string in that label now is something like "$53.95" and VAL( ) sees the "$" and chokes.

You could easily check on this with a tiny bit of debugging. If you haven't learned how to use the VS debugger, now's the time to start.

Not to ask a dumb question, but what's the point of doing things like
    TextBox3.Text = payrate.ToString("C2")
in your Button2_Click sub???

Since you never give any value to payrate, it's guaranteed to be zero, so you could accomplish the exact same thing by code such as:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox3.Text = "$0.00"
        Label9.Text = "$0.000"
        Label10.Text = "0%"
        Label11.Text = "0%"
        Label12.Text = "0%"
        Label13.Text = "$0.00"
End Sub

Anyway, go change all your label and textbox names to *READABLE* and meaningful names (if I were your instructor, I'd mark you off for using the names as-is). And figure out if VAL( ) is indeed the culprit, being unable to accept a "$" in the string value it is supposed to evaluate.

I'd also recommend not using VAL( ). It's a very very old fashioned way to code. Look into the various kinds of conversions that the .NET framework supports, instead.
 
Old December 16th, 2008, 07:35 AM
Authorized User
 
Join Date: Dec 2008
Location: Keyport, NJ 07735
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Tip quoated from Professional Visual Basic 2008: "For a best practice, always set a variable equal to something when it is declared."

1.) Any value in double quotes is a literal value.
2.) Any object not set to a value = Nothing.

In your code you have:
Code:
Dim payrate As Decimal
If you proceed that line of code with something like:
Code:
MsgBox(payrate.ToString)
You would see that the result is 0 because payrate was never set to any value.

BUT IN YOUR CODE YOU HAVE:
Code:
TextBox3.Text = payrate.ToString("C2")
Are you trying to write a string of the value of payrate or "C2"?
If you want the value of payrate converted to a string then just use payrate.ToString
If you want the value "C2" converted to a string then just use ToString("C2")

NOTE: payrate is of type decimal and "C2" is not a decimal value so payrate will never be "C2"

If C2 is a variable or any other object in your application other then the literal string "C2" then you need to remove the double quotes which would equate to:

Code:
variable1.ToString(Variable2)
and the result would just ignore the variable1 part of the statement and return the string value of variable2 !!!

I hope this helps and I didn't confuse you.
__________________
Need free one on one help getting started with Visual Basic? Contact me via http://patrickcambria.com its free.

Last edited by Patrick Cambria; December 16th, 2008 at 07:41 AM..




Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie help... mkruger XSLT 7 October 24th, 2007 02:33 AM
Newbie here jmac731976 HTML Code Clinic 13 August 29th, 2007 03:54 PM
Newbie please help indyanguy XSLT 1 September 2nd, 2005 09:18 AM
Newbie Help! TheShadow Javascript 1 March 21st, 2005 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.