1

I have added a reference to Microsoft.VisualBasic and a using statement but still get an error calling the Left function: eg)

string s = e.Row.Cells[3].Text;

e.Row.Cells[3].Text = Left(s, 10);

"The name 'Left' does not exist in the current context".

Also tried Strings.Left and Microsoft.VisualBasic.Left and Microsoft.VisualBasic.Strings.Left...

Any other ways to do this in C#?

JohnnyBizzle
  • 971
  • 3
  • 17
  • 31

2 Answers2

4

Use .NET methods like String.Substring:

string firstTen = s.Substring(0, 10);

or String.Remove:

string firstTen = s.Remove(10); 

Note that you should handle the case that s is null or that the input string is too short.

If you insist on VisualBasic.Left you could fully qualify it:

string firstTen = Microsoft.VisualBasic.Strings.Left(s, 10);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Fully qualifying it didn't work - I tried that. I will try substring, thanks. – JohnnyBizzle Feb 09 '15 at 08:57
  • @JohnnyBizzle - Did you have a reference to the Microsoft.VisualBasic assembly? – Chris Dunaway Feb 09 '15 at 14:59
  • @ChrisDunaway: OP has mentioned that he has _"added a reference to Microsoft.VisualBasic"_. – Tim Schmelter Feb 09 '15 at 15:01
  • Hmmm.... I wonder why fully qualifying it didn't work then. We're missing some piece of information. – Chris Dunaway Feb 09 '15 at 15:03
  • 1
    Fully qualifying requires including the correct class - "Microsoft.VisualBasic.Strings.Left(s, 10);". VB allows omitting 'Strings' in the qualification since 'Strings' is a VB 'module' and methods within it do not require the module qualification (only VB allows this or has any concept of 'module'). (Just use 'Substring'!) – Dave Doknjas Feb 09 '15 at 15:37
0

You can use the String.Substring :

string s = e.Row.Cells[3].Text;
e.Row.Cells[3].Text = s.Trim().Substring(0, 10);
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70