Public Sub MyFunction()
Dim lowstring As String
lowstring = "hi"
Me.RichTextView.Find(lowstring, 0, 2)
End Sub
The above produces the error of
Overload resolution failed because no accessible 'Find' can be called without a narrowing conversion:
Public Function Find(characterSet() As Char,
start As Integer, end As Integer) As Integer:
Argument matching parameter 'characterSet' narrows
from 'String' to '1-dimensional array of Char'.
Public Function Find(str As String, start As Integer,
options As System.Windows.Forms.RichTextBoxFinds) As Integer:
Argument matching parameter 'options' narrows from 'Integer' to
'System.Windows.Forms.RichTextBoxFinds'.
The error doesn't occur if you change the replacement string value, only if you change the second or third values to something other than 0.
Why doesn't the use of standard integers work here? What does this error really mean? Can anyone point me to some documentation for handling overloaded functions in vb.net (2010)?
I hope this question is focused enough... I've just been pretty confused about this one.
Thanks for any help - EB
As you can see, RichTextBox.Find has 7 overloads.
The one you're calling with 3 arguments and two integers takes a Char[] as first parameter, not a String.
This overload is used when you want to find the first instance of a character from a list of characters.
I assume that you want to find the position of your string in a given range. Then you need to use this overload: RichTextBox.Find(String, Int32, Int32, RichTextBoxFinds).
For example:
' Obtain the location of the search string in RichTextView'
Dim index = RichTextView.Find(lowstring, 0, 2, RichTextBoxFinds.WholeWord)
Note that you can combine different RichTextBoxFinds bitwise.
For example:
Dim index = Me.RichTextView.Find(
lowstring,
0,
2,
RichTextBoxFinds.WholeWord Or RichTextBoxFinds.MatchCase
)
Your function call does not match any of the overloads exactly. However, VB was kind enough to find two possible matches that would work if an argument you provided was casted to a different type.
You probably wanted the overload with the string parameter. So you should have written,
RichTextBox1.Find(lowstring, 0, RichTextBoxFinds.WholeWord)
The fact that RichTextBoxFinds.WholeWord happens to have numeric value of 2 is not a reason to use that value instead of the enum member name.
This would also work:
RichTextBox1.Find(lowstring, 0, CType(2, RichTextBoxFinds))
but it is stupid.
Related
I am using IsNumeric() function in my code to validate numbers.
IsNumeric(100) - true,
IsNumeric(-100) - true,
IsNumeric(+100) - true,
IsNumeric(100-) - true - I have doubt in this. (100-) Is this a valid number? IsNumeric () returns true to this value.
Dim latitude As String = "12.56346-"
If IsNumeric(latitude) Then
If (Convert.ToDouble(latitude) >= -90 And Convert.ToDouble(latitude) <= 90) Then
isValidLatitude.Text = "true"
Else
isValidLatitude.Text = "false"
End If
Else
isValidLatitude.Text = "false"
End If
Error while converting latitude to double
Input string was not in a correct format.
IsNumeric is from the Microsoft.VisualBasic namespace/dll, a bunch of helper stuff intended to help VB6 programmers get their arcane VB6 code /knowledge working on VB.NET
You'll note if you use other functions from the same dll such as Microsoft.VisualBasic.Conversion.Int(), or you use dedicated VB.NET converters such as CInt or CDbl these will also cope with a trailing minus sign and return a negative value
If you want to dispense with the old ways of VB6, use a numeric type's TryParse.. but all in, be consistent / if you use a function from Microsoft.VisualBasic to determine if a conversion can be made, use the vb conversion because within itself the package will be consistent but between Microsoft.VB and normal .net System there are some differences in behavior
Edit:
A couple of people have been wondering about the source code and how it uses TryParse, so why doesn't it work like using TryParse directly?
Microsoft.VisualBasic.Information.IsNumeric() uses Microsoft.VisualBasic.CompilerServices.DoubleType.TryParse() to determine whether an expression is numeric. This DoubleType.TryParse is not the same as Double.TryParse - it is a helper method again in the VB namespace that specifically sets the NumberStyles.AllowTrailingSign flag among many other flags. When parsing the number (first as non currency related, using Double.Parse, then if it fails a second attempt is made using adjusted values for currency related tests) this AllowTrailingSign flag will be considered in conjunction with other regional number formatting rules in determining if the passed in value is numeric
You'll note that on a machine obeying a USA number formatting culture, a string of "(100$)" is also declared to be numeric but calling Double.TryParse("(100$)", x) will also return false. The VB helper methods here are being a lot more liberal in what they accept than the System methods, because they're telling the System methods to be more liberal than they are by default
As noted, I've always regarded the Microsoft.VisualBasic namespace as a bunch of helper methods intended to allow terrible old VB6 code to be pasted into VB.NET and work with minimal fiddling. I wouldn't advocate using it for new projects and I remove the reference when I work on VB.NET - using it to support the VB6 notions of "just sling anything in, of any type, and it'll probably figure it out and work.. and if it doesn't we can always on error resume next" should be discarded in favour of precise and accurate about the operations executed and their intent
Note: my previous answers were wrong about assuming it was a bug. As the answer of #Damien_The_Unbeliever states, this function tries to validate the string as a lot of data types. And actually, the value "100-" is a valid Decimal number. That's why it returns true (as it's a "valid" Decimal) but gives a exception when converting to Double (as it's not a valid Double). #Damien_The_Unbeliever really deserves your +1 for pointing that.
From the documentation (showing all the data types that IsNumeric tries to validate):
IsNumeric returns True if the data type of Expression is Boolean, Byte, Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or UShort. It also returns True if Expression is a Char, String, or Object that can be successfully converted to a number. Expression can contain non-numeric characters. IsNumeric returns True if Expression is a string that contains a valid hexadecimal or octal number. IsNumeric also returns True if Expression contains a valid numeric expression that begins with a + or - character or contains commas.
Also, #CaiusJard did a nice search and pointed out that inner methods use a NumberStyles.AllowTrailingSign option, which allows this behavior.
Ok, now to the solution:
Just use a TryParse method, from your desired data type (int, long, etc...). The cool thing is that it'll behaves exactly as you expect, and if the parsing is successful, we have the parsed value already available to use!
if (Int32.TryParse(value, out int number))
{
// String is a valid number, and is already parsed in the 'number' variable!
}
else
{
// String is not a valid number!
}
Solution's VB.Net version:
Dim value As String = "12.56346-"
Dim number As Double = 0
If Double.TryParse(value, number) Then
' String is a valid number, and is already parsed in the "number" variable!
isValidLatitude.Text = "true"
Else
' String is not a valid number!
isValidLatitude.Text = "false"
End If
IsNumeric answers a question no sane person wants to ask. As quoted by Vitox's answer:
IsNumeric returns True if the data type of Expression is Boolean, Byte, Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or UShort. It also returns True if Expression is a Char, String, or Object that can be successfully converted to a number.
Note, it doesn't tell you that the given string can be converted to all numeric types. It tells you that the string can be converted to at least one numeric type. And for bonus bad style points, of course, it doesn't tell you which types the string can be converted to.
Decimal.Parse("100-") will execute perfectly well and give you a Decimal containing a value of -100.
So, it's not a bug, it's a bad function that has been retained for backwards compatibility reasons. Nowadays, we know better, and that we want to test whether a string can be converted to a specific data type, for which the TryParse family of functions have been designed.
Question: Can I assume that Str(myString) will always return the same result as Str(CDbl(myString)) (assuming that myString is statically typed as a string)?
Context:
I am trying to understand VBA's implicit conversions. So far, it appears to me that Str(myString)
implicitly parses myString into a double (culture-sensitive) and then
converts the result into a culture-insensitive string.
For example, using a German locale (i.e. using , as the decimal separator), it holds that
" 1.2" = Str(1.2) = Str("1,2") = Str(CDbl("1,2"))
Since these implicit conversions contain a lot of "magic" to me, I am trying to rewrite a procedure that uses an implicit conversion (Str(myString)) to one using explicit conversion without changing the behavior.
Unfortunately, the documentation is wrong and, thus, useless. (The documentation claims that the argument to Str is interpreted as a Long, which is obviously rubbish: If that were the case Str(1.2) could never yield " 1.2".)
Your statement is true. Str(x) and Str(Cdbl(x)) give identical result provided that x is String data type and contains a valid number.
I made a small test to get convinced.
I used Excel, but it holds the same with Access.
Public Function myStr(txt As String) As String
myStr = Str(txt)
End Function
Public Function myStrCDbl(txt As String) As String
myStrCDbl = Str(CDbl(txt))
End Function
I tried with some key values (0, 1.2, 1E+307, 1E-307, ...) : result of myStr and myStrCDbl are always identical.
I also agree with you that the documentation is wrong. If Str() argument would be interpreted as Long, then Str(1.2) would give "1", because Long is an integer type.
In the mean time, I've found the VBA language specification and can confirm that the spec also answers the question with "yes":
CDbl, when receiving a string, performs a Let-coercion to Double:
If the value of Expression is not an Error data value return the Double data value that is the result of Expression being Let-coerced to Double.
Str, when receiving a string, first performs a Let-coercion to Double and then applies Str:
[If Number is a String,] the returned value is the result of the Str function applied to the result of Let-coercing Number to Double.
I have a fixed length string variable:
<VBFixedString(10)>
Public myVar As String
When I assign the var to a value that has 11 chars the var changes length.
This is what I get:
myvar = "1234567890ABCD" ' result = myvar being "1234567890ABCD"
What I want is myvar to be: "1234567890"
and then if it is less than 10 I would like
myvar = "12345" to be "12345 "
I can do this with PadLeft PadRight but was wondering if I can declare it as a fixed length and it would handle all that for you.
and from the old school, LSET will do exactly what you're asking.
myvar = lset("1234567890ABCD",10)
results in "1234567890", while
myvar = lset("12345",10)
results in "12345___" - sorry, the editor trimmed the spaces after the 12345 replaced with underscores
see MSDN entry for LSET
From what I understand the VBFixedString attribute is only recognised by certain file based methods to help structure content written to/read from files. The compiler will not use that attribute for anything else, including to alter how a variable assignment is compiled.
Taken from MSDN:
The VBFixedStringAttribute is informational and cannot be used to
convert a variable length string to a fixed string. The purpose of
this attribute is to modify how strings in structures and non-local
variables are used by methods or API calls that recognize the
VBFixedStringAttribute. Keep in mind that this attribute does not
change the actual length of the string itself.
The last sentence is the important bit:
Keep in mind that this attribute does not change the actual length of the string itself.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbfixedstringattribute.aspx
EDIT 1:
A quick example on how to auto-padding a string based on a fixed length:
Function FixedLengthString(ByVal value As String, ByVal totalLength As Integer, ByVal padding As Char) As String
Dim length = value.Length
If (length > totalLength) Then Return value.Substring(0, totalLength)
Return value.PadRight(totalLength, padding)
End Function
Here you can pass in a string and if the length of the string is greater than the specified total length you will get a string matching that length. Anything less and you'll get the string plus the padding character upto the specified total length.
This can be improved with error checking and maybe making the method an extension method so you don't have to pass "value".
I want to know what exactly are the differences between CStr(), Str() and .ToString()?
Label1.Text = CStr(Int(Rnd() * 10))
and
Label1.Text = Str(Int(Rnd() * 10))
and
Label1.Text = Int(Rnd() * 10).ToString
If I use this condition:
If Label1.Text = "7" Then
'Some code here
End If
Str() doesn't work here. What's the difference?
ToString will call the .ToString() function on a particular instance.
In practice, this means that it will throw an exception if the object in
question is Nothing. However, you can implement .ToString() in your own
classes to get a useful string representation of your object, whereas
CType/CStr only work with built-in classes and interfaces.
CStr and CType(expression, String) are exactly equivalent (I'm not
sure where the other poster got the idea that CStr is faster). But they
aren't really functions, they're compiler directives that will emit very
different code depending on the declaration of expression. In most
cases, these directives call a bunch of internal VB code that tries to
get a reasonable string out of expression.
DirectCast(expression, String) assumes that the expression in
question really is a String and just casts it. It's the fastest of all
these options, but will throw an exception if expression is anything
other than a String.
As an Addition to the VBA/VB6 Environment where we have no ToString():
Str() is not aware of international representation. The decimal separator always is a dot (.).
As already mentioned above it prefixes the resulting string with a blank in case of positive values.
There also exists Str$(). The difference to Str() is the return type:
Str() returns a variant of type string, Str$() returns a string.
And Str$() is slightly faster then Str().
CStr() in contrast is aware of international representation. The decimal separator depends on the Windows international settings.
No additional prefixing for positive values will be done.
So if you need to convert a value type to a string and have to ensure a dot as a decimal separator and no prefixing blank, then use this syntax:
Dim d As Double
d = 123.456
Dim s As String
s = Trim(Str$(d))
I don't know about ToString() and i don't know about VB.NET
But in VB6 (Visual Basic 6):
Both of Cstr() and Str() converts values to string. but Cstr() is better because:
Str(): After converting to string it adds 1 space before positive numbers. for example: Str(22) > " 22"
Cstr(): After converting to string it never adds the above extra space - For best result use it with Trim() - Trim(Cstr(Variable))
Although not a problem in the code in the question, it is important to mention that Str() only converts numerical expressions to string, gives an error in other cases, so don't use it for converting values of a cell.
My answer is str() is evil as it always prepends a space for the sign character so if you are comparing values it fails. Instead use CStr() instead which does not do this.
You may comes across business logic that tries to do this:
Eg:
Dim sVar as String = "1"
Dim i as Integer = 1
console.write( cstr(i) = sVar )
Which outputs:
False
I lost a couple hours on this one as the code was quite deep in old code and was very difficult to grok in production environment where logging based debugging was all that was available.
Basically, I have been using both Integer.Parse and CInt in most of my daily programming tasks, but I'm a little bit confused of what the difference is between the two.
What is the difference between Integer.Parse and CInt in VB.NET?
CInt does a whole lot more than Integer.Parse.
CInt will first check to see if what it was passed is an integer, and then simply casts it and returns it. If it's a double it will try to convert it without first converting the double to a string.
See this from the help for CInt and other Type Conversion Functions
Fractional Parts. When you convert a
nonintegral value to an integral type,
the integer conversion functions
(CByte, CInt, CLng, CSByte, CShort,
CUInt, CULng, and CUShort) remove the
fractional part and round the value to
the closest integer.
If the fractional part is exactly 0.5,
the integer conversion functions round
it to the nearest even integer. For
example, 0.5 rounds to 0, and 1.5 and
2.5 both round to 2. This is sometimes called banker's rounding, and its
purpose is to compensate for a bias
that could accumulate when adding many
such numbers together.
So in short, it does much more than convert a string to an integer, e.g. applying specific rounding rules to fractions, short circuiting unnecessary conversions etc.
If what you're doing is converting a string to an integer, use Integer.Parse (or Integer.TryParse), if you're coercing an unknown value (e.g. a variant or object from a database) to an integer, use CInt.
Looking with ILDASM at some sample code you can see that CInt is converted to this call:
Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(string)
Using .NET Reflector, you can extract this piece of code:
Public Shared Function ToInteger(ByVal Value As String) As Integer
Dim num As Integer
If (Value Is Nothing) Then
Return 0
End If
Try
Dim num2 As Long
If Utils.IsHexOrOctValue(Value, (num2)) Then
Return CInt(num2)
End If
num = CInt(Math.Round(Conversions.ParseDouble(Value)))
Catch exception As FormatException
Throw New InvalidCastException(Utils.GetResourceString("InvalidCast_FromStringTo", New String() { Strings.Left(Value, &H20), "Integer" }), exception)
End Try
Return num
End Function
You can see that internally it calls Conversions.ParseDouble.
Therefore, as already explained by Binary Worrier, use Integer.Parse for string coercing and CInt only for casting.
Here is a real difference :
Integer.parse("1.00") will thrown an error.
CInt("1.00") will work
The <Data Type>.Parse methods are used to extract a value of the type from a string that represents the value (e.g 2 from "2") The C<Data Type> functions operate on expressions and return a variant sub-typed to the desired type (e.g. CInt("2") OR CInt(SomeDouble + SomeDouble), etc.).