Why am I getting 'Trim' is not declared with this VB.NET code? - vb.net

I am trying to get a VB.NET app to compile. Besides the "elephant in the room", I'm also getting 7 "'Trim' is not declared" errors on code like this:
...as well as one "'IsNothing' is not declared. It may be inaccessible due to its protection level." on this line:
If IsNothing(memberList) = False Then
I don't know VB, so there may be a simple solution to this, but I have no clue what the problems are.

The Trim function requires a reference to Microsoft.VisualBasic from the assembly Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Usually is preferable to use the native Trim method from the string class and not add a reference to this assembly (mainly used to help porting old VB6 apps)
mail.CC.Add(addr.Trim())
Notice also that the string.Trim removes other whitespace characters as tabs while the Microsoft.VisualBasic function does not.

You have to use addr.Trim instead of Trim(addr)
Read more about Trim in this MSDN article
And you should use
If not memberList Is Nothing Then
Instead of
If IsNothing(memberList) = False Then
Or
You have to import Microsoft.VisualBasic namespace

If you use the Left(), Mid(), and Right() string functions you might find it easier to convert those too:
Left(t, l) becomes t.Substring(0, l)
Mid(t, s, l) becomes t.Substring(s-1, l)
Right(t, l) becomes t.Substring(t.Length - l)
Often Left and Right are properties and stop you using the old VB string functions.

String.Trim doesn't take a string parameter. It returns a new string in which all leading and trailing occurrences of a set of specified characters from the current String object are removed.
It should be...
addr.Trim()

Related

How can I get the display name from my enum

