crazy4s
asked on
Operator '||' cannot be applied to operands of type int and bool
Hi all,
I'm new in c#.
I got the error: Operator '||' cannot be applied to operands of type int and bool in one of the class in my prog?
Can anyone figure out what's the issue?
Below is part of my abstract syntax program:
Any help would be greatly appreciated:)
I'm new in c#.
I got the error: Operator '||' cannot be applied to operands of type int and bool in one of the class in my prog?
Can anyone figure out what's the issue?
Below is part of my abstract syntax program:
Any help would be greatly appreciated:)
class Type
{ // Type = int | bool | float | char
readonly static string INTEGER = "int";
readonly static string BOOLEAN = "bool";
readonly static string FLOAT = "float";
readonly static string CHAR = "char";
string id;
Type(string t)
{
id = t;
}
public bool isInt()
{
return id.Equals(INTEGER);
}
public bool isBool()
{
return id.Equals(BOOLEAN);
}
public bool isFloat()
{
return id.Equals(FLOAT);
}
public bool isChar()
{
return id.Equals(CHAR);
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
return indent + "Type(" + id + ")";
}
}
abstract class Expression
{ // Expression = VariableRef | Value | Binary | Unary
VariableRef vr = new VariableRef();
Value v = new Value();
Binary b = new Binary();
Unary u = new Unary();
public String toString()
{
return toStringIndented("");
}
abstract public String toStringIndented(String indent);
}
abstract class Value : Expression
{ // Value = Integer intValue | Boolean boolValue | Float floatValue | Character charValue
Type type;
int intValue;
float floatValue;
bool boolValue;
char charValue;
public String toStringIndented(String indent)
{
return indent + "Value("
+ (intValue || boolValue || floatValue || charValue)
+ ")";
}
}
It's as the error message says: you are trying to perform a boolean comparison using the || operator between two incompatible types: bool and int. You can only use the || operator against two bool values.
What is the intent of the logic in line 70?
What is the intent of the logic in line 70?
return string.Format("{0} Value({1} || {2} || {3} || {4})", indent, intValue, boolValue, floatValue, charValue);
//is this what you want?
//is this what you want?
ASKER
i wanted to check if the type is an intValue or boolValue or floatValue or charValue and pass it back, but don't really know a way to do it?
you could jusst have an object type member and then assign whatever is passed in to that and then
return yourObject.GetType().ToStr ing();
return yourObject.GetType().ToStr
ASKER
i'm not sure what's an object type member? can u give an example?
do you meant smt like this:
Value(int i)
{
type = new Type(Type.Integer);
intValue = i;
}
do you meant smt like this:
Value(int i)
{
type = new Type(Type.Integer);
intValue = i;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you don't want to change your code like suggested in the previous comments, you could fix the error by changing your toStringIndented() function:
public String toStringIndented(String indent) {
string s = indent + "Value(";
if (type == typeof(int))
s += intValue.ToString();
else if (type == typeof(bool))
s += boolValue.ToString();
else if (type == typeof(floatValue))
s += floatValue.ToString();
else
s += charValue;
return s + ")";
}
ASKER
so the object.GetType will determine whether value being pass is either int or bool or... right?
gettype returns the type of the object being passed, corrrect -- to string makes it text
ASKER
i understand the 2nd one but it gave me an error: Operator '==' cannot be applied to operands of type 'CliteLexer.Type' and 'System.Type'
i dont' know in what context you are referring.
what lis CliteLexer? what is the type property on it
this is an example of a way to use gettype
if(yourObject.GetType() == typeof(Int32))
//then do something
what lis CliteLexer? what is the type property on it
this is an example of a way to use gettype
if(yourObject.GetType() == typeof(Int32))
//then do something
ASKER
hmm no the object type (in your 1st solution) works fine, the error actually appear when i try the 2nd solution that u gave me (the if else in toStringIndented)
can you give me the code you have now, at the line that has the error?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i edited some of your code and i got the new error:
abstract class Value : Expression
{
Type type;
int intValue;
float floatValue;
bool boolValue;
char charValue;
public String toStringIndented(String indent)
{
string s = indent + "Value(";
if (type == typeof(intValue)) <-- intValue is a field but used as a type
s += intValue.ToString();
else if (type == typeof(boolValue))
s += boolValue.ToString();
else if (type == typeof(floatValue))
s += floatValue.ToString();
else
s += charValue;
return s + ")";
}
}
gotcha .. ok should be
if (type == typeof(Int32))
s += intValue.ToString();
if (type == typeof(Int32))
s += intValue.ToString();
ASKER
that works great!
Thanks.
Thanks.
as a side not i would recommend using string.format to build your string as Strings are immutable.
Glad to have helped
return indent + "Value("
+ (intValue || boolValue || floatValue || charValue)
+ ")";