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
minkeshminkesh 

how to know Data Type?

Hello All,

                I have created List<Object> objList = new List<Object>();

                Now I have requirement to know the what is the datatype of the value stored in this list.

                How we get to know the datatype of the stored value ? Is there any method ?

                i.e. objList = {'hello',1234.56,12};

                how we get to know that 2nd value in the list is Number Or decimal Type?

                Please Help me to solve this issue?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can use the instanceof keyword to determine if the object is a particular type.  Unfortunately this means that you end up with a potentially large if statement.  E.g.

 

Object ele=objList[1];

if (ele instanceof Double)
{
// do something with a double } else if (ele instanceof Integer) {
// do something with an integer } etc

 

All Answers

bob_buzzardbob_buzzard

You can use the instanceof keyword to determine if the object is a particular type.  Unfortunately this means that you end up with a potentially large if statement.  E.g.

 

Object ele=objList[1];

if (ele instanceof Double)
{
// do something with a double } else if (ele instanceof Integer) {
// do something with an integer } etc

 

This was selected as the best answer
minkeshminkesh

Hello bob,

                   Thank you for quick response.

                   I have tried your code and i am able to save the code by applying your changes but it does not appear into my vf page. code is given below:-

                  

                 

object obj = asm1.Account.get(tfc.akritiv__CustomeField__c);
               if(obj == null){
               if(obj instanceof Double){
                  obj =0.0;
               }
               else if(obj instanceof String){
                  obj='-';
               }
               }
              // system.debug('---------obj---'+obj);
               asm1.objList.add(obj); 

Piece Of code of My visual force page is given below : 

 

<apex:repeat value="{!pos.objList}" var="obj">
     <td style="background-color:#fff;border:1px solid gray;text-align:       right;padding: 6px 6px 6px 3px;font-size:12px">
         <apex:outputText value="{!obj}" > </apex:outputText>
     </td>
</apex:repeat>

  and i have Bill to# column which is String so I want to put '-' in this column but i am not able to get this changes.

 

 

bob_buzzardbob_buzzard

Is your type definitely a double or a string?  

minkeshminkesh

Hello Bob,

                    My Bill to column type is String and other column contains Double Value. here is the output of  the code.

 

 

 

Hierarchy

     

 

 

 

 

 

bob_buzzardbob_buzzard

I've just tried the following in my dev org:

 

Double d=1.0;

Object obj=d;

System.debug('### Instanceof = ' + (obj instanceof Double));

 

and the output is:

 

08:22:22.039 (39485000)|USER_DEBUG|[5]|DEBUG|### Instanceof = true

 

so it looks to be working.  Have you added debug to confirm that you definitely have the correct type (e.g. add an else clause with some debug).

 

minkeshminkesh

Hello bob,

                    can Object Data Type contain String data Type ? I am facing error i.e. '-' is not a valid Number. why it is showing me ?

                    

bob_buzzardbob_buzzard

That implies that you are trying to store a string into a numeric (Double/Decimal/Integer/Long).

 

I've just tried the following in my dev org and it works correctly:

 

String s='Hello';

Object obj=s;

System.debug('### Instanceof string = ' + (obj instanceof String));
System.debug('### Instanceof double = ' + (obj instanceof Double));

 

Giving the output:

 

09:23:48.049 (49685000)|USER_DEBUG|[5]|DEBUG|### Instanceof string = true
09:23:48.049 (49795000)|USER_DEBUG|[6]|DEBUG|### Instanceof double = false

 

 

 

minkeshminkesh

Hello bob,

                   One thing i forgot to tell you. My obj value is null so how can i check that ?

bob_buzzardbob_buzzard

Ah, that changes things, as null will return true for instanceof in all cases.  Looking back at your earlier code, if you are using sobject fields, you can use the describe capability to determine the type of the field.

 

If you aren't using sobject fields, I seem to recall doing something along these lines a while ago where I used exception handling to attempt to set a string value, if that failed try a double value etc.  I can't remember if that was a total success or not though.

minkeshminkesh

Hello bob,

                  Thank you for helping me. Now i know i can not do it using instanceOf so i have to do it using schema.describe.

minkeshminkesh

Hello bob,

                   I am facing another problem.As i said i have List<Object> which contains (162, 0.0, 0.00, 0.0, - ) values but when i am trying to display it in visualforce page inside repeat it shows me following error. what is the reason behind it?

 

  The value '-' is not a valid number. 

 



bob_buzzardbob_buzzard

What markup are you using to display these values?

minkeshminkesh

Hello bob,

                    I am using <apex:outputText> to display the value. I have developped another page which has the same functionality.In that page List<Object> is displayed usind HTML markup. I have tried HTML markup also but still the error is occuring.

bob_buzzardbob_buzzard

I don't see why ouputtext would be trying to turn the string into a number - are you using formatting/nested apex:param tags?

minkeshminkesh

Hello bob,

                   I have requirement to use that but just now i am not using anything.I simply putdown <apex:outputText> {!obj} </apex:outputtext>

 

bob_buzzardbob_buzzard

Can you post the full code - controller and page?