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
sebipinnsebipinn 

Overloading other methods

Is there a way to set up a parameter in a method to accept anyType? almost how when you use Double.valueOf(anyType) works. I want to do this to my own method where i can accept a string, an integer, or double and return a double after i do some transformations to it.

 

Is this possible?

Best Answer chosen by Admin (Salesforce Developers) 
sebipinnsebipinn

Ah nevermind. Looks like it didn't like me doing the following:

 

public static Double someMethod(Double x) { .. }

public static Double someMethod(Integer x) { .. }

public static Double someMethod(String x) { .. }

 

It was complaining when i tried to test with an Integer, but apparently Double accepts integers, so I simply had to leave off the Integer one...

 

public static Double someMethod(Double x) { .. }

public static Double someMethod(String x) { .. }

 

works just fine

All Answers

sebipinnsebipinn

Ah nevermind. Looks like it didn't like me doing the following:

 

public static Double someMethod(Double x) { .. }

public static Double someMethod(Integer x) { .. }

public static Double someMethod(String x) { .. }

 

It was complaining when i tried to test with an Integer, but apparently Double accepts integers, so I simply had to leave off the Integer one...

 

public static Double someMethod(Double x) { .. }

public static Double someMethod(String x) { .. }

 

works just fine

This was selected as the best answer
Avinaash BAvinaash B

Hi , I have a requirement. I have a numeric field in Account called 'Total_Sum_of_Contacts__c' (parent field) and an other numeric field in Contact called 'Num__c' (child field). The numeric field(Total_Sum_of_Contacts__c) in Account should show the total of all the contacts' numeric field(Num__c). I need to write a trigger. Please someone suggest me a good trigger.
Lynnell Emmanuel NeriLynnell Emmanuel Neri
Hello. I believe you can do this using formula. Lynnell Emmanuel Neri Back-End Web Developer lynnellemmanuel@gmail.com
UC InnovationUC Innovation
You can pass in an object type parameter and cast it arbitrarily inside the function for different situations like so
 
public static void funct(object parameter) {
		if (situation1) {
                        string castParam = (String)parameter; 
                        // do something here
                } else if (situation2) {
                        Double castParam = (Double)parameter;
                        // do something here
                } else {
                        Boolean castParam = (Boolean)parameter;
                        // do something here
                }
	}

instead of overloading the function you can make it behave the way you want using conditionals and will accept different single param type

Hope this helps!

AM
SF YodhaSF Yodha
Hi,
Can I overload a method with different number of parameters ?