two questions

kevorski

New member
Joined
Mar 17, 2009
Messages
2
Programming Experience
Beginner
First: Ok so I'm trying to do a project in my beginning VB .Net class. We are supposed to be taking information from 5 different sections of a form and store them into an array and then call upon them later. We have Name, Address, City,State, and Zip. How would I go about saving the information into an array and then calling upon it later through File IO?

Second: I'm having a problem converting my string into a decimal. Whenever I calculate the price of the order I get 3-4 decimal places. Here is a little snippet of my code for the this:


Case "AK"
PriceWithoutTax = ((Val(txtRegular.Text * 8.5) + (Val(txtDecaf.Text) * 10.5)))
lblSalesTax.Text = ((Val(PriceWithoutTax * 0.0)))
lblSalePrice.Text = PriceWithoutTax
lblTotalPrice.Text = (Val(PriceWithoutTax * 0.0) + (Val(PriceWithoutTax)))

I have dimmed pricewithout tax as a decimal and stateselect as a string. The stateselect is based off a drop down list.

Any help would be greatly appreciated!
 
First, i'd use a datatable; not only are they designed for storing different types of data alongside each other, but they can read and write themselves to/from disk with teh ReadXML and WriteXML commands

Second, er.. yes.. Decimal values can indeed have more than 2 decimal places. For example 1.25 divided by 2 is 0.625. Are you expecting VB to be able to detect youre talking about money, and keep them to 2 dp? This can also cause lots of rounding errors! I'd suggest that you round them just before you display them, and always keep them in the most precise form possible
 
I agree that a datatable & XML file would be more efficient but for a class project he/she might not have a choice.

Also your conversions are off
PriceWithoutTax = ((Val(txtRegular.Text * 8.5) + (Val(txtDecaf.Text) * 10.5)))

The above converts the total of (txtRegular.Text * 8.5) to a numeric value after you already have done a math calculation using text.

Also since variable prefixes werent used, I cant tell what datatype PriceWithoutTax is supposed to be. As CJ has said, a decimal can have more then 2 places past the decimal point but you can round this in a decimal value or format it for text display.

VB.NET:
Dim strPriceWithoutTax As String = ""
strPriceWithoutTax = FormatNumber[COLOR="SeaGreen"]([/COLOR][COLOR="Blue"]([/COLOR]Val[COLOR="red"]([/COLOR]txtRegular.Text[COLOR="red"])[/COLOR] * 8.5[COLOR="blue"])[/COLOR] + _
                                  [COLOR="blue"]([/COLOR]Val[COLOR="Red"]([/COLOR]txtDecaf.Text[COLOR="red"])[/COLOR] * 10.5[COLOR="blue"])[/COLOR], 2[COLOR="seagreen"])[/COLOR]

Dim decPriceWithoutTax As Decimal = 0
decPriceWithoutTax = Decimal.Round[COLOR="Orange"]([/COLOR]CDec[COLOR="SeaGreen"]([/COLOR][COLOR="Blue"]([/COLOR]Val[COLOR="Red"]([/COLOR]txtRegular.Text[COLOR="red"])[/COLOR] * 8.5[COLOR="Blue"])[/COLOR] + _
                                        [COLOR="Blue"]([/COLOR]Val[COLOR="red"]([/COLOR]txtDecaf.Text[COLOR="red"])[/COLOR] * 10.5[COLOR="blue"])[/COLOR][COLOR="seagreen"])[/COLOR], 2[COLOR="orange"])[/COLOR]
 
so if i use cdec on before the calculations it should work correct? i will have to try that in a few

Well the point being you first have to convert the text in the textbox to a numeric value before using it in a numeric calculation. You can decide wether that value would be a int, decimal, double etc or use a generic conversion such as the Val() function.

Since both the calculations could possibly have returned a double, I used the CDec() to include the total results and ensure the total is a decimal value. The Decimal.Round() was then used to round it two places past the decimal point.

The Val function is a bit outdated and I would also suggest a more updated method be chosen for conversions. The Decimal.TryParse takes two parameters and returns a boolean value, so personal preference i'd rather use the CDec method.
 
Reason for using TryParse instead of CInt, CDec, CDbl, etc:
VB.NET:
Dim TestString As String = "Something A User Inputted"

Dim NeededDecimalFromUser As Decimal = CDec(TestString) 'Entire app just crashed and user has to completely restart the app
To get around that you'd have to use even more computer resources and slow the conversion even more:
VB.NET:
Dim TestString As String = "Something A User Inputted"
Try
    Dim NeededDecimalFromUser As Decimal = CDec(TestString)
Catch Ex As Exception
  'Just failed, now what??
  'Oh and the user just sat there for a few seconds while the exception was generated from the call stack
  'what a waste of time and cpu cycles
End Try
And of course I'd like a smooth way to do this conversion with no Try/Catch blocks needed and no app crashing:
VB.NET:
Dim TestString As String = "Something A User Inputted"

Dim NeededDecimalFromUser As Decimal = 0
If Decimal.TryParse(TestString, NeededDecimalFromUser) = False Then
    'make a call to another sub that'll get the value or will continue with the program flow
End If
Why would anyone not use TryParse when getting data from a textbox from the user? I also recommend using a NumericUpDown when getting numeric input from the user too, a Textbox is pointless for numeric input
 
Why would anyone not use TryParse
MaskedTextBox?

slow the conversion
of user input? How fast can your users type? 5 MChars/s?

The best thing (imho) would be regexing (or whatever one likes) the input in textbox_changed event and simply remove any illegal char from the user input. Why should a user allowed to enter '1a467!9' in a field that only should return a (decimal, int, float) number?
 
MaskedTextBox?
Which is good for things like phone numbers or other input where there's fixed chars in the string
of user input? How fast can your users type? 5 MChars/s?
Since the conversion happens after the input's has already been retrieved I don't see the relevance of the overhead of using Try/Catch blocks vs typing speed.
The best thing (imho) would be regexing (or whatever one likes) the input in textbox_changed event and simply remove any illegal char from the user input. Why should a user allowed to enter '1a467!9' in a field that only should return a (decimal, int, float) number?
So you'd rather maintain code to capture and analyze user's input as they type things into the TextBox when the NumericUpDown already does that for you? Talk about raising development cost(s).

Regex is a great tool, however I wouldn't recommend using it in this case since it adds to the initial development time and adds unnecessary code they may need to be maintained in the long run.
 
The best thing (imho) would be regexing (or whatever one likes) the input in textbox_changed

No, really.. the best thing would be to use a numeric up down. It only accepts numbers, so you just drop it on the form and away you go, no dicking about with regexes, event handlers etc.

It's called making your life easier :)
 
Back
Top