I have researched and it seems that most is bouncing around the problem I have.
#Code
#Imports System.ComponentModel
Dim values = New SelectList([Enum].GetNames(GetType(myEnum)).GetAttribute<DisplayAttribute>()
End Code
The last pararenthesis has a blue line under it and when hover tells me an expression is expected. I want to capture the display name from my enum and have tried many things found on the google search without success. Why am I getting the expression expected error?
Attempted to incorporate and now getting at end parenthesis
Dim type = typeof(MyEnum) ls is expected.
You might want to have a look at this awesome NuGet package called UnconstrainedMelody from Jon Skeet.
https://www.nuget.org/packages/UnconstrainedMelody/
Helpful static methods (or extension methods) for enums and delegates, with constraints which can't be expressed in regular C#.
Have a look at the functions UnconstrainedMelody.Enums.GetNames() and UnconstrainedMelody.Enums.GetValues()

How do I pass parameters to a method both as normal and like a property?

In RealBasic (now Xojo), which I'm leaving to the past, I used to be able to declare a method like this:
Sub MyCoolSub(param1 as string, Assigns parameter2 as integer)
Do
'Waste CPU time scrying the universe.
Loop
End Sub
And then call it in this way:
MyCoolSub("Answer")=42
Now I'd want to replicate this behaviour in VB.Net.
The closest thing I stumbled upon is the Property's clauses, but VS does not let me add parameters to it that would however require some overhead which decreases the convenience of this type of declaration.
Do you have any better suggestion?
PS. As a side question I would be pretty happy to know that there is a way to comment with "//" in VB.Net, as I'm not that comfortable with the apostrophe character. Is there any thing as a VS comment characters list? Maybe an extension could do it...
When I look at the documentation for the Xojo Assigns keyword the closest thing I can think of is to create a write-only property like this:
Public WriteOnly Property theVolume(a As Integer, b As Integer) As Integer
Set(c As Integer)
Debug.WriteLine("a={0}, b={1}, c={2}", a, b, c)
End Set
End Property
theVolume(1, 2) = 3
a=1, b=2, c=3
Regarding your "P.S." note:
to ease your switching to VB.NET you can use the following AutoHotKey command:
:O://::'
This will do the appropriate substitution for you during typing.
You can limit it only to your IDE window using #IfWinActive directive.
Anyway, Roslyn VB.Net compiler is now open source so you can download and play...

Enforce Type Alias in VB.NET

How might one go about aliasing a type in VB.NET or C# such that when the alias is used as an argument type in a function, accidentally using the not aliased type is an error?
i.e.
Imports AccessSpecifier = System.String
Module Accessors
Delegate Function IoOper(ByRef strm As System.IO.Stream) As Action
Public Function accessMethod(ByRef spec As AccessSpecifier) As IoOper
' implementation
' ...
End Function
End Module
Module Main
Public Sub Main()
Dim spec As AccessSpecifier = New AccessSpecifier(CType("READ_WRITE", Char()))
Dim val = Accessors.accessMethod(spec)
System.Console.WriteLine(val.GetType())
Dim shouldFail = Accessors.accessMethod("FAIL_ME")
System.Console.WriteLine(shouldFail.GetType())
End Sub
End Module
Or perhaps is there a better way to go about this?
Overall, I'm wanting the IDE to force me to know what I'm doing if I'm throwing Ints around to mean Flags, or States and Strings around to mean Names, Propertys and Records.
Help?
I've never liked Type aliasing in .NET. It makes for imprecise code and it is not immediately clear what is happening. As in your example, when an individual went looking for the AccessSpecifier.vb file (or class) they would not find it.
Also, Aliasing only works within YOUR project and only within a single code file. So you would have to define that alias in all the various code files where it was to be used.
A better (as in easier to read, easier to maintain, and more clear of intent) option is to create a class and overload the type conversion operators for automatic conversion to/from String. In this manner you retain your ability to use your new class as if it were a String, but you gain your strict type checking.

Write String.Join(Of T) in VB.Net

I have a simple code in C#:
Console.WriteLine(string.Join<char>("", ""));
And I can't convert it to VB.Net. Even reflector show me code in VB like:
Console.WriteLine(String.Join(Of Char)("", ""))
But it can't be compiled becouse I have an starge error:
Error 1 Expression expected.
It looks like VB.Net don't have this generic method at all.
Both project use Net Framework 4.
Why this error happened?
UPD:
I've create a custom class and copy Join(Of T) declaration to it:
Class string2
Public Shared Function Join(Of T)(ByVal separator As String, ByVal values As System.Collections.Generic.IEnumerable(Of T)) As String
Return "1"
End Function
End Class
Console.WriteLine(string2.Join(Of Char)("", ""))
It works
UPD2:
My compilation string, where you can see that I'm using Net4:
http://pastebin.com/TYgS3Ys3
Do you have a code element named String somewhere in your project?
Based on the answer you have added to this question (where you indicate that changing String to [String] appears to have solved the problem), I guessed that this may be the result of a naming collision.
I was able to duplicate the error you are seeing -- "Expression expected" -- by adding a module to my project called String and defining a (non-generic) Join method from within that module.
This may not be the specific scenario you find yourself in. But the fact that the code works for you with [String] is, to me, very compelling evidence of a simple namespace collision.
Based on the documentation for the "Expression expected" error, I'm guessing you haven't included the entire section of code where this error is appearing for you.
Do you have a lingering operator such as + or = somewhere?
(The VB.NET code you posted is indeed equivalent to the C# code above it and should compile no problem. This is why I suspect the real issue lies elsewhere.)
String.Join<T>(string, IEnumerable<T>) is useful with LINQ, for standard joins is better to use the String.Join(string, string()) overload.
In C#, "" as Char produces an empty Char (\0). Writing the same thing ("") in VB produces an empty string which is not the same as an empty char. In order to produce an empty character, you'll have to write New Char().
Your VB code therefore becomes:
Console.WriteLine(String.Join(Of Char)(New Char(), New Char()))
Edit
I just checked and it appears String.Join does not support the format you're specifying.
Instead, it goes as follows:
Join(separator As String, value As String()) As String
Your code should be as follows:
Console.WriteLine(String.Join("", New String() {""}))
String.Join(Of Char)(str1, str2) wasn't added til .net 4, it seems. That's why your custom class worked -- it had the declaration, but the String class in the framework you're actually using doesn't.
Check your settings and references to make sure you're targeting .net 4 all around -- cause that's the only thing that seems able at this point to stop the call from working.
Here the solution:
Console.WriteLine([String].Join(Of Char)("", ""))
Why this problem occurs only with generic method? I wish I know...

ToString vs. ToString() in VB.NET

What is the difference between using ToString and ToString() in VB.NET?
Nothing. VB.NET allows you exclude the parentheses on any method that doesn't take in an argument.
The existing answer is wholly correct but does not cover when ToString is used as a Method. This is essentially incorrect coding but it is possible
Dim sbrBuilder as New StringBuilder
...
sbrBuilder.ToString()
return sbrBuilder.ToString
The first ToString (which does nothing) does not produce an error but the brackets are forced on by the IDE. The second ToString does not require brackets (optional - as explained already in the Answer) as it is used to collect the value of ToString.
Hopefully this will help anyone who is wondering why the IDE keeps adding brackets on ToString - then you will realise that you forgot to assign it to anything like I did