# CS0103 — Name does not exist in current context
## Pattern
`error CS0103: The name 'X' does not exist in the current context`
## Root Causes (ordered by likelihood)
1. Accessing a property or method in the wrong `State` (e.g., reading `BarsArray` before `State.DataLoaded`)
2. Missing `this.` prefix on an inherited member (rare in C# but occurs with shadowed names)
3. Typo in member name (case-sensitive mismatch)
4. Missing `using` directive for an extension method or static class
## Fix Strategies
### Strategy 1: Wrong State guard
- **Detect**: Error references a property like `BarsArray`, `Instrument`, `Close`, `High`, `Low`, `Open`, `Volume`, `CurrentBar`, or any `Series<T>` field. The access is inside `OnStateChange` at a state earlier than `State.DataLoaded`, or inside a constructor.
- **Lookup**: NT8 state machine order: `SetDefaults` → `Configure` → `DataLoaded` → `Historical` → `Realtime` → `Terminated`. Data properties are only valid from `State.DataLoaded` onward.
- **Fix**: Wrap the access in the correct state guard or move initialization to `State.DataLoaded`.
- **Verify**: Confirm the property is only accessed after `State.DataLoaded` is reached.
```csharp
// Wrong: accessing BarsArray in Configure
if (State == State.Configure)
{
int count = BarsArray[0].Count; // CS0103 or NullReferenceException
}
// Correct: defer to DataLoaded
if (State == State.DataLoaded)
{
int count = BarsArray[0].Count;
}
```
### Strategy 2: Typo in member name
- **Detect**: The name in the error message is close to but not identical to a known NT8 or DEEP6 member (e.g., `currentbar` vs `CurrentBar`, `closeprice` vs `Close`).
- **Lookup**: NT8 NinjaScript API uses PascalCase for all public members. Check the exact spelling in the NT8 API reference or the base class source.
- **Fix**: Correct the casing or spelling to match the declared member name exactly.
- **Verify**: The corrected name resolves without error and matches the intended API member.
### Strategy 3: Missing `using` directive for extension method
- **Detect**: The name is a method that exists in a known namespace but the file lacks the corresponding `using` statement. Common with LINQ (`Where`, `Select`, `FirstOrDefault`) or NT8 extension helpers.
- **Lookup**: Identify which namespace declares the method. For LINQ: `System.Linq`. For NT8 helpers: `NinjaTrader.NinjaScript`.
- **Fix**: Add the missing `using` at the top of the file.
- **Verify**: The `using` is present and the method resolves.
```csharp
// Add at top of file
using System.Linq;
```
### Strategy 4: Scope issue — variable declared in inner block
- **Detect**: The name was declared inside an `if`, `for`, or `using` block and is referenced outside that block.
- **Lookup**: Trace the variable declaration site. If it's inside a narrower scope than the usage, the fix is to hoist the declaration.
- **Fix**: Move the variable declaration to the enclosing scope (method or class level as appropriate).
- **Verify**: The variable is declared before its first use and in a scope that covers all usages.
## NT8-Specific State→Property Availability Map
| Property / Method | Earliest valid State |
|-------------------|----------------------|
| `BarsArray` | `State.DataLoaded` |
| `Instrument` | `State.Configure` |
| `Close`, `High`, `Low`, `Open`, `Volume` | `State.DataLoaded` |
| `CurrentBar` | `State.DataLoaded` (inside `OnBarUpdate`) |
| `Series<T>` fields (custom) | `State.DataLoaded` (after `AddDataSeries` or `new Series<T>(this)`) |
| `Account` | `State.DataLoaded` (strategies only) |
| `Position` | `State.DataLoaded` (strategies only) |
## Example Fix
```diff
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "DEEP6Footprint";
}
- else if (State == State.Configure)
- {
- // CS0103: 'Close' does not exist in current context
- double seed = Close[0];
- }
+ else if (State == State.DataLoaded)
+ {
+ // Correct: Close is valid from DataLoaded onward
+ double seed = Close[0];
+ }
}
```