views:

194

answers:

3

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();

+10  A: 
Justin Niessner
thanks a ton for ur quick reply!!!! thanks again !!!
isthatacode
No problem. Updated now that you changed the question around.
Justin Niessner
+1  A: 

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.

Jesse C. Slicer
thank u so much !!!! I highly appriciate ur help . like u said i was actually mixing up the constructor syntax and property initialization syntax
isthatacode
Glad to be of help. Happy coding!
Jesse C. Slicer
+2  A: 

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 };
Thomas
thanks !! my mistake, it was a typo.. have fixed the question now.
isthatacode