Quick Answer
The C# error CS0127 ("Since 'method' returns void, a return keyword must not be followed by an object expression") means that a method declared void has a line like return 1; or return true;. A void method returns nothing, so the return must stand alone: return;. The Microsoft reference gives the example in one line: void Show() { return 1; } is CS0127, and void Show() { return; } is the fix.
Its mirror image is CS0126 ("An object of a type convertible to 'type' is required"): a method that is declared to return a value has a bare return;. The fix is return 0; or whatever value the type needs. In a cBot you meet these two errors mostly when you move code from MQL5, because MQL5 handlers such as OnInit() return an int and cTrader handlers such as OnStart() return void. This post shows both errors, the MQL5 habits behind them, and a cBot that prints a result you can check. It is not the same as CS0161 ("not all code paths return a value", see CS0161).
From the author: I convert MQL5 to cTrader C#. The first thing that breaks in a converted
OnInit()is the last line,return(INIT_SUCCEEDED);. It is a good error: it tells you that a handler in cTrader is not a handler in MQL5, and that the way to stop a cBot at start is different too.
Key Facts
| Error | Message (Microsoft reference) | Cause | Fix |
|---|---|---|---|
| CS0127 | Since 'method' returns void, a return keyword must not be followed by an object expression | A value returned from a void method or property setter | Write return;, or change the return type |
| CS0126 | An object of a type convertible to 'type' is required | A return; without a value in a method that returns a value | Return a value of that type |
| CS0161 | 'method': not all code paths return a value | A branch reaches the end of a value-returning method without return | Add the missing return, see CS0161 |
The messages and the three one-line examples come from the Microsoft C# reference on return errors. They describe the language, not cTrader. From the cTrader Automate reference I confirmed only that OnStart(), OnBarClosed() and OnStop() are protected override void methods.
CS0127: A Value Returned From a void Method
protected override void OnStart()
{
if (Bars.Count < 50)
return false; // CS0127: OnStart returns void
Print("Enough bars");
return true; // CS0127
}
Two fixes, depending on what you meant. If you wanted to stop the handler early, use a bare return;:
protected override void OnStart()
{
if (Bars.Count < 50)
{
Print("Not enough bars");
return;
}
Print("Enough bars");
}
If you wanted to answer a question ("do we have enough bars?"), the answer belongs in a separate method that returns bool, which the handler calls:
private bool HasEnoughBars()
{
return Bars.Count >= 50;
}
protected override void OnStart()
{
if (!HasEnoughBars())
{
Print("Not enough bars");
return;
}
}
The MQL5 Habit: return(INIT_SUCCEEDED)
In MQL5, OnInit() returns an int, and return(INIT_FAILED); tells the terminal to unload the EA. Converted line by line, that becomes this:
protected override void OnStart() // converted from int OnInit()
{
if (Symbol.PipSize <= 0)
return 1; // CS0127 (INIT_FAILED in MQL5)
return 0; // CS0127 (INIT_SUCCEEDED in MQL5)
}
There is no return code in a cTrader handler. The two things the code was doing in MQL5 are done in other ways:
| MQL5 | cTrader C# |
|---|---|
| return(INIT_SUCCEEDED); at the end of OnInit() | Nothing: let OnStart() reach its end |
| return(INIT_FAILED); when a check fails | Print the reason and call Stop();, then return; |
| return(INIT_PARAMETERS_INCORRECT); | The same: print the reason, Stop();, return; |
protected override void OnStart()
{
if (Symbol.PipSize <= 0)
{
Print("Invalid symbol data, stopping");
Stop();
return;
}
}
Check Stop() in your version of cTrader Automate: it is a member of Robot, but I have not built this code.
The Same Habit in OnTick and Helper Methods
MQL5 programmers often write helpers that return a status, and a handler that returns the result of a helper. In C# the return type must match what the method says:
private void OpenBuy() // void
{
// ... send the order ...
return true; // CS0127: the method says void
}
If the caller needs to know whether the order went through, change the signature, not the return:
private bool OpenBuy()
{
TradeResult result = ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "Test");
return result.IsSuccessful;
}
The members of TradeResult are described in order failed: TradeResult error codes.
CS0126: A return Without a Value
private double GetSpreadInPips()
{
if (Symbol.Spread < 0)
return; // CS0126: a double is required
return Symbol.Spread / Symbol.PipSize;
}
The first return; leaves a method that must give a number. Return a number that means "no value":
private double GetSpreadInPips()
{
if (Symbol.Spread < 0)
return double.NaN;
return Symbol.Spread / Symbol.PipSize;
}
If you use double.NaN as the marker, the caller must test it with double.IsNaN(x), because x == double.NaN is never true. Do not return 0 for "unknown" when a real value can be zero.
Property Setters
The Microsoft page says CS0127 also applies to a property setter. A setter returns nothing, so return value; in a set block is the same error. If you wrote a setter that computes something, move the work to a method that returns a value.
A cBot That Prints a Result You Can Check
This cBot does not trade. It uses a bool helper and a bare return:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class ReturnTest : Robot
{
private bool HasEnoughBars(int needed)
{
return Bars.Count >= needed;
}
private double SafeSpreadPips()
{
if (Symbol.PipSize <= 0)
return double.NaN;
return Symbol.Spread / Symbol.PipSize;
}
protected override void OnStart()
{
if (!HasEnoughBars(1000000))
{
Print("Not enough bars for 1,000,000: stopping early");
return;
}
Print("This line is not reached on a normal chart");
}
protected override void OnStop()
{
Print("Spread in pips: {0}", SafeSpreadPips());
}
}
}
On a normal chart I expect the first Print and then the stop message with the spread (the cBot keeps running until you stop it, so the second line comes at stop). I have not built the cBot. If you change HasEnoughBars to void and keep its return, the build fails with CS0127.
Which Return Do I Need?
| You want | Write |
|---|---|
| To leave a void method early | return; |
| To answer yes or no | A method that returns bool, and return x > 0; |
| To give back a number | A method that returns double or int |
| To signal "no value" | double.NaN, or a bool plus an out parameter (TryX) |
| To stop the cBot | Stop(); then return; |
Similar cTrader Messages
| Message | What it means | Where to look |
|---|---|---|
| CS0127 | A value is returned from a void method | This post |
| CS0126 | A return has no value in a value method | This post |
| CS0161 | A path has no return | CS0161 |
| CS0029 | A value cannot be converted to the target type | CS0029 |
| CS0266 | double cannot be converted to int | CS0266 |
| CS0115 | No method found to override | CS0115 |
Checklist: What to Check When You See CS0127 or CS0126
- Read the method name in the message and look at its return type.
- Is the method
void? Then thereturnmust stand alone. - Does the caller need an answer? Then change the return type and the caller.
- Is it a handler (
OnStart,OnStop,OnBarClosed)? It returnsvoid: no status codes. - Convert
INIT_FAILEDto a printed reason,Stop();andreturn;. - Check every bare
return;in a method that returns a value. - Pick a clear "no value" (
double.NaN, abool), not a made-up zero. - Build again, and fix the next error.
Common Mistakes
- Copying
return(INIT_SUCCEEDED);from MQL5. - Returning
trueorfalsefrom avoidhelper. - Writing
return;in a method that must return a number. - Using
0as "unknown" when 0 is a real value. - Comparing with
double.NaNusing==. - Mixing up CS0127 and CS0161.
FAQ
Related Guides
- cTrader error CS0161: not all code paths return a value: the missing return.
- cTrader error CS0029: cannot implicitly convert type: a value of the wrong type.
- cTrader error CS0266: cannot implicitly convert double to int: numbers and types.
- cTrader error CS0115: no suitable method found to override: handler names and signatures.
- cTrader error CS1002 and CS1513: semicolon or brace expected: syntax errors.
- cTrader error CS0120: an object reference is required: static and instance members.
- Pine Script to cTrader C# cBot: the full conversion process.
- What to check in a trading bot before going live: a pre-live checklist.
Fix Compile Errors Without Guessing
If a cBot does not build, paste the code and the error message into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. Before a bot goes live, the Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always build the code in cTrader Automate and test it on a demo account before using it on a live account.