locked
error CS0266: Cannot implicitly convert type 'object' to 'string' RRS feed

  • Question

  • I added wpf usercontrol to winform project.
    However I failed compilation.

    I got errors. error CS0266: Cannot implicitly convert type 'object' to 'string'.

    string abc = test.PropertyAccessor.GetProperty(schema name);

    //type of test is Microsoft.Office.Interop.Outlook.Attachment

    //test.PropertyAccessor.GetProperty return dynamic.

    I found the cause.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:CustomControls"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        mc:Ignorable="d">

    xmlns:local="clr-namespace:CustomControls" is the cause.
    If xmlns:local="clr-namespace:CustomControls" is removed, I don't get error.

    Why is that?





    • Edited by 라바 Wednesday, May 20, 2020 2:39 AM
    Tuesday, May 19, 2020 7:30 AM

Answers

  • As Timon mentions, you are mixing types. If you look at the documentation for the GetProperty method you're calling it accepts a string and returns an Object. Therefore this is not valid.

    //Valid
    object value = test.PropertyAccessor.GetProperty("name");  //Returns object
    
    //Not valid - cannot convert object to string
    string abc = value;
    
    //Try a conversion - note may return null
    string valueAsString = value?.ToString();


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by 라바 Wednesday, May 20, 2020 2:21 AM
    Tuesday, May 19, 2020 1:59 PM

All replies

  • Hi,

    Thank you for posting here.

    CS0266 is a conversion error, try this way:

    string x = Convert.toString(value)

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Tuesday, May 19, 2020 1:59 PM
    Tuesday, May 19, 2020 8:13 AM
  • It seems to me you somehow managed to assign a wrong schema in your RessourceDictionary. While using the PropertyAccessor.GetProperty method for an Attachement object, the type of the object returned will be the same as the type of the property specified by (the correct) SchemaName (for details Getting the Value of a Property). And after removing the respective line in your namespace settings it recurred to the correct default schema.

    wizend

    Tuesday, May 19, 2020 9:59 AM
  • As Timon mentions, you are mixing types. If you look at the documentation for the GetProperty method you're calling it accepts a string and returns an Object. Therefore this is not valid.

    //Valid
    object value = test.PropertyAccessor.GetProperty("name");  //Returns object
    
    //Not valid - cannot convert object to string
    string abc = value;
    
    //Try a conversion - note may return null
    string valueAsString = value?.ToString();


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by 라바 Wednesday, May 20, 2020 2:21 AM
    Tuesday, May 19, 2020 1:59 PM
  • Thanks for your comments.

    That schema name is just sample.

    I passed right schema name.

    I'm sorry I made you misunderstand.

    Wednesday, May 20, 2020 2:38 AM