CDbl() Vs Val() in reading double values from a text box - vb.net

I have a TextBox named ValueText. when i execute the following code:
Dim txtval As Double = 0.0
txtval = Val(ValueText.Text)
txtval = CDbl(ValueText.Text)
Both Val() and CDbl() will do the same work. then what is the difference between these two? where to use Val() and where to use CDbl()

Two subtle differences between the functions:
CDbl("") throws a RTE13 "Type Mismatch" error on a blank/empty string where Val("") returns 0.
CDbl() allows commas as the thousands separator in strings where Val() does not. i.e. CDbl("-1,234.56") = -1234.56 but Val("-1,234.56") = -1

the val() Returns the numbers contained in a string as a numeric value of appropriate type. where as CDbl() will Returns an expression that has been converted to a Variant of subtype Double.
The main difference in its use is that
CDbl() will return Error as Conversion from string "" to type 'Double' is not valid. when The TextBox is blank or Empty.
While Val() will give 0.0 in the above situation

Related

How do you designate a Currency literal in VBA

If I wanted to define a Date literal, I would write the following:
Dim myDate as Date = #1/1/2021#
What character should I use to do an analogous thing for a Currency variable?
That is
Dim amt as Currency = ?200.00?
What character should "?" be when using VBA?
In VBA a declaration cannot legally be joined with an assignment in the same instruction, so that would be:
Dim myDate As Date
myDate = #1/1/2021#
For the sake of this discussion it's important to differentiate the variable from the literal value, because the declared type of the variable is irrelevant to the type of the literal - this is perfectly confusing, but perfectly legal too:
Dim myDate As Long
myDate = #1/1/2021#
We're talking about the literals here.
A numeric literal is an Integer literal if it's between -32767 and 32767, a Long literal if it's between -2147483647 and 2147483647, and a Double otherwise (the VBE automatically adds a # type hint then).
That means the 200 in myAmount = 200 is an Integer literal, and if we typed 200.00 the VBE would "prettify" it to 200# (making it a Double literal).
The type hint character to use for a Currency literal, is #, so the 200 in myAmount = 200# is a Currency value that the VBA compiler understands as such.
It doesn't matter though, because VBA will do lots of widening (narrowing too) implicit type conversions for us: if we assign an Integer literal to a Currency variable, the Integer value is converted to "fit" the reserved Currency "bucket". So if we declared myAmount As Currency and then assigned it to the literal value 200 (an Integer literal), if we asked VBA what type myAmount is, we would get the declared type - Currency.
In other words it's perfectly legal to assign an Integer or a Double literal to a Currency variable, and that makes type-hinted literals somewhat awkward beasts that don't really belong, in the vast majority of situations:
Dim myAmount As Currency
myAmount = 200 'integer literal
Debug.Print TypeName(myAmount) 'currency
myAmount = 200# 'double literal
Debug.Print TypeName(myAmount) 'currency
myAmount = 200# 'currency literal
Debug.Print TypeName(myAmount) 'currency
Things get groovy when we drop the declared type and use a Variant instead (implicit or not):
Dim myAmount 'As Variant
myAmount = 200 'integer literal
Debug.Print TypeName(myAmount) 'integer
myAmount = 200# 'double literal
Debug.Print TypeName(myAmount) 'double
myAmount = 200# 'currency literal
Debug.Print TypeName(myAmount) 'currency
A date literal needs # "delimiters" on both sides, because the / date separator character has another grammatical meaning in VBA (and many other languages too): in other contexts, it's the division operator: in a sense, # delimiters are more closely related to the " double quotes that delimit string literals, than they are to # type hints.
Here is a list of VBA type-declaration characters I collected:
/ means no type-declaration character exists for that datatype.
Also no type-declaration character exists for any composite data types such as arrays or structures.
Type
Type-declaration character
Boolean
/
Byte
/
Currency
#
Date
/
Decimal
/
Double
#
Integer
%
Long
&
LongLong (64-bit VBA only)
^
Object
/
Single
!
String
$
You can prove that for example in die Immediate-Window:
?TypeName(1%) 'Returns Integer
?TypeName(1&) 'Returns Long
?TypeName(1#) 'Returns Currency
?TypeName(1#) 'Returns Double
?TypeName(1^) 'Returns LongLong
?TypeName(1!) 'Returns Single

What does concatenating an empty string to a string do?

I've come across this convention in some code I'm editing where they frequently concatenate an empty string to an existing string when assigning it to another variable.
For example:
Dim var1
Dim var2 = "string"
var1 = var2 & ""
I can't think of a reason why they would do this since & already makes the result a string. Is there any reason why this is done?
I can't think of a reason to do it exactly as you show, but this is a pattern you sometimes see used to coerce something into a string type - for example when working with database recordsets:
blah = rs.Fields("foo").Value & ""
where there's a chance that the field value may be null and you just want an empty string instead.
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/ampersand-operator
If an expression is not a string, it is converted to a String variant.
The data type of result is String if both expressions are string
expressions; otherwise, result is a String variant.
If both expressions are Null, result is Null. However, if only one
expression is Null, that expression is treated as a zero-length string
("") when concatenated with the other expression. Any expression that
is Empty is also treated as a zero-length string.

converting integer to decimal values while adding both

I am working on a vb.net application.
My code is the following:
txtTotalPrevious.Text = Val(txtTotalPrevious.Text) + Val(txtoldtotal.text)
my values are:
txtTotalPrevious.Text=187.0000
txtoldtotal=3
I get the result
result =190
but my expected result is:
result=190.0000
What is the issue in my logic?
You can use String.Format if you want to output string be like you want:
Dim str1 As String = "187.0000"
Dim str2 As String = "3"
Dim resultString As String = String.Format("{0:0.0000}", Val(str1) + Val(str2))
' And the result will be 190.0000
And if you want the double result:
Dim resultDouble as Double= Val(str1) + Val(str2)
' And the result will be Double 190
EDIT
Based on varocarbas comments you should consider some notes:
Avoid using Val method
The Val function stops reading the string at the first character it
cannot recognize as part of a number. Symbols and characters that are
often considered parts of numeric values, such as dollar signs and
commas, are not recognized. However, the function recognizes the radix
prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and
linefeed characters are stripped from the argument.
The following call
returns the value 1615198.
Val(" 1615 198th Street N.E.")
The Val function recognizes only the period (.) as a valid decimal
separator. When different decimal separators are used, as in
international applications, use CDbl or CInt instead to convert a
string to a number. To convert the string representation of a number
in a particular culture to a numeric value, use the numeric type's
Parse(String, IFormatProvider) method. For example, use Double.Parse
when converting a string to a Double.
As a VB.Net Programmer use & instead of + for string concatenation
When you use the + operator, you might not be able to determine
whether addition or string concatenation will occur. Use the &
operator for concatenation to eliminate ambiguity and to provide
self-documenting code.

Visual Basic format number to hower many number on left of decimal and 1 decimal without rounding

I am acutally using SSRS but it is for an expression so this is VB code. I am wondering how I would get a number such as 236.4723423 to appear at 236.4 instead of 236.5, so basically I jsut want to truncate it always after 1 decimal.
I tried Format = "N1" this rounds it
I tried Formate = "#######.0" and "######.#" and this rounds it as well.
Any ideas?
value = Math.Floor(value * 10) / 10
Use the format "########.00", then once it's in string form, trim the last char.
Edit:
Dim myString as String
myString = CStr(FORMAT(((SUM(Fields!Shipment_Weight.Value)) / 2000),"######.00"))
myString = myString.Substring(0, myString.Length - 1) & "T"
You will probably want to implement a custom IFormatProvider interface to do this. There is a great example on MSDN here.

String.ToCharArray() stored in StringCollection conversion to numbers and back in VB.net

I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?
I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.
Is there already a method built into vb.net for doing this?
Thanks for the help!
The Convert class has methods that can convert between characters and integers:
Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)
To loop the values converted from the characters in a string into an array of integers:
Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
codes(i) = Convert.ToInt32(theString.Chars(i))
Next
Try something like this:
For Each c As Char In yourString
Dim i As Int32 = DirectCast(c, Int32)
Next
Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).