How to create functions to be reused in C#
I am working on the MACD. The MACD function is getting too long and I am repeating code. I want to create a function SMA and call it to the MACD function. I created this class but I get an error :
namespace myBackEnd
{
public class SMA
public decimal SMA (Queue<Models.DateClose> queue, int period) <--- error here
decimal average, sum=0;
for (int i = 0; i < period; i++)
Models.DateClose dateClose;
dateClose = queue.Dequeue();
sum += dateClose.Close;
return average = sum/period;
I get an error 'member name cannot be the same as enclosing name". How can I fix this?
c#
add a comment |
I am working on the MACD. The MACD function is getting too long and I am repeating code. I want to create a function SMA and call it to the MACD function. I created this class but I get an error :
namespace myBackEnd
{
public class SMA
public decimal SMA (Queue<Models.DateClose> queue, int period) <--- error here
decimal average, sum=0;
for (int i = 0; i < period; i++)
Models.DateClose dateClose;
dateClose = queue.Dequeue();
sum += dateClose.Close;
return average = sum/period;
I get an error 'member name cannot be the same as enclosing name". How can I fix this?
c#
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22
add a comment |
I am working on the MACD. The MACD function is getting too long and I am repeating code. I want to create a function SMA and call it to the MACD function. I created this class but I get an error :
namespace myBackEnd
{
public class SMA
public decimal SMA (Queue<Models.DateClose> queue, int period) <--- error here
decimal average, sum=0;
for (int i = 0; i < period; i++)
Models.DateClose dateClose;
dateClose = queue.Dequeue();
sum += dateClose.Close;
return average = sum/period;
I get an error 'member name cannot be the same as enclosing name". How can I fix this?
c#
I am working on the MACD. The MACD function is getting too long and I am repeating code. I want to create a function SMA and call it to the MACD function. I created this class but I get an error :
namespace myBackEnd
{
public class SMA
public decimal SMA (Queue<Models.DateClose> queue, int period) <--- error here
decimal average, sum=0;
for (int i = 0; i < period; i++)
Models.DateClose dateClose;
dateClose = queue.Dequeue();
sum += dateClose.Close;
return average = sum/period;
I get an error 'member name cannot be the same as enclosing name". How can I fix this?
c#
c#
asked Nov 12 '18 at 21:07
Jam66125Jam66125
596
596
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22
add a comment |
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22
add a comment |
2 Answers
2
active
oldest
votes
That's cause you can't have a method name defined same as the class name (Exception: Constructor). Compiler may think it's a constructor but it can't be since the member function has return type and constructor can't. So essentially provide a separate meaningful name like
public decimal SMAMethod (Queue<Models.DateClose> queue, int period)
add a comment |
In C# you are not allowed to have methods named the same as the type you are putting them inside. So since your class is named SMA, you aren't allowed to name any method inside there SMA. Try another name
public decimal DoStuff (Queue<Models.DateClose> queue, int period)
(Obviously not DoStuff, since that isn't really self-documenting, but something relevant to your use case)
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
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%2f53270119%2fhow-to-create-functions-to-be-reused-in-c-sharp%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's cause you can't have a method name defined same as the class name (Exception: Constructor). Compiler may think it's a constructor but it can't be since the member function has return type and constructor can't. So essentially provide a separate meaningful name like
public decimal SMAMethod (Queue<Models.DateClose> queue, int period)
add a comment |
That's cause you can't have a method name defined same as the class name (Exception: Constructor). Compiler may think it's a constructor but it can't be since the member function has return type and constructor can't. So essentially provide a separate meaningful name like
public decimal SMAMethod (Queue<Models.DateClose> queue, int period)
add a comment |
That's cause you can't have a method name defined same as the class name (Exception: Constructor). Compiler may think it's a constructor but it can't be since the member function has return type and constructor can't. So essentially provide a separate meaningful name like
public decimal SMAMethod (Queue<Models.DateClose> queue, int period)
That's cause you can't have a method name defined same as the class name (Exception: Constructor). Compiler may think it's a constructor but it can't be since the member function has return type and constructor can't. So essentially provide a separate meaningful name like
public decimal SMAMethod (Queue<Models.DateClose> queue, int period)
answered Nov 12 '18 at 21:09
RahulRahul
62.2k124481
62.2k124481
add a comment |
add a comment |
In C# you are not allowed to have methods named the same as the type you are putting them inside. So since your class is named SMA, you aren't allowed to name any method inside there SMA. Try another name
public decimal DoStuff (Queue<Models.DateClose> queue, int period)
(Obviously not DoStuff, since that isn't really self-documenting, but something relevant to your use case)
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
add a comment |
In C# you are not allowed to have methods named the same as the type you are putting them inside. So since your class is named SMA, you aren't allowed to name any method inside there SMA. Try another name
public decimal DoStuff (Queue<Models.DateClose> queue, int period)
(Obviously not DoStuff, since that isn't really self-documenting, but something relevant to your use case)
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
add a comment |
In C# you are not allowed to have methods named the same as the type you are putting them inside. So since your class is named SMA, you aren't allowed to name any method inside there SMA. Try another name
public decimal DoStuff (Queue<Models.DateClose> queue, int period)
(Obviously not DoStuff, since that isn't really self-documenting, but something relevant to your use case)
In C# you are not allowed to have methods named the same as the type you are putting them inside. So since your class is named SMA, you aren't allowed to name any method inside there SMA. Try another name
public decimal DoStuff (Queue<Models.DateClose> queue, int period)
(Obviously not DoStuff, since that isn't really self-documenting, but something relevant to your use case)
edited Nov 12 '18 at 22:41
Tim
2,1541715
2,1541715
answered Nov 12 '18 at 21:34
Otis HowellOtis Howell
111
111
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
add a comment |
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
1
1
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
Can you explain how this solution works?
– rassar
Nov 12 '18 at 22:00
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.
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%2f53270119%2fhow-to-create-functions-to-be-reused-in-c-sharp%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
You can't have a method (/function) with the same name as the class it belongs to. That naming, a "function" with the same name as the class is reserved for constructors (which your SMA.SMA is not. How can you fix it... Change the name
– Flydog57
Nov 12 '18 at 21:11
Fly, then how do I call the method? can I have the method all by itself and not in a class?
– Jam66125
Nov 12 '18 at 21:22