What am I doing wrong? Can't access methods.  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
ManuelVene
Posts: 17
Joined: 12 Jun 2015
Been thanked: 1 time

What am I doing wrong? Can't access methods.

Postby ManuelVene » 12 Nov 2015

Hello,

when I create a function inside mi signale, let's say something like this:

Code: Select all

//...

protected override void CalcBar()
{
//...
int a = One();
}

public static int One()
{
//...
return 1;
}

//...
Inside the body of the functions, in this case One(), I can't access stuff like Ouput.WriteLine(), or Bars.TimeValue.
It's quite annoying because sometimes the way to go around this is not an easy one.

Any idea of why?

Thanks!! :D

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: What am I doing wrong? Can't access methods.

Postby JoshM » 13 Nov 2015

Inside the body of the functions, in this case One(), I can't access stuff like Ouput.WriteLine(), or Bars.TimeValue.
It's quite annoying because sometimes the way to go around this is not an easy one.

Any idea of why?
Because `One()` is a static method. For more, see static classes and methods @ MSDN.

This would work:

Code: Select all

public int One()
{
Output.WriteLine("This works");

return 1;
}

private int Two()
{
Output.WriteLine("If the method only needs to be used in the class..");
Output.WriteLine(".. you can just declare it as private.");

return 2;
}

ManuelVene
Posts: 17
Joined: 12 Jun 2015
Been thanked: 1 time

Re: What am I doing wrong? Can't access methods.

Postby ManuelVene » 13 Nov 2015

Ok, it's true, now that I tried it works with every non static methods as you said.
The problem is when I want to access a method within another method, in that case the first has to be static, but still I many times need the output for many reasons.

To get around this I worote a class called Log, with method Log.Print(string s), that prints information in a file, and since Log is static it can be access from everywhere, and information stored in file, but still, it doesn't sound like a nice solution, especially because it doesn't solve the problem of not being able to access Bars info.
There's actually no way to access those information from a static Method?

Thanks!!

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: What am I doing wrong? Can't access methods.  [SOLVED]

Postby Henry MultiСharts » 16 Nov 2015

Hello ManuelVene,

You can pass all required data into your static class as parameters:

Code: Select all

public static int One(IOutput _out, IInstrument _bars)
{
_out.WriteLine("bla-bla-bla, {0}", _bars.OpenValue + _bars.CloseValue);
//...
return 1;
}

ManuelVene
Posts: 17
Joined: 12 Jun 2015
Been thanked: 1 time

Re: What am I doing wrong? Can't access methods.

Postby ManuelVene » 17 Nov 2015

Thank you very much!

:) :)

trd001
Posts: 7
Joined: 25 Jan 2019
Has thanked: 1 time
Been thanked: 1 time

Re: What am I doing wrong? Can't access methods.

Postby trd001 » 28 Dec 2020

Hi, I have the same problem but struggeling to call the kind of method that Henry suggested with the Interfaces as parameters.
Could someone please show an example of how to call the method including definition of the parameters passed into the method? Thanks a lot in advance!

trd001
Posts: 7
Joined: 25 Jan 2019
Has thanked: 1 time
Been thanked: 1 time

Re: What am I doing wrong? Can't access methods.

Postby trd001 » 29 Dec 2020

I found it out, just in case someone else needs it too:

Code: Select all

One( this.Output , this.Bars );
or

Code: Select all

One( this.Output , this.BarsOfData(1) );
Can I thank myself? ;-)


Return to “MultiCharts .NET”