Pass different data types into function that returns nothing
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
add a comment |
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
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 aMyClass
instance and one that takes adouble
.
– 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 ofdouble
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
add a comment |
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
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
c# class methods
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 aMyClass
instance and one that takes adouble
.
– 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 ofdouble
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
add a comment |
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 aMyClass
instance and one that takes adouble
.
– 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 ofdouble
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
add a comment |
3 Answers
3
active
oldest
votes
You can overload the method MyMethod like below.
public void MyMethod(MyClass x)
public void MyMethod(double x)
OP "but modifies aninstance variable
of thecurrent object
" - how is thing going to work fordouble
which is passed by value?
– MickyD
Nov 12 at 6:49
add a comment |
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
in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
– Anupam Singh
Nov 12 at 5:32
add a comment |
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can overload the method MyMethod like below.
public void MyMethod(MyClass x)
public void MyMethod(double x)
OP "but modifies aninstance variable
of thecurrent object
" - how is thing going to work fordouble
which is passed by value?
– MickyD
Nov 12 at 6:49
add a comment |
You can overload the method MyMethod like below.
public void MyMethod(MyClass x)
public void MyMethod(double x)
OP "but modifies aninstance variable
of thecurrent object
" - how is thing going to work fordouble
which is passed by value?
– MickyD
Nov 12 at 6:49
add a comment |
You can overload the method MyMethod like below.
public void MyMethod(MyClass x)
public void MyMethod(double x)
You can overload the method MyMethod like below.
public void MyMethod(MyClass x)
public void MyMethod(double x)
answered Nov 12 at 5:29
Nishil
2258
2258
OP "but modifies aninstance variable
of thecurrent object
" - how is thing going to work fordouble
which is passed by value?
– MickyD
Nov 12 at 6:49
add a comment |
OP "but modifies aninstance variable
of thecurrent object
" - how is thing going to work fordouble
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
add a comment |
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
in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
– Anupam Singh
Nov 12 at 5:32
add a comment |
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
in this example using 'MyMethod' method in 2 different forms. 1 . for MyClass, 2. for double
– Anupam Singh
Nov 12 at 5:32
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 adouble
.– 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