checking an object is compatible with another object
-
i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike
-
i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike
Please try with the code given below.
bool CheckConfigVariable(object variable, Type expectedType)
{
if (variable.GetType() == expectedType)
{
return true;
}
return false;
}Software - Bundle of bugs covered with features.
-
i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike
typeof takes the type, I thought, as in typeof(int). I think you need hello.GetType()
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
typeof takes the type, I thought, as in typeof(int). I think you need hello.GetType()
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
This isn't exactly doing what i thought it was doing sorry. variable.GetType() == expectedType is checking whether Object is equal to Type blah. Its always returning false. I just want it to return if it is compatible with casting it to another object, not if it is the same type.
-
This isn't exactly doing what i thought it was doing sorry. variable.GetType() == expectedType is checking whether Object is equal to Type blah. Its always returning false. I just want it to return if it is compatible with casting it to another object, not if it is the same type.
OK, perhaps generics will help. Because what you need is access to the type to call 'is' as you were trying to do. Passing a Type doesn't sound like it worked, but if the type is specified and your generic method says something like return (myvar is T);
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
OK, perhaps generics will help. Because what you need is access to the type to call 'is' as you were trying to do. Passing a Type doesn't sound like it worked, but if the type is specified and your generic method says something like return (myvar is T);
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Christian Graus wrote:
return (myvar is T);
where T is my parameter of type? bool CheckConfigVariable(object variable, Type expectedType) { return (variable is expectedType); } i get the error: The type or namespace name 'expectedType' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
-
Christian Graus wrote:
return (myvar is T);
where T is my parameter of type? bool CheckConfigVariable(object variable, Type expectedType) { return (variable is expectedType); } i get the error: The type or namespace name 'expectedType' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
Yeah, but you write a generic method instead. static bool CheckType<T>(object o) { return (o is T); } Then you call it like this: DateTime dt = DateTime.Now; bool b = CheckType<int>(dt);
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Yeah, but you write a generic method instead. static bool CheckType<T>(object o) { return (o is T); } Then you call it like this: DateTime dt = DateTime.Now; bool b = CheckType<int>(dt);
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
wow, I didn't know how that worked. Now I'll have to go and do some more indepth reading on Generics. Just another question on comparing types. How do enums fit into the picture? Is enum a type? Can I compare a string to an enum (using the method above (using is keyword)), or one of the values in the enumto see if it can be converted?
-
wow, I didn't know how that worked. Now I'll have to go and do some more indepth reading on Generics. Just another question on comparing types. How do enums fit into the picture? Is enum a type? Can I compare a string to an enum (using the method above (using is keyword)), or one of the values in the enumto see if it can be converted?
You use Enum.Parse to convert a string to an enum. An enum is a type.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
You use Enum.Parse to convert a string to an enum. An enum is a type.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Thank you so much for your help, it is greatly appreciated. :)
-
Thank you so much for your help, it is greatly appreciated. :)
Glad to help
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike
-
i am trying to write a method that checks whether an object can be cast to a specific type: that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method? Mike
private static bool
F
(
object o
,
System.Type t
)
{
if ( o == null )
{
throw ( new System.ArgumentNullException() ) ;
}return ( t.IsAssignableFrom ( o.GetType() ) ) ;
}
And may I humbly suggest my TypeTransmogrifier[^] ?