[SalesForce] Stuck on a “Variable does not exist: String” error

I'm trying to convert some C# code I wrote to access a third party REST service into APEX. It's failing with an error:

"Variable does not exist: String"

at the first line of the getSecureUrl method. I checked both variables I'm passing in and they're valid instances with values inside, so I'm not sure where its breaking down and the error message is just not helpful. Here's the code that's causing the error:

private string getSecureUrl(
    String method,
    List<String> parameters) {
    String requestUrl = String.format('https://blargh.com/v2/{0}', method); // error!

    return String.format(requestUrl, parameters);
}

/// Authorization Methods   ============================================================================
private void authorize(
    String userName,
    String token)  {
    String requestUrl = this.getSecureUrl('UserAuth/login?username={0}&app_token={1}', new String[] {
        userName,
        token
    });

    this.getResponse(requestUrl);
}

I'd appreciate some help because APEX is driving me out of my mind…

Best Answer

You are passing a single string method as the second parameter to the string.format method. I think it is confusing the compiler into thinking you are looking for a variable called String.

Try:

private string getSecureUrl(
    String method,
    List<String> parameters) {
    String requestUrl = String.format('https://blargh.com/v2/{0}', new List<string>{method});

    return String.format(requestUrl, parameters);
}

Apex doesn't have the C# concept of params. So you need to explicitly put the into to List<String> or String[].