Converting

  • Thread starter Thread starter Default User
  • Start date Start date
D

Default User

hello

How do i convert a varible object to string?
I have a varible called "folder" and my app reads the registry for the
folder path so

object folder;
folder = "C:\Program Files\Spirte\"

when i trying something like File.Delete(folder); and compile or debug i get
the error

"Argument '1': cannot convert from 'object' to 'string'

thanks in advanced
 
can this be done to convert any varible so say i wanted to convert something
to an int or long?
 
Newbie said:
can this be done to convert any varible so say i
wanted to convert something to an int or long?

Yes, but it will fail (throw an exception) if the object isn't
convertible to that type.

Don't declare things as "object". Declare them as what they are.

P.
 
Be careful about the difference between casting and converting.

You can cast in your example, because the object is already a string.
You're just holding a reference to a string in an object reference
variable, so you can tell the compiler "treat this like a string" and
it works, because it really is a string. The compiler doesn't need to
call a method to change what kind of thing it is, because it is already
a string... the compiler just doesn't know it.

A _conversion_, on the other hand, implies that the thing is _not_ the
type you want it to be, so you have to go through a conversion method.
See the Convert class for these.
 
Back
Top