tags:

views:

2829

answers:

6

Hi, This is probably trivial, but I can't think of a better way to do it. I have a COM object that returns a variant which becomes an object in C#. The only way I can get this into an int is

int test = int.Parse(string.Format("{0}", myobject))

Is there a cleaner way to do this? Thanks

+1  A: 

Maybe Convert.ToInt32.

Watch out for exception, in both cases.

J.W.
+3  A: 

Use Int32.TryParse as follows.

  int test;
  bool result = Int32.TryParse(value, out test);
  if (result)
  {
     Console.WriteLine("Sucess");         
  }
  else
  {
     if (value == null) value = ""; 
     Console.WriteLine("Failure");
  }
CodeToGlory
Actually, Parse calls TryParse and throws an exception if TryParse returns false. So TryParse doesn't handle the exception because it never throws one.
Samuel
Well technically they both call the same method NumberToInt32, but only Parse throws exceptions when it doesn't work.
Samuel
TryParse still requires converting the variant to a string. AFAIK the issue isn't converting from a string to an int, but from a variant that is an int to an actual int.
Darren Clark
A: 

Convert.ToInt(myobject);

this will handle the case where myobject is null and return 0, instead of throwing an exception.

Zahir
A: 

There's also TryParse.

From MSDN:

private static void TryToParse(string value)
   {
      int number;
      bool result = Int32.TryParse(value, out number);
      if (result)
      {
         Console.WriteLine("Converted '{0}' to {1}.", value, number);         
      }
      else
      {
         if (value == null) value = ""; 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value);
      }
   }
Lance Harper
+15  A: 
Joel Coehoorn
Downvoter: any particular reason?
Joel Coehoorn
Probably some other answerer is pissy about the 32k user having a better answer.
Samuel
yep...some people just get jealous when a higher rep person comes in and steals the attention with a better answer...lol
TStamper
I did find a point that wasn't really an error but perhaps simplified things too much so someone might think that. I've removed that sentence and added a link to authoritative documentation.
Joel Coehoorn
Will the direct cast work with an implicit conversion? I was under the impression that that will strictly do unboxing only, not any other conversions.
Darren Clark
Not exactly a response, but read this: http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx
Joel Coehoorn
The upshot is that it definitely does more than just unbox. Otherwise why could you use it to cast a double to an int, for example?
Joel Coehoorn
And reading some other things I may have my implicit/explicit backwards up there- but either way I think it gets the point across.
Joel Coehoorn
I meant specifically the cast from ref to value type, not in general. That's why the (int) (short) works, one unbox, one explicit cast. Actually I think int val = (short) myObject should work since a cast from short to int is implicit.
Darren Clark
instead of "as int" (which won't compile) you could try "as int?"
Jimmy
good idea - implemented.
Joel Coehoorn
+4  A: 

The cast (int) myobject should just work.

If that gives you an invalid cast exception then it is probably because the variant type isn't VT_I4. My bet is that a variant with VT_I4 is converted into a boxed int, VT_I2 into a boxed short, etc.

When doing a cast on a boxed value type it is only valid to cast it to the type boxed. Foe example, if the returned variant is actually a VT_I2 then (int) (short) myObject should work.

Easiest way to find out is to inspect the returned object and take a look at its type in the debugger. Also make sure that in the interop assembly you have the return value marked with MarshalAs(UnmanagedType.Struct)

Darren Clark