+ Start a Discussion
TechSteveTechSteve 

Arrays format.

Hi

    Trying to use an array to give a field a value based on the url element 'form' which i have used from a previous page.

I cant seem to get the right format for the for loop, ths code below throws up, Compile Error: Loop must iterate over a collection type: Integer. Am a bit lost as to what to do. Please help if you can, thanks.

 

   String[] mystrings = new String[]{'SIS__c', 'MSQ__c', 'PDQ_39__c'};
    String[] mystring2 = new String[]{'StrImp', 'MSquiz', 'PDQ'};
    public void PNExtMethod(){
        for(Integer i : mystrings.size()){
            if (ApexPages.currentPage().getParameters().get('form') == mystrings[i]){
                System.debug(i);
                pn.Relobject__c = mystring2[i];
            }
        }
    }*

Best Answer chosen by Admin (Salesforce Developers) 
ChizChiz

Man,

mystrings.size() - it's JUST ONE INTEGER DIGIT. It's not an array.

Instead of

 for(Integer i : mystrings.size()){

 you have to write

 for(Integer i=0; i<mystrings.size(); i++){

 I hope it will help you.

All Answers

ChizChiz

Man,

mystrings.size() - it's JUST ONE INTEGER DIGIT. It's not an array.

Instead of

 for(Integer i : mystrings.size()){

 you have to write

 for(Integer i=0; i<mystrings.size(); i++){

 I hope it will help you.

This was selected as the best answer
TechSteveTechSteve

Thank you

Sorry new to this and limited experience elsewhere.

I wonder if you could be so kind as to help a little more. The method needed to display the result is not coming to me.

I now have :

    String[] mystrings = new String[]{'SIS__c', 'MSQ__c', 'PDQ_39__c'};
    String[] mystring2 = new String[]{'StrImp', 'MSquiz', 'PDQ'};
    public void PNExtMethod(){
        for(Integer i=0; i<mystrings.size(); i++){
            if (ApexPages.currentPage().getParameters().get('form') == mystrings[i]){
                System.debug(i);
                pn.Relobject__c = mystring2[i];
                i = 100;
            }
        }
    }

 

With the i set to something that will never be reached to make it hop out the loop when it has found the right value but the value isn't being given to the field pn.Relobject__c ?

Do i need to execute this higher up in the controller or does it not matter?

Do i need to change or remove the void statement?

Please your patience is appreciated.

 

Steve

ChizChiz

Instead of

i = 100;

 use

break;

 So the code wil llook like

String[] mystrings = new String[]{'SIS__c', 'MSQ__c', 'PDQ_39__c'};
    String[] mystring2 = new String[]{'StrImp', 'MSquiz', 'PDQ'};
    public void PNExtMethod(){
        for(Integer i=0; i<mystrings.size(); i++){
            if (ApexPages.currentPage().getParameters().get('form') == mystrings[i]){
                System.debug(i);
                pn.Relobject__c = mystring2[i];
                break;
            }
        }
    }

 break; will stop looping. So you will finish iteratign in loop and start doing stuff after the loop.

TechSteveTechSteve

Thanks got it all sorted now.

sorry it was so simple but thats the sort of thing I keep falling over on.

 

 

Thanks again

 

Steve