• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

"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.
0
amillyard
Asked:
amillyard
  • 7
  • 4
  • 3
  • +1
7 Solutions
 
MikkkCommented:
try this:

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

maybe a misstyped char?
0
 
MikkkCommented:
Cay you write us the definition of ViewState object? is an Array?
0
 
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LimbeckCommented:
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
 
MikkkCommented:
Try with
return bool.Parse(ViewState["_isChecked"].value);
0
 
LimbeckCommented:
                 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
 
MikkkCommented:
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_davisCommented:
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now