1

Void methods like below does not work with system.debug... why ?

List<String> myStrings = new List<String> { 'one', 'two','Three','four'};
mystring.sort();
system.debug ('----' + mystrings) // works
but....... 
system.debug('------' + mystring.sort()); // does not work because method return the void....

1 Answer 1

2

With the + you're saying that you'll be appending something to the existing string '-------'. You can't append a void to a string. You can't also debug a void. The name itself tells you that it's a void (doesn't return anything).

You should be calling the method and then outputing the result list:

List<String> myStrings = new List<String> { 'one', 'two','Three','four'};
myStrings.sort();
system.debug(myStrings);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .