Home > front end >  string does not contain a definition for valueOf
string does not contain a definition for valueOf

Time:04-30

Hello I have a Problem With my Code

string does not contain a definition for valueOf

Code In c#

        {
            public static String e(String str, int i5)
            {
                return String.valueOf(str.ToCharArray()[(int)(str.Length / i5)]);
            }
        }

Code In VB.NET

   Public Shared Function e(ByVal str As String, ByVal i5 As Integer) As String
        Return String.valueOf(str.ToCharArray()(CInt((str.Length / i5))))
    End Function

Here IS the Problem String.valueOf

CodePudding user response:

In Java, valueOf provides a string representation of some other kind of argument, like a bool true would become a string "true". This code pulls a char out of the string at position Length/i5, converts it to a string and returns it

In C# you could do that with:

public static String E(string str, int i5)
{
    return str[str.Length/i5].ToString();
}

Or:

public static String E(string str, int i5) =>
  str[str.Length/i5].ToString();

In VB:

Public Shared Function E(str as String, i5 as Integer) As String
    Return str(str.Length/i5).ToString()
End Function

CodePudding user response:

is what you are expecting is to convert vb to c#?

void Main()
{
    string s ="xhhxzx";
    int i5 = 3;
    string res = e(s, i5);
    Console.WriteLine(res);
    
}

 public static String e(String str, int i5)
            {
    return (str.Length / i5).ToString();
}

you can do in one line

public static String e(String str, int i5) => (str.Length / i5).ToString();
  • Related