Comparing Strings in VB 6.5 - vba

I am trying to compare two strings in VB but compareTo, compare, equals etc all give compile errors.
If String.Compare(string_one, string_two) = 0 Then
'...do stuff
End If
If String.Equals(string_one, string_two) Then
'...do stuff
End If
Now both lines give me the error,
Compile error:
Expected: (
and it highlights the dot after String, i.e.
String.(whatever)
______↑__________
Do I need to include something, I normally program C, Java and C# so I am not very familiar with VB
Now I am doing this in a very crappy program that uses Microsoft Visual Basic 6.5 is it that these functions just simply do not exist?

VB6 just uses the = operator:
If string_one = string_two Then
''# Do Stuff
End If
For that matter you do it that way in VB.Net as well, though vb.net also allows the .Equals() function in this form:
If string_one.Equals(string_two) Then
''# Do Stuff
End If

You could also use StrComp function:
StrComp(String1, String2, [Compare As VbCompareMethod = vbBinaryCompare])

Related

Error using isNumeric() in VB

I am using the IsNumeric() function in visual basic 2012.
my code is this
Dim input As String = "123"
If isNumeric(input) Then
'number codes
Else
'not a number codes
End If
and i'm getting an error on the isNumeric(input) part
isNumeric is a namespace and cannot be used as an expression
i just want to know what is wrong with this, i cant find any documentation that this function has already changed or something.
It sounds like you've created a name clash. You have presumably named your project 'IsNumeric'. The root namespace for the project is named after the project by default so you now have a root namespace named 'IsNumeric' and that takes precedence over the IsNumeric method.
There are a number of options to fix this. Firstly, you can change the root namespace for the project to something other than 'IsNumeric', which you would do in the project properties. Alternatively, you can qualify the method name with its namespace, its module or both, i.e. use Microsoft.VisualBasic.IsNumeric, Information.IsNumeric or Microsoft.VisualBasic.Information.IsNumeric.
I'd tend to suggest not using IsNumeric anyway. It can't distinguish between types of numbers and provides no access to the actual numeric value. If you need to do any of that sort of thing then call the appropriate TryParse method instead, e.g.
Dim number As Double
If Double.TryParse(someText, number) Then
'The input was a valid Double and the value is in 'number'.
Else
'The input was not a valid Double.
End If
Note that IsNumeric actually calls Double.TryParse internally and is the reason it was created in the first place. That's why calling IsNumeric and then something like CDbl is bad: you're parsing the same text twice in that case.
It's very strange, because IsNumeric is a standard function available in VB.Net. Try to create a new console application:
Sub Main()
Dim str As String = "123"
If (IsNumeric(str)) Then
End If
End Sub
For me it works.

Format number with leading zeroes in .NET 2.0

I have problem to format numbers and convert it to string with leading zeroes when application uses NET framework 2.0 with Visual Basic.
I try:
Dim myNum = 12
Dim myStr as String
Dim myStr = myNum.ToString("0000")
or
Dim myStr = myNum.ToString("D4")
... in order to get wanted string: 0012
Please help to solve this.
You have an old version of Visual Studio, one that doesn't have Option Infer yet. Or it isn't turned on. That makes the myNum identifier a variable of type Object.
So your code tries to call the Object.ToString() method. Which does not have an overload that takes an argument. The compiler now tries to make hay of your code and can only do so by treating ("0000") or ("D4") as an array index expression. Indexing the string that's returned by Object.ToString(). That has pretty funny side effects, to put it mildly. A string like "0000" is not a valid index expression, the compiler generates code to automatically convert it to an Integer. That works for "0000", converted to 0 and the result is a character, just "1"c. Converting "D4" to an integer does not work so well of course, that's a loud Kaboom!
The solution is a very simple one, just name the type of the variable explicitly:
Dim myNum As Integer = 12
Dim myStr = myNum.ToString("D4") '' Fine
VB.NET's support for dynamic typing is pretty in/famous. Meant to help new programmers getting started, it in fact is an advanced technique given the myriad ways it can behave in very unexpected ways.
The universal advice is always the same. Let the compiler help you catch mistakes like this. Put this at the top of your source code file:
Option Strict On

Using statement in Visual Basic

I'm having difficultly with the using statement in Visual Basic. Does anyone know how this should be written?:
Using (Dim doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True))
//blah blah
End Using
The code works fine without the using and its obviously as syntactic error. "Dim" is highlighted, and an expression is expected apparently. Sorry if this a bit basic, but the info on vb using statements is not clear and its obviously doesn't work in the c# style.
There's two things wrong.
First, you must remove the Dim keyword. The Using keyword replaces the Dim keyword. Both Dim and Using have the same effect of declaring a new variable, just in different ways.
Secondly, you must remove parentheses. The very first thing after the Using keyword must be the variable name.
Using doc As WordprocessingDocument = WordprocessingDocument.Open(filename, True)
' blah blah
End Using

