Reputation: 1
I have a list which contains the 'Reference' class as a generic type. So I should be able to fill my list with children of the 'Reference' class, but the compiler reports an error:
'naturalCalc.Enginee.Reference'
does not contain a definition for'Opener'
and no extension method'Opener'
accepting a first argument of type'naturalCalc.Enginee.Reference'
could be found (are you missing a using directive or an assembly reference?) (CS1061)ToPostfix.cs:27,34
The switch code in the 'ToPostfix' class ensures that the item has 'Opener' property in the second case.
// The 'MyBrackets' class which contains the 'Opener' property.
public class MyBrackets : naturalCalc.Enginee.Reference
{
private bool opener;
public MyBrackets( bool opener )
{
this.opener = opener;
}
public bool Opener { get { return this.Opener; } }
}
// The 'ToPostfix' class in which the error is taken place.
class ToPostfix
{
List<Reference> infix = new List<Reference>();
List<Reference> postfix = new List<Reference>();
public ToPostfix(List<Reference> infixForm)
{
this.infix = infixForm;
foreach (Reference item in this.infix)
{
switch ( item.ToString() )
{
case "naturalCalc.Enginee.MyFloat":
this.postfix.Add(item);
break;
case "naturalCalc.Enginee.MyBrackets":
if (item.Opener)
{
this.postfix.Add(item);
}
break;
}
}
}
}
Upvotes: 0
Views: 133
Reputation: 3996
You are using an List
of Reference
, but defining the Opener
property in the MyBracketClass
. If you want your property to be accessed from a Reference
instance, move it there.
Upvotes: 0
Reputation: 838974
You should try to move the method into the interface if possible.
If this is not possible you can test the type dynamically and cast as follows:
Opener opener = item as Opener;
if (opener != null)
{
if (opener.Opener)
{
this.postfix.Add(item);
}
}
Also your getter doesn't work correctly. You should also learn to use auto-implemented properties:
public class MyBrackets : naturalCalc.Enginee.Reference
{
public MyBrackets(bool opener)
{
this.Opener = opener;
}
public bool Opener { get; private set; } // Auto-implemented property.
}
Upvotes: 1
Reputation: 7692
This is not a complete answer, but I believe that there's an error on your MyBrackets class.
opener
and Opener
are not the same property.
I believe it should be:
public class MyBrackets : naturalCalc.Enginee.Reference
{
public MyBrackets(bool opener)
{
this.Opener = opener;
}
public bool Opener
{
private set { this.Opener = value; }
get { return this.Opener; }
}
}
Regards
Upvotes: 0