Type declaration ?
-
Heres what I do now
Type a = b.GetType();
if (a.Name == "Blog")
{
Console.WriteLine("blog = {0}", ((Blog)b).text);
error = "ok";
return 0;
}
if (a.Name == "String")
{
Console.WriteLine("blog = {0}", (String)b);
error = "ok";
return 0;
}
Console.WriteLine("blog = TypeError");
error = "unKnownType";
return 1;I want to set the type similar to this ((GetType())b).text How can I do this ?
-
Heres what I do now
Type a = b.GetType();
if (a.Name == "Blog")
{
Console.WriteLine("blog = {0}", ((Blog)b).text);
error = "ok";
return 0;
}
if (a.Name == "String")
{
Console.WriteLine("blog = {0}", (String)b);
error = "ok";
return 0;
}
Console.WriteLine("blog = TypeError");
error = "unKnownType";
return 1;I want to set the type similar to this ((GetType())b).text How can I do this ?
If b implements the interface IBlog, then you can cast b to an IBlog and provided Text is part of the interface declaration and is implemented in b's class, you can get the text. Something like:
interface IBlog
{
public string text { get; }
}public class Blog : IBlog
{
public string text
{
get { return "name"; }
}
}public int testThisExample()
{
object b = new Blog();if(b is IBlog) Console.WriteLine("blog = {0}", (b as IBlog).text); return 0;
}
Between the idea And the reality Between the motion And the act Falls the Shadow
-
Heres what I do now
Type a = b.GetType();
if (a.Name == "Blog")
{
Console.WriteLine("blog = {0}", ((Blog)b).text);
error = "ok";
return 0;
}
if (a.Name == "String")
{
Console.WriteLine("blog = {0}", (String)b);
error = "ok";
return 0;
}
Console.WriteLine("blog = TypeError");
error = "unKnownType";
return 1;I want to set the type similar to this ((GetType())b).text How can I do this ?
You could override the ToString() function in the
Blog
class.class Program
{
static void Main( string[] args )
{
object obj = "Test from string";
Console.WriteLine("The result of to string is:" + obj.ToString());
obj = new Test();
Console.WriteLine("The result of to string is:" + obj.ToString());
Console.ReadKey();
}
}public class Test
{
public override string ToString()
{
return "Test from class";
}
}Using the wrong tool for the job is half the fun.
-
Heres what I do now
Type a = b.GetType();
if (a.Name == "Blog")
{
Console.WriteLine("blog = {0}", ((Blog)b).text);
error = "ok";
return 0;
}
if (a.Name == "String")
{
Console.WriteLine("blog = {0}", (String)b);
error = "ok";
return 0;
}
Console.WriteLine("blog = TypeError");
error = "unKnownType";
return 1;I want to set the type similar to this ((GetType())b).text How can I do this ?
Hi, 1. you could concentrate on the ToString() method which exists for all objects; you should override for most types since for most classes ToString() by default returns a type string. 2. you should NOT check types by getting the type's name, then string.compare. C# has the
is
keyword which returns true when the type matches (directly or through inheritance). :)Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.