"cannot convert from 'object' to 'string'"

Development platform : c# asp.net 2.x, iis 6

I am getting a compiler error as follows:

"cannot convert from 'object' to 'string'"

for the line:  return bool.Parse(ViewState["_isChecked"];

your time and efforts with this enquiry are much apprieated.

many thanks.
amillyardAsked:
Who is Participating?
 
LimbeckConnect With a Mentor Commented:
from VS2005 help:

C#  Copy Code
private const int defaultFontSize = 3;

// Add property values to view state with set;
// retrieve them from view state with get.
public String Text
{
    get
    {
        object o = ViewState["text"];
        return (o == null)? String.Empty : (string)o;
    }

    set
    {
        ViewState["Text"] = value;
    }
}

 
0
 
MikkkConnect With a Mentor Commented:
try this:

return bool.Parse(ViewState["_isChecked"]);

maybe a misstyped char?
0
 
MikkkConnect With a Mentor Commented:
Cay you write us the definition of ViewState object? is an Array?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
amillyardAuthor Commented:
@Mikkk - as requested :

        public bool _isChecked
        {
            get
            {
                if (ViewState["_isChecked"] == null)
                {
                    return false;
                }
                else
                {
                    return bool.Parse(ViewState["_isChecked"]);
                }
            }
            set
            {
                ViewState["_isChecked"] = value;
            }
        }
0
 
MikkkConnect With a Mentor Commented:
Try with
return bool.Parse(ViewState["_isChecked"].value);
0
 
LimbeckConnect With a Mentor Commented:
                 return bool.Parse((string)ViewState["_isChecked"]);
0
 
amillyardAuthor Commented:
@Mikkk,

ok, thanks.  have updated scripting, am geting the following compiling error message as follows:

'object' does not contain a definition for .value (blue underline for .value)
0
 
amillyardAuthor Commented:
@Limbeck

thank you for your contribution also.

the :  return bool.Parse((string)ViewState["_isChecked"]);  is compiling ok -- am just checking function itself is doing what I am hoping it should be achieving...
0
 
MikkkConnect With a Mentor Commented:
Ok, then, remove the .value I suggest. It will not work.

What about this?
return (ViewState["_isChecked"]==true);
0
 
amillyardAuthor Commented:
@Mikkk - I had to add the " ", in order to compile -- am checking functionality to confirm the script is performing as expected.

return (ViewState["_isChecked"]=="true");
0
 
amillyardAuthor Commented:
In the calling procedure - I am using :

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["_isChecked"] = RadioButton_Role.Checked;
           
           if (ViewState["_isChecked"] )
           {
                RadioButton_Role.Checked = false;
                RadioButton_StaffMember.Checked = true;
           }
        }

i.e. to check the Radio Button checked status (historic user interation changes to button status) and reconfigure on a PreRender request.

the line :

if (ViewState["_isChecked"]) is not compiling -- a casting error.

what I am trying to check if the state is true or false ... what I am doing wrong here please?


__updated procedure______________________________

       public bool _isChecked
        {
            get
            {
                if (ViewState["_isChecked"] == null)
                {
                    return false;
                }
                else
                {
                    return (ViewState["_isChecked"] == "true");
                    //return bool.Parse((string)ViewState["_isChecked"]);
                }
            }
            set
            {
                ViewState["_isChecked"] = value;
            }
        }
0
 
p_davisConnect With a Mentor Commented:
how about wrapping the object in a convert instead of parsing

like:
Convert.ToBoolean(ViewState["_isChecked"])
0
 
amillyardAuthor Commented:
@p_davis : worked 1st time -- thank you
0
 
amillyardAuthor Commented:
@Mikkk, @Limbeck -- thank you both for your contributions :-)
0
 
p_davisCommented:
your welcome
0
 
LimbeckCommented:
glad to be of help :)

Ed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.