Pass different data types into function that returns nothing










0














I have a class method that returns nothing but modifies an instance variable of the current object, it needs to be able to take in two different data types as the same parameter (An instance of the class or a number).



public class MyClass
{
public void MyMethod([EITHER MyClass x OR double x])

if(x is MyClass)

//do something

else

//do something else











share|improve this question



















  • 1




    The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
    – Klaus Gütter
    Nov 12 at 5:24







  • 2




    Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
    – Flydog57
    Nov 12 at 5:24






  • 2




    In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
    – mjwills
    Nov 12 at 5:24











  • You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
    – MickyD
    Nov 12 at 6:51
















0














I have a class method that returns nothing but modifies an instance variable of the current object, it needs to be able to take in two different data types as the same parameter (An instance of the class or a number).



public class MyClass
{
public void MyMethod([EITHER MyClass x OR double x])

if(x is MyClass)

//do something

else

//do something else











share|improve this question



















  • 1




    The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
    – Klaus Gütter
    Nov 12 at 5:24







  • 2




    Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
    – Flydog57
    Nov 12 at 5:24






  • 2




    In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
    – mjwills
    Nov 12 at 5:24











  • You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
    – MickyD
    Nov 12 at 6:51














0












0








0







I have a class method that returns nothing but modifies an instance variable of the current object, it needs to be able to take in two different data types as the same parameter (An instance of the class or a number).



public class MyClass
{
public void MyMethod([EITHER MyClass x OR double x])

if(x is MyClass)

//do something

else

//do something else











share|improve this question















I have a class method that returns nothing but modifies an instance variable of the current object, it needs to be able to take in two different data types as the same parameter (An instance of the class or a number).



public class MyClass
{
public void MyMethod([EITHER MyClass x OR double x])

if(x is MyClass)

//do something

else

//do something else








c# class methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 5:24









John

11.2k31736




11.2k31736










asked Nov 12 at 5:20









Nick Herman

31




31







  • 1




    The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
    – Klaus Gütter
    Nov 12 at 5:24







  • 2




    Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
    – Flydog57
    Nov 12 at 5:24






  • 2




    In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
    – mjwills
    Nov 12 at 5:24











  • You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
    – MickyD
    Nov 12 at 6:51













  • 1




    The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
    – Klaus Gütter
    Nov 12 at 5:24







  • 2




    Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
    – Flydog57
    Nov 12 at 5:24






  • 2




    In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
    – mjwills
    Nov 12 at 5:24











  • You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
    – MickyD
    Nov 12 at 6:51








1




1




The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
– Klaus Gütter
Nov 12 at 5:24





The solution of @IpsitGaur would then check the type at runtime. Alternative: create two methods with the same name.
– Klaus Gütter
Nov 12 at 5:24





2




2




Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
– Flydog57
Nov 12 at 5:24




Create two overloaded versions of the function (using the same function name), one that takes a MyClass instance and one that takes a double.
– Flydog57
Nov 12 at 5:24




2




2




In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
– mjwills
Nov 12 at 5:24





In 99.5% of cases, you should implement this as two separate methods, as per @Flydog57's suggestion.
– mjwills
Nov 12 at 5:24













You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
– MickyD
Nov 12 at 6:51





You can't pass an instance variable of double to a method and then expect to modify it in such a way that it reflects on the instance variable. double is passed by value
– MickyD
Nov 12 at 6:51













3 Answers
3






active

oldest

votes


















5














You can overload the method MyMethod like below.



public void MyMethod(MyClass x) 


public void MyMethod(double x)







share|improve this answer




















  • OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
    – MickyD
    Nov 12 at 6:49



















1














Use method overloading as:



public class MyClass

public void MyMethod(MyClass x)

// do anything with MyClass


public void MyMethod(double x)

// do anything with double







share|improve this answer






















  • in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
    – Anupam Singh
    Nov 12 at 5:32


















-1














Please check if below may be helpful.



 public static void MyTestObjectClass(object value)

Type getTypeOfParam = value.GetType();//Get type of parameters.
//Handle on getTypeOfParam, you will get namespace.yourType
if(getTypeOfParam==?)

//Then do here








share|improve this answer




















  • urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
    – TheGeneral
    Nov 12 at 5:46










  • I changed class name. The requirement is like same i tried to give solution
    – Nakul
    Nov 12 at 5:46










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256313%2fpass-different-data-types-into-function-that-returns-nothing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














You can overload the method MyMethod like below.



public void MyMethod(MyClass x) 


public void MyMethod(double x)







share|improve this answer




















  • OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
    – MickyD
    Nov 12 at 6:49
















5














You can overload the method MyMethod like below.



public void MyMethod(MyClass x) 


public void MyMethod(double x)







share|improve this answer




















  • OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
    – MickyD
    Nov 12 at 6:49














5












5








5






You can overload the method MyMethod like below.



public void MyMethod(MyClass x) 


public void MyMethod(double x)







share|improve this answer












You can overload the method MyMethod like below.



public void MyMethod(MyClass x) 


public void MyMethod(double x)








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 5:29









Nishil

2258




2258











  • OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
    – MickyD
    Nov 12 at 6:49

















  • OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
    – MickyD
    Nov 12 at 6:49
















OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
– MickyD
Nov 12 at 6:49





OP "but modifies an instance variable of the current object" - how is thing going to work for double which is passed by value?
– MickyD
Nov 12 at 6:49














1














Use method overloading as:



public class MyClass

public void MyMethod(MyClass x)

// do anything with MyClass


public void MyMethod(double x)

// do anything with double







share|improve this answer






















  • in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
    – Anupam Singh
    Nov 12 at 5:32















1














Use method overloading as:



public class MyClass

public void MyMethod(MyClass x)

// do anything with MyClass


public void MyMethod(double x)

// do anything with double







share|improve this answer






















  • in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
    – Anupam Singh
    Nov 12 at 5:32













1












1








1






Use method overloading as:



public class MyClass

public void MyMethod(MyClass x)

// do anything with MyClass


public void MyMethod(double x)

// do anything with double







share|improve this answer














Use method overloading as:



public class MyClass

public void MyMethod(MyClass x)

// do anything with MyClass


public void MyMethod(double x)

// do anything with double








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 5:33









Barr J

5,8591931




5,8591931










answered Nov 12 at 5:29









Anupam Singh

9291222




9291222











  • in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
    – Anupam Singh
    Nov 12 at 5:32
















  • in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
    – Anupam Singh
    Nov 12 at 5:32















in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
– Anupam Singh
Nov 12 at 5:32




in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
– Anupam Singh
Nov 12 at 5:32











-1














Please check if below may be helpful.



 public static void MyTestObjectClass(object value)

Type getTypeOfParam = value.GetType();//Get type of parameters.
//Handle on getTypeOfParam, you will get namespace.yourType
if(getTypeOfParam==?)

//Then do here








share|improve this answer




















  • urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
    – TheGeneral
    Nov 12 at 5:46










  • I changed class name. The requirement is like same i tried to give solution
    – Nakul
    Nov 12 at 5:46















-1














Please check if below may be helpful.



 public static void MyTestObjectClass(object value)

Type getTypeOfParam = value.GetType();//Get type of parameters.
//Handle on getTypeOfParam, you will get namespace.yourType
if(getTypeOfParam==?)

//Then do here








share|improve this answer




















  • urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
    – TheGeneral
    Nov 12 at 5:46










  • I changed class name. The requirement is like same i tried to give solution
    – Nakul
    Nov 12 at 5:46













-1












-1








-1






Please check if below may be helpful.



 public static void MyTestObjectClass(object value)

Type getTypeOfParam = value.GetType();//Get type of parameters.
//Handle on getTypeOfParam, you will get namespace.yourType
if(getTypeOfParam==?)

//Then do here








share|improve this answer












Please check if below may be helpful.



 public static void MyTestObjectClass(object value)

Type getTypeOfParam = value.GetType();//Get type of parameters.
//Handle on getTypeOfParam, you will get namespace.yourType
if(getTypeOfParam==?)

//Then do here









share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 5:44









Nakul

1149




1149











  • urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
    – TheGeneral
    Nov 12 at 5:46










  • I changed class name. The requirement is like same i tried to give solution
    – Nakul
    Nov 12 at 5:46
















  • urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
    – TheGeneral
    Nov 12 at 5:46










  • I changed class name. The requirement is like same i tried to give solution
    – Nakul
    Nov 12 at 5:46















urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
– TheGeneral
Nov 12 at 5:46




urm... MyGenericClass really? besides all that, this is a very messy solution to a simple problem
– TheGeneral
Nov 12 at 5:46












I changed class name. The requirement is like same i tried to give solution
– Nakul
Nov 12 at 5:46




I changed class name. The requirement is like same i tried to give solution
– Nakul
Nov 12 at 5:46

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256313%2fpass-different-data-types-into-function-that-returns-nothing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto