invalidcastexception derived treenode

hi all!

in my program i have a treeview formed by some classes derived from the treenode parent class...the scope of my program is to write an xml file by the structure of the treeview...

i can save my project( and so write the xml file) and when i save a new project i can do a good cast...for example:

org = (Organization) node; ->org is an instance of Organization(derived from treenode) node is an instance of TreeNode

i can also open a file and then save...but here i found casting problems...i dont know why here i cant cast...i do exactly the same things!!

please help me!!i dont know what to do!!

thanks all

bye



Answer this question

invalidcastexception derived treenode

  • Nazzo

    now i try...

    i have modified some lines

    if (node.Text == "organization"){

    Organization org;

    object objGeneral = node;

    string type = objGeneral.GetType().ToString();

    MessageBox.Show(type.ToString());

    if (objGeneral is Organization){

    org = (Organization)objGeneral;

    ecc...

    }}

    else{

    Object objGeneral2 = node;

    objGeneral2 = (Organization)xmlOrg;

    string type2= objGeneral2.GetType().ToString();

    MessageBox.Show(type2);

    if (objGeneral2 is Organization){

    xmlOrg = (Organization)objGeneral2;

    if (xmlOrg == null)

    ecc

    i see that when i save from a new treeview (here there arent exception) the type of the object objGeneral = node; is organization (so is a class type)

    when i save from an opened xml file the type of object objGeneral = node; is treenode

    here is the problem i want to convert the treenode in organization..

    what u think is possible

    tnx


  • crystak

    org = (organization) treeview2.selectednode;

    Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'Tesi.Organization'.

    the exception is thrown on the selectednode and not on org...

    what i have to do to solve this problem really i dont know how to solve this...


  • anderl

    hi,

    you lost me here , would you post part of your code which indicate where is the problem

    best regards



  • Jiteshk

    Write like this to avoid invalid cast exceptions

    if (treeview2.SelectedNode is Organization)
    {
    org = treeview2.SelectedNode as Organization;
    // ...
    }

    this will also give same result

    org = treeview2.SelectedNode as Organization;
    if (org != null)
    {
    // ...
    }



  • introne

    Did you try it where you able to resolve the problem

    Thank you,
    Bhanu.


  • BLanders

    Are you really sure that the node that gives InvalidCastException is Organization derived from TreeNode

    You should not rely on exceptions to control flow of your program. You can use the is keyword to find out if a object is of a certain type

    if (node is Organization)
    {
    // handle if it is of type Organization
    }
    else
    {
    // handle node as if it is TreeNode
    }

    Also the code below is redundandt

    catch (InvalidCastException)
    {
    TreeNode nodeI = new TreeNode
    ();
    nodeI = node;
    Object objGeneral = nodeI;

    node is enforced to be of type TreeNode because it is in the foreach() statement.



  • DickM

    that's the function to save xml files...thanks for helping me

    private void saveNode(TreeNodeCollection tnc) {

    foreach (TreeNode node in tnc){

    if (node.Text == "organizations"){

    try{

    xr.WriteStartElement(node.Text);

    xr.WriteStartAttribute("default_organization");

    xr.WriteValue(orgs.p_default_organization);

    xr.WriteEndAttribute();

    saveNode(node.Nodes);

    xr.WriteEndElement();}

    catch (ArgumentNullException){

    MessageBox.Show("Please compile every field!", "Organizations Form Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    // orgform = new OrganizationForm;

    orgsform = new OrganizationsForm(orgs);

    orgs.FormOrgs = orgsform;

    orgs.FormOrgs.Show();}}

    if (node.Text == "organization"){

    try{

    org = (Organization)node;

    try{

    xr.WriteStartElement(node.Text);

    xr.WriteStartAttribute("id_organization");

    xr.WriteValue(org.p_id_organization);

    xr.WriteEndAttribute();

    xr.WriteStartAttribute("channel_type");

    xr.WriteValue(org.p_channel_organization);

    xr.WriteEndAttribute();

    xr.WriteStartAttribute("struct_organization");

    xr.WriteValue("hierarchical");

    xr.WriteEndAttribute();

    xr.WriteElementString("title", org.p_title_organization);

    saveNode(node.Nodes);

    xr.WriteEndElement();}

    catch (ArgumentNullException){

    MessageBox.Show("Please compile every field!", "Organization Form Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    orgform = new OrganizationForm(org);

    org.FormOrg = orgform;

    org.FormOrg.Show();}}

    catch (InvalidCastException){

    TreeNode nodeI = new TreeNode();

    nodeI = node;

    Object objGeneral = nodeI;

    Organization orgI = objGeneral as Organization;

    orgI = (Organization)objGeneral;

    if (orgI != null){

    try{

    xr.WriteStartElement(nodeI.Text);

    xr.WriteStartAttribute("id_organization");

    xr.WriteValue(org.p_id_organization);

    xr.WriteEndAttribute();

    xr.WriteStartAttribute("channel_type");

    xr.WriteValue(org.p_channel_organization);

    xr.WriteEndAttribute();

    xr.WriteStartAttribute("struct_organization");

    xr.WriteValue("hierarchical");

    xr.WriteEndAttribute();

    xr.WriteElementString("title", org.p_title_organization);

    saveNode(node.Nodes);

    xr.WriteEndElement();}

    catch (ArgumentNullException){

    MessageBox.Show("Please compile every field!", "Organization Form Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    orgform = new OrganizationForm(org);

    org.FormOrg = orgform;

    org.FormOrg.Show();}}

    else{

    break;

    }

    }

    }


  • invalidcastexception derived treenode