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.
As AAU already mentioned, your problem lies in parameter list:
public static List<Contact> searchForContacts(string LastName, PostalCode)
PostalCode MUST have a type, in your case it should be String.
But also, another problem could(and probably would if that's the full code) arise from these 2 lines:
LastName = LastName;
MailingPostalCode = PostalCode;
Here you just set values of LastName and PostalCode to some variables that were not defined anywhere. Instead I would recommend to delete these 2 lines and edit your query from
[SELECT ID, Name
FROM Contact WHERE LastName = :LastName
AND MailingPostalCode= :MailingPostalCode];
to
[SELECT ID, Name
FROM Contact WHERE LastName = :LastName
AND MailingPostalCode = :PostalCode];
This will save you 2 lines of code, 2 allocations and will do exact same thing.
Best Answer
The following code will solve your problem:
Let's view it line-by-line:
This is just a class definition. Public says that this class is in public scope
This is method declaration. Again, public keyword makes this method public accessible(public scope).
Static means that this method is class-method(static-method) and not instance-method. Difference is that for instance-method you would omit static keyword, but you would need an instance of that class to call this method.
List<Contact>
is return type of the method. It means that this method will return aList
ofContact
objects.searchForContacts is method name, use it to call this method.
String lastName, String mailingPostalCode
are parameters that are expected by this method.This line assigns the result of the query to retList variable.
List<Contact> retList
makes a variable with name retList of typeList<Contact>
.SELECT Id, Name, LastName, MailingPostalCode FROM Contact
is a simple query that will return all contacts. However, you only need the ones that match given parameters, so the next line of the query is:WHERE LastName = :lastName AND MailingPostalCode = :mailingPostalCode
This adds a filter to the query, so that it will only return results which match LastName and MailingPostalCode with given parameters.
Note the
:
in the query before the parameters:If you want to use variables in your query, they should have
:
in front. That tells your code that it's not the value to be searched for, but a variable from the codereturn retList;
will return the list of matching contacts that you just foundupdate: alternativly, instead of creating a retList variable, you could immediately return the query. Like this: