C# help needed

J

Jon Paal

how do I set the cast in C# ?

Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing
a cast?)

Source Error:



Line 6: string t = System.Web.HttpContext.Current.Profile.GetPropertyValue("SiteTheme");
 
J

Jeff User

Not sure what the particular object is that you are referencing (I am
rather new myself), but try this:

string t =
(string)System.Web.HttpContext.Current.Profile.GetPropertyValue("SiteTheme");

Jeff
 
S

Steve C. Orr [MVP, MCSD]

How about this?
string t =
System.Web.HttpContext.Current.Profile.GetPropertyValue("SiteTheme").ToString();
 
L

Lloyd Dupont

Let me explain the compiler error:
Compiler Error Message: CS0266: Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing

indeed 'System.Web.HttpContext.Current.Profile.GetPropertyValue("SiteTheme")'
return an object.

How do you know it's a string? Are you sure it's not a bug?

if it's a bug: correct it ;-)

If you know for sure it's a string, let it be known to the compiler by making a cast, just like in C
OPEN_PARENTHESIS TYPE CLOSE_PARENTHESIS

as in:
(string)

object o = "a string";
string s = (string) o;
 
K

Kevin Spencer

You convert instead of casting. Try Convert.ToString()

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top