+ Start a Discussion
SalesRedSalesRed 

Apex/Visualforce Trying to Use a Case Statement to Cast a Class Type

Hello,

 

I am trying to check within a visualforce component if an attribute is of a particular type. If it is I then wish to cast the attrbiute to a different class type.  The type is a String which is used for comparison.  I thought a Case statement could be used for this but I'm getting errors and wondering now if it cannot be used

 

First of all I tried the following

 

{!
Case(detailItem.type, 'NUMB',(ItemNumb)detailItem, 'TEXT',(ItemText)detailItem, detailItem)
}

 

but this gives syntax errors.  Missing ')'

 

From this I cannot determine where I am missing the ')'

 

To see if it was a result of where I try to cast the class I reoved the cast and tried the following (just for debugging)

 

{!
Case(detailItem.type, 'NUMB',detailItem, 'TEXT'detailItem, detailItem)
}

 

but this gives an error as follows

 

Description Resource Path Location Type
Save error: Incorrect parameter for function 'Case()'. Expected Object, received Text 

 

My expression is therefore a string but the return type is an object.  Is this not allowed?  If so woukd an if statement allow this?

 

On trying the following I received a Missing ')' error also.

 

{!if(detailItem.type=='NUMB',(ItemNumb)detailItem,detailyItem)}   

 

Any suggestions on whether a cast like this can be done in an if/case statement, and where I am missing  ')' if so are welcome.

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

I doubt you can do casting inside a CASE function.  You'll probably have to write a controller for this component to accomplish what you want.