locked
Can't set custom property for composite control RRS feed

  • Question

  • User1376887987 posted

    I'm using Visual Studio 2008 and C# for all objects mentioned below.
    A web control (MyWebControl) contains some custom objects that are used by other cuscom components.
    MyWebControl is included as a web reference in a composite control (MyControl) and in a web project (MyProject).
    MyControl exposes some MyWebControl objects as public properties.
    MyProject instantiates and loads what should be identical type MyWebControl objects to be passed to MyControl.
    I have confirmed that the MyWebControl objects contained in MyProject contain the expected contents; they do exist and are not null.
    However, when I try to write a line of code to set a MyControl object to an equivalent MyProject object, the following error is displayed:
      Cannot implicitly convert type 'MyProject.MyWebControl.someObject' to 'MyCompany.MyControl.MyWebControl.someObject'
    I've tried an explicit cast but that didn't work either.
    This error is displayed whenever MyProject tries to set any of the custom MyWebControl objects exposed by MyControl.
    Other objects exposed by MyControl, such as strings, can be set with no problem.


    MyControl statements:
    using MWC = MyCompany.MyControl.MyWebControl;
    namespace MyCompany.MyControl
    {
     public class AControl : CompositeControl
     {
      private MWC.someObject _myObject;
      
      [Bindable(true)]
      [Category("Behavior")]
      [DefaultValue(null)]
      [Localizable(true)]
      public MWC.someObject myObject
      {
       get
       {
        return ((_myObject == null) ? new MWC.myObject() : _myObject);
       }
      
       set
       {
        _myObject = value;
       }
      }
      
      // lots of additional code believed to be irrelevant to this issue
     }
    }
    MyControl compiles with no errors or warnings and I am able to add it to my Visual Studio toolbox.

    In MyProject, I drag a copy of MyControl from the toolbox onto a web form and set its ID="MyControl1".
    MyProject web page codebehind statements:
    using MWC = MyProject.MyWebControl;
    namespace MyProject
    {
     public partial class AWebPage : System.Web.UI.Page
     {
      protected void someMethod()
      {
       MWC.someObject myObject;
       
       // some code setting myObject to an appropriate non-null value.
       
       // The error is displayed for the following line:
       MyControl1.MyObject = myObject;
      
       // lots of additional code believed to be irrelevant to this issue
      }
     }
    }

    Any help would be greatly appreciated.

     

    Monday, July 13, 2009 3:00 PM

Answers

All replies

  • User-2106054853 posted

    Hi,

    From the description there're two types:

    • MyProject.MyWebControl.someObject
    • MyCompany.MyControl.MyWebControl.someObject

    From the name of the above types most likely the namespace is wrong so actually they are different types, in which case you cannot assign instance of one type to that of another. Please double check the namespace. If it still doesn't work please upload a demo project to http://skydrive.live.com/ and paste the download link here for me to test.

    Tuesday, July 14, 2009 9:52 PM
  • User1376887987 posted

    MyControl and MyProject are separate projects in separate solutions and each has its own namespace.
    Doesn't the using directive require the project namespace to identify the reference?  It certainly doesn't seem to work without including the namespace.

    Are you telling me that MyControl and MyProject must use the identical namespace?
    That just doesn't seem right.

    Since both projects use MyWebControl object types, isn't the critical issue that both use compatible MyWebControl versions?
    Both projects were built with their web references pointing to the identical MyWebControl assembly so that should not be a problem.
    I'm certain about that because I recreated MyControl and MyProject from scratch.

    Unfortunately, my original problem description is as close as I can come to providing a demo.
    The troublesome MyWebControl objects are complex entities related to a third party package.
    A demo would be useless without that third party package.

    Wednesday, July 15, 2009 7:04 PM
  • User-2106054853 posted

     Hi,

    Two types in two namespaces are recognized as two different types even their class name is the same one. I could provide you a demo that can reproduce this issue. Please create a new Console project and try following code:

     

    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyClass mc; //It's ConsoleApplication5.MyClass
                mc =(MyClass)new AnotherNameSpace.MyClass();//Trying to convert to ConsoleApplication5.MyClass failed.
            }
        }
        public class MyClass { }
    }

    namespace AnotherNameSpace {
        public class MyClass { }
    }

     

    The code in your project has the similar problem. Please check the type name.

    Not quite sure what your scenario is, but I think a possible solution is to delete one type definition from one namespace and use the remained one, if the two types are same.

    It's a pure C# issue. If you still cannot solve this issue I suggest you ask the question in the following forum:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads

     

    Thursday, July 16, 2009 2:03 AM
  • User1376887987 posted

    That explains the problem but doesn't solve it.

    It looks like I have to figure out some way to make all 3 projects refer to compatible objects...

    Thursday, July 16, 2009 7:25 PM
  • User-2106054853 posted

     Yes you can create a class library and refer to it in all your projects for reuse.

    For more details please refer to:

    http://www.homeandlearn.co.uk/csharp/csharp_s16p1.html

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 17, 2009 1:19 AM