function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepu BDeepu B 

Method error

User-added image

For the above task i have created a class as below,


public class StringArrayTest 
{
  public string[] arraynames;
  public StringArrayTest()
  {
   arraynames=new string[]{};
  }
  public string[] generateStringArray(integer num)
  {
  
  for(integer i=0;i<num;i++)
  {
   string  s='Test'+i;
   /*arraynames.add(s);*/
   system.debug(s);
  }
  return arraynames; 
  }
}




it showing below error


User-added image


where i did mistake.?
Best Answer chosen by Deepu B
Rahul Sangwan7341Rahul Sangwan7341
Hi Deepu,

Try this, this will work:
public class StringArrayTest {

    public static List<String> generateStringArray(Integer i){
        List<String> str=new List<String>();
        for(Integer j=0;j<i;j++){
            str.add('Test '+j);
        }
        return str;
    }
}

As your method should be static as it is given.

Please mark this as Best Answer if it helps to solve your problem.

Let me know if you have any issue in that

All Answers

Gaurav KheterpalGaurav Kheterpal
You are not really doing anything in your generateStringArray() method. The proper way is to do something like this
 
public class StringArrayTest {
    
    public static List<String> generateStringArray(Integer n)
    {
        List<String> myArray = new List<String>();
        for(Integer i=0;i<n;i++)
        {
           myArray.add('Test '+i);
           System.debug(myArray[i]);
        }
        return myArray;
    }
}
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others and improve the overall quality of Discussion Forums.

Gaurav Kheterpal
Certified Force.com Developer| Salesforce Mobile Evangelist| Developer Forums Moderator| Dreamforce Speaker
 
Rahul Sangwan7341Rahul Sangwan7341
Hi Deepu,

Try this, this will work:
public class StringArrayTest {

    public static List<String> generateStringArray(Integer i){
        List<String> str=new List<String>();
        for(Integer j=0;j<i;j++){
            str.add('Test '+j);
        }
        return str;
    }
}

As your method should be static as it is given.

Please mark this as Best Answer if it helps to solve your problem.

Let me know if you have any issue in that
This was selected as the best answer
Deepu BDeepu B
Thank you and if possible could you please give me some assignments to work out.?
Rahul Sangwan7341Rahul Sangwan7341
Hi Deepu, 

You can go ahead with trailhead.......its good for practise purpose........and read apex developer guide.