Function in VB that doesn't have a return type

I am not much familiar with Visual Basic 6.0 and am not having a VB compiler installed, but I was looking at some VB code for some debugging and saw this:
Private Function IsFieldDeleted(oLayoutField As Object)
Dim oColl As Collection
Set oColl = GetFieldIdsForField(oLayoutField)
IsFieldDeleted = (oColl.Count = 0)
Set oColl = Nothing
End Function
In other functions I see they define the return type with an "As" for example "As Boolean" but this one does not have an "As" :D and then how they have used it is like this:
If Not IsFieldDeleted(oRptField.GetUCMRLayoutField) Then
Call oCollection.Add(oRptField, oRptField.ObjectKeyString)
Call AddToNewLineSeperatedString(sCaseFldDescMsg, oFld.FieldDescription)
End If
How is this working? Is it just like rewriting it and saying that the function returns an integer and compare the return type to be either 0 or 1? Or are there some other hidden tips in there?
When no type is specified, in VB.NET it assumes Object for the return type. In VB6, it assumes Variant. In VB.NET you can make things much more obvious by turning Option Strict On, but I don't believe that option was available in VB6.
The value that is returned, in reality, is still typed as a Boolean, but you are viewing the returned value as a Variant. So, to do it "properly", you really ought to cast the return value like this:
If Not CBool(IsFieldDeleted(oRptField.GetUCMRLayoutField)) Then
....
End If
Calling CBool casts the value to a Boolean instead of a Variant. This is unnecessary, though, since VB will use late-binding to determine the type of the return value is a boolean.
The best thing to do in this case is to change the function to As Boolean. Doing so will not break any existing code since that's all it ever returned anyway. However, if it's a public member in a DLL, that would break compatibility.

VB.NET: Is there a way to keep Nothing from defaulting to 0 for number types?

It's really bugging me that the VS 2010 IDE isn't barking at me for trying to pass Nothing through a method parameter that takes an user-defined enum. Instead, it's passing 0 through to the method. c# would never allow this. Is there some module-level modifier I can add like option strict that will force the IDE to not allow these types of implicit conversions?
Sadly, no.
But you can assign values to your enumeration members while skipping 0 (or use a placeholder named None or something like that), and at least handle this case at run time.
Sub Main
MyMethod(Nothing) ' throws Exception
End Sub
Sub MyMethod(e as MyEnum)
If e = 0 Then
Throw New Exception
End If
End Sub
Enum MyEnum
a=1
b=2
c=3
End Enum
Nothing is the equivalent of default in the C# language. So no.
Reconsider your programming style, Nothing should be used very sparingly. Basically only in generic code, same place you'd use default in C#. You don't need it anywhere else, VB.NET doesn't insist on variable initialization like C# does. Any variable of a reference type gets initialized to Nothing automatically. Cringe-worthy to a C# programmer perhaps, but entirely idiomatic in VB.NET code.