1

I have these four classes in DatabaseLayer:

namespace DatabaseLayer
{
    public class ReactionDoa
    {
        public int Id;
        public MessageDoa Message;
        public VisitorDoa User;
        public string Text;
        public DateTime Date;
    }

    public abstract class MessageDoa
    {
        public int Id;
        public string Title;
        public VisitorDoa User;
        public CategoryDoa Category;
        public DateTime Date;
    }

    public class MediaDoa : MessageDoa
    {
        public string Location;
    }

    public class TextDoa : MessageDoa
    {
        public string Message;
    }
}

And three class in MediaSharing:

namespace MediaSharing
{
    public class Reaction
    {
        public Message Message { get; }
        public Visitor User { get; }
        public string Text { get; }
        public DateTime Date { get; }

        public Reaction(ReactionDoa r)
        {
            if (r.Message is Text)
            {
                this.Message = new Text(r.Message);
            }
                else if (r.Message is Media)
            {
                this.Message = new Media(r.Message);
            }

            User = new Visitor(r.User);
            Text = r.Text;
            Date = r.Date;
        }
    }

    public class Text : Message
    {
        public string Message { get; }

        public Text(string title, string message, Visitor user, Category category)
            : base(title, user, category)
        {
            Message = message;

            VisitorDoa u = new VisitorDoa()
            {
                Id = user.Id
            };

            CategoryDoa c = new CategoryDoa()
            {
                Id = category.Id
            };

            Database.Create.Text(u, title, c, message);
        }

        public Text(TextDoa m)
            : base(m)
        {
            Message = m.Message;
        }
    }

    public class Media : Message
    {
        public string Location { get; }

        public Media(string title, string location, Visitor user, Category category)
            : base(title, user, category)
        {
            Location = location;

            VisitorDoa u = new VisitorDoa()
            {
                Id = User.Id
            };

            CategoryDoa c = new CategoryDoa()
            {
                Id = category.Id
            };

            Database.Create.Media(u, title, c, location);
        }

        public Media(MediaDoa m)
            : base(m)
        {
            Location = m.Location;
        }
    }
}

When i try to do

this.Message = new Text(r.Message);

and

this.Message = new Media(r.Message);

It returns Argument 1: cannot convert from 'DatabaseLayer.MessageDoa' to 'DatabaseLayer.MediaDoa' Domain ......

And

Argument 1: cannot convert from 'DatabaseLayer.MessageDoa' to 'DatabaseLayer.TextDoa' Domain ......

But MediaDoa and TextDoa uses MessageDoa...

yooouuri
  • 2,578
  • 10
  • 32
  • 54
  • 2
    The problem is you don't know if the `MessageDoa` is a `MediaDoa` or a `TextDoa` or possibly neither. Now if you tried to pass a `MediaDoa` or a `TextDoa` to a method taking a `MessageDoa` that would work just fine. – juharr Oct 27 '15 at 14:46
  • @juharr do you have an example, because the *Doa classes are only used for storing data. – yooouuri Oct 27 '15 at 14:52
  • 1
    There's a few things wrong with this code. `r.Message` will never be of type `Text` or `Media` because `ReactionDoa.Message` is of type `MessageDoa` which neither `Text` nor `Media` derive from. Secondly, as @juharr pointed out, the compiler can't prove that `r.Message` is the type the `Text` or `Media` constructors want, so you'd have to cast it and take the run-time exception if it fails. My general rule is if I end up writing code to check types then I'm probably trying to express something in the type system that I shouldn't be. – Ed T Oct 27 '15 at 14:58

0 Answers0