-2

I have super class and subclass.

public class Fee
{
    public decimal Dollar { get; set; }
    public decimal Percentage { get; set; }
}

public class AdminFee : Fee
{

}

And I also have another class

public class TypeFee
{ 
    public AdminFee AdminFees { get; set;}
}

And I want to assign datarow value to that adminFees. When I assign value, I got cast error or adminfees field null value at both run time.

Fee addfee = new Fee();
addfee.Dollar = Convert.ToDecimal(row["AdminFee"]);  ////no error

TypeFee tfee = new TypeFee();
tfee.AdminFees = (AdminFee)addfee;  ////cast error
tfee.AdminFees.Dollar = addfee.Dollar; ////null error

How do I resolve this error?

Parami
  • 3
  • 2
  • 3
    Please provide a [mcve], properly formatted - currently your code is very hard to read and has trivial mistakes (`Public` instead of `public`, `()` at the end of the class declaration) which show that this *isn't* the code you're actually testing. I'd also *strongly* advise you to start following .NET naming conventions. – Jon Skeet Jul 12 '16 at 09:24
  • I am not copying code, just type in from mobile. Will edit later. – Parami Jul 12 '16 at 09:29
  • 1
    In future, please don't do that. Why inflict a badly-written question on everyone, rather than waiting until you're in a position to write a *good* one to start with? – Jon Skeet Jul 12 '16 at 09:32
  • Possible duplicate of [Unable to Cast from Parent Class to Child Class](http://stackoverflow.com/questions/988658/unable-to-cast-from-parent-class-to-child-class) – OldBoyCoder Jul 12 '16 at 09:32

1 Answers1

0

You had plenty of typos and coding errors.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TypeFee tfee = new TypeFee();
            AdminFee addfee = new AdminFee();
            tfee.adminFees = (AdminFee)addfee;
            tfee.adminFees.dollar = addfee.dollar;
        }
    }
    public class Fee
    { 
        public decimal dollar {get;set;} 
        public decimal percentage {get;set;} 
    } 
    public class AdminFee :Fee
    { }
    public class TypeFee
    { 
        public AdminFee adminFees {get;set;}
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • It's not worth answering a question which is so badly formed, IMO. We can't tell which of the problems were in the OP's *real* code, and which were just because they decided to write a question on a mobile, without being able to copy/paste. – Jon Skeet Jul 12 '16 at 10:05
  • Disagree. It is better to remove all the stupid typos errors first. There may not be any other issues. – jdweng Jul 12 '16 at 10:21
  • That's the point - that's what the *OP* should have done before posting the question. It's just a bad question. We don't know which of those typos are in the real code - pandering to a bad question has no long-term benefit to the site, and encourages laziness IMO. – Jon Skeet Jul 12 '16 at 10:26
  • The one error the OP mentions is fixed by your code (the casting error which I've flagged could be a duplicate). I'd agree with Jon though it's a bad question – OldBoyCoder Jul 12 '16 at 10:27
  • I don't know how to fix this error, that's why I am asking in here. If I know the answer, no time to come here. I have already commented that I will edit my question. There's no point in pessimistic comments. I appreciate your answer jdweng. – Parami Jul 12 '16 at 21:37
  • The reason for the pessimistic comments is that if you've just typed in some code into a mobile we don't know if errors are caused by typing mistakes into your mobile rather than being in the original code. The cause of your error is probably that 'Fee adFee = new Fee()' should be 'Fee adFee = new AdminFee()'. But I don't know if that line is genuinely what is in your source or just what you remember is in your source. Do remember that everyone here gives their time for free (I work on here in my lunch breaks) and tries to be helpful, but do try your best to post code that is the actual code – OldBoyCoder Jul 13 '16 at 06:13