Microsoft.VisualBasic.Strings.Len() works, but Len() does not

  • Thread starter Thread starter Tim Daim
  • Start date Start date
T

Tim Daim

Can anybody please tell me what I need to do to be able to just type
Len(sMyString) instead of Microsoft.VisualBasic.Strings.Len(sMyString)

When I convert old projects (from VB6 to .NET), Len() works, but if I
create a new VB.NET project, it does not. I set up the references so
that they look exactely like my converted project (with
VisualBasic.Compatibility, etc...), but I must have missed something.

Thank you!
Tim
 
Tim Daim said:
Can anybody please tell me what I need to do to be able to just type
Len(sMyString) instead of Microsoft.VisualBasic.Strings.Len(sMyString)

When I convert old projects (from VB6 to .NET), Len() works, but if I
create a new VB.NET project, it does not. I set up the references so that
they look exactely like my converted project (with
VisualBasic.Compatibility, etc...), but I must have missed something.

Thank you!
Tim


Try adding

imports Microsoft.VisualBasic.Strings

at the top of the code.
 
Can anybody please tell me what I need to do to be able to just type
Len(sMyString) instead of Microsoft.VisualBasic.Strings.Len(sMyString)

When I convert old projects (from VB6 to .NET), Len() works, but if I
create a new VB.NET project, it does not. I set up the references so
that they look exactely like my converted project (with
VisualBasic.Compatibility, etc...), but I must have missed something.

Thank you!
Tim

You need to import Microsoft.VisualBasic.

There are two ways to do this. The first is to add in each source
file that needs it:
Imports Microsoft.VisualBasic

The second is to go to the References section of the project's
Properties, find Microsoft.VisualBasic in the bottom listbox and check
the checkbox. This effectively adds the above Imports to each source
file. The converter probably does this.
 
Can anybody please tell me what I need to do to be able to just type
Len(sMyString) instead of Microsoft.VisualBasic.Strings.Len(sMyString)

When I convert old projects (from VB6 to .NET), Len() works, but if I
create a new VB.NET project, it does not. I set up the references so
that they look exactely like my converted project (with
VisualBasic.Compatibility, etc...), but I must have missed something.

Thank you!
Tim

Just start a new Winform or Console application template.
Microsoft.VisualBasic namespace must be referenced automatically, thus
Len function will be recognized without being forced to qualify full
path.

Additional trick, when you look at "References" in Solution Explorer
by clicking "Show All Files", you may not be able to see
Microsoft.VisualBasic, but it is imported as namespace, so to confirm
this, go to project settings -> References Tab, see "imported
namespaces".

Then you can use Len function directly or String.Length property
in .NET.

Hope this helps,

Onur Güzel
 
Can anybody please tell me what I need to do to be able to just type
Len(sMyString) instead of Microsoft.VisualBasic.Strings.Len(sMyString)

When I convert old projects (from VB6 to .NET), Len() works, but if I
create a new VB.NET project, it does not. I set up the references so
that they look exactely like my converted project (with
VisualBasic.Compatibility, etc...), but I must have missed something.

Thank you!
Tim

Above all else, I would highly recommend you drop the VB classic
syntax and programming style as you move into .NET. Not only is
thinking that .NET is the next iteration of VB classic a dangerous
assumption, but using the syntax will make it harder for you to switch
between languages (such as converting a C# sample to something usuable
in VB, or for a future job / project that is written in a different
language).

For this particular scenario, just use the .Length() property of the
string to accomplish the same thing in a cross-language friendly
fashion.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Back
Top