Reputation: 1641
I have an automatic property
public int GearCount { get; set; }
when i try to initialize it like this-
Tandem t = new Tandem(GearCount = 5);
It gives an error as below
The name 'GearCount' does not exist in the current context
Whats wrong here ? Also if i do normal intantiation it works fine. Tandem t = new Tandem();
Upvotes: 2
Views: 368
Reputation: 245429
We need the rest of your code
You show us an auto-property called HasToolkit
but the problem you're having has nothing to do with HasToolkit
.
It doesn't look like you have an auto-property on your Tandem
class called GearCount
.
With the question fixed, it looks like you might just have some syntax issues.
If your Tandem
class looks like:
public class Tandem
{
public bool HasToolkit {get; set;}
public int GearCount {get; set;}
}
Then your initialization code would be:
Tandem t = new Tandem() { GearCount = 5 };
Or:
Tandem t = new Tandem() { GearCount = 5, HasToolkit = true };
Upvotes: 10
Reputation: 20157
That's because the property is named HasToolKit
and is of type bool
, not named GearCount
with a type of int
.
To that end, you also seem to be mixing constructor and property initializer syntax. What you'd want in the calling case is:
Tandem t = new Tandem {GearCount = 5};
The definition of Tandem would need to have something of the sort of:
public int GearCount { get; set; }
Not quite sure what HasToolKit
means in the scheme of things.
Upvotes: 1
Reputation: 64645
The property you have declared is not the same name nor type as the one you are trying to set in the initializer. In addition, you need to use braces instead of parentheses when you want to use initializers:
var t = new Tandem{ HasToolKit = true };
Upvotes: 2