type object
-
hello everyone I want to know how to specify the type of a variable declared as Object better: I have a variable type object, and I want to test whether this object is string it will make me so and so instrution and if this object and int, and so on. thank you Here is a snippet incomplet:
public static string Format(string format, object val,int length)
{
LineDefinition LD;
string var;
if (val.GetType() )var = string.Format(LD.Format , val).PadRight (LD .Length ,' '); var = string.Format(LD.Format, val).PadLeft (LD.Length, '0'); return var;
-
hello everyone I want to know how to specify the type of a variable declared as Object better: I have a variable type object, and I want to test whether this object is string it will make me so and so instrution and if this object and int, and so on. thank you Here is a snippet incomplet:
public static string Format(string format, object val,int length)
{
LineDefinition LD;
string var;
if (val.GetType() )var = string.Format(LD.Format , val).PadRight (LD .Length ,' '); var = string.Format(LD.Format, val).PadLeft (LD.Length, '0'); return var;
So you have a type that is an object and you want to determine whether or not it can be cast to a particular type. Is that what you are asking? If so (and if it is possible that the value comes in looking like a string), you can use the TryParse method on a particular type to determine whether or not it is of that type. You need to be careful with this approach because you need to carefully order the sequence of tests; this means that if you have the following:
object o = 10;
then o could be a short, an int or a long. If the variable looks like this example, then you can use the is operator to determine the type, e.g.
if (o is int)
{
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
So you have a type that is an object and you want to determine whether or not it can be cast to a particular type. Is that what you are asking? If so (and if it is possible that the value comes in looking like a string), you can use the TryParse method on a particular type to determine whether or not it is of that type. You need to be careful with this approach because you need to carefully order the sequence of tests; this means that if you have the following:
object o = 10;
then o could be a short, an int or a long. If the variable looks like this example, then you can use the is operator to determine the type, e.g.
if (o is int)
{
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.