You can't instantiate a List
like that using an integer. You don't need to explicitly specify how much items are going into the list when it is created. Instead, just remove the integer:
//Instantiate the list
String[] myArray = new List<String>();
You're logic in your loop seems to be incorrect as well. Specifically, myArray
's size will always be 0 when you instantiate it. Change your loop logic to:
for(Integer i=0;i<length;i++) {
// code
}
You want to generate a List of strings based on the i
variable. You will want to change your internal logic to:
//Populate the array
myArray.add('Test ' + i);
// Write value to the debug log
System.debug(myArray[i]);
Finally, you want to return that Array. You need to change your method so the return type is no longer void
by changing its signature:
public static String[] generateStringArray(Integer length)
then it is just a matter of return
ing that array at the end of your method:
return myArray;
This would bring it all together as:
public class StringArrayTest {
//Public Method
public static String[] generateStringArray(Integer length) {
//Instantiate the list
String[] myArray = new List<String>();
//Iterate throught the list
for(Integer i=0;i<length;i++) {
//Populate the array
myArray.add('Test ' + i);
// Write value to the debug log
System.debug(myArray[i]);
} //end loop
return myArray;
}//end method
}// end class
Some more info on Arrays and Lists, Loops, and Class methods for future reference.
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[]
.
Best Answer
You're trying to call:
Which is not a valid function. You need to query your user first, then reference that instance:
Fields can referenced as tokens using
Object.Field
, whereObject
is any object you can access (User, Account, etc), andField
is the API name of the field. This lets you reference fields dynamically.See the SOBjectField class for more details on how field tokens work.
Note that
{!mergefields}
doesn't work in Apex Code, except in some limited instances (notably, using dynamic components with expressions). You generally want to use the + operator to concatenate strings.