[RESOLVED]'string' does not contain a definition for 'Right'

Let’s say the db query returns token = "AbCDjKl01O"

Getting the error message on the last line.

Why?

var token = db.QueryValue("SELECT confirmationToken FROM webpages_Membership WHERE UserId=@0", theuser);
            //get rid of 0,o,O for user convenience.
            var mtokenlong = token.Replace("0", "6").Replace("o", "m").Replace("O", "M");
            var mtoken = mtokenlong.Right(5);

AFAIK You dont have a function called Right in C#, You can use SubString instead of that

You can try with the below code

var token = db.QueryValue("SELECT confirmationToken FROM webpages_Membership WHERE UserId=@0", theuser);
            //get rid of 0,o,O for user convenience.
            var mtokenlong = token.Replace("0", "6").Replace("o", "m").Replace("O", "M");
            var mtoken = mtokenlong.Substring(mtokenlong.Length - 5);

Hi,

This is correct. If you check the doc for this class, you’ll see that it doesn’t have a Right method.

Try http://www.csharphelp.com/2007/07/c-left-right-and-mid-functions/ (or just
use Substring).

You cannot call a method Right on string. What actually are you trying to do with it? 

Is there a particular function, you wanted to execute, or expected to? 

I need to get the last 5 from the confirmation token.

The suggested method (A2H) works, but I am wondering why a string operator (get me the last 5) won’t work on a string.

Guess it is not a string operator.

wavemaster

, but I am wondering why a string operator (get me the last 5) won’t work on a string.

By Operator if you mean "Right" operator, then the reason is Right function doesnot exist for C#.

It’s just not part of the String class:
http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx
 and I’m not sure where you saw this "string operator" stuff…

It seems a confusion with
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.right(v=vs.110).aspx
 which provides a set of string functions that are globally exposed making them to look as if it were directly part of the VB language…

I am assuming you were guessing for similar method that C++ has, for the Right shift or the Left shift. No, in C# there is no such method. 

However, you can create subStrings for the string you are using. If that method runs, mark him as answer. :-)

The Right function that you are referring to is commonly used within Visual Basic but does not exist within C#.

As mentioned, the SubString() method can be used to accomplish the same thing, however if you would prefer the syntax, you could easily write your own extension method like the following to handle this for you :

static class StringExtensions
{
    public static string Right(this string value, int length)
    {
        if (String.IsNullOrEmpty(value))
        { 
             return "";
        } 

        return value.Length <= length ? value : value.Substring(value.Length - length);
    }
}

This will allow you to use the same syntax that you are accustomed to in C# :

// Example string
var example = "AbCDjKl01O";
		
// Grab the last 5 right characters
var rightExample = example.Right(5); // yields "K101O";

You can see a working example here.

Rion,

You hit my head on the nail…

I had copied some existing code (.Right etc.) from another application. I never realized until now there is some function in a class file somewhere. Which I did not copy across.

Wow.

Leave a Reply