4

in numerous other Types I have created it is possible to downCast a type and i usually Create An Extension method too so it will be easier to manage...

BaseTypeM
BTDerV : BaseTypeM
BTDerLastDescndnt : BTDerV

now i create A LastDerived Type and assign its value To ParentType

BTDerV BTDer;

BTDerLastDescndnt BTDerLastDesc = new BTDerLastDescndnt(parA, ParB);


this.BTDer = BTDerLastDesc;

then using the downCast Extension

var LDesc = this.BTDer.AsBTDerLastDescndnt();

which is actually

public static BTDerLastDescndnt AsBTDerLastDescndnt(this BTDerV SelfBTDerV )
{
    return (BTDerLastDescndnt)SelfBTDerV;
}

now when i do this as the code below, here it does compile but gives me a run-time error

        //BTDerV---v                 v---BaseTypeM
 public class SqlParDefV : SqlParameterM
 {
     public override SqlSpParDefMeta ParDMT
     {
         get { 
             return base.ParDMT;
         }
         set {
             base.ParDMT = value;
         }
     }
    public SqlParDefV(int bsprpOrdinal, string bsprpParName, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
    {
        this.ParDMT = new SqlSpParDefMeta(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsReq, bsprpIsDbStuctured, bsprpDirection);
    }

}


       //BTDerLastDescndnt---v
public sealed class SqlParTvDrecDefinitionVScl : SqlParDefV
{
    public override SqlSpParDefMeta ParDMT
    {
        get {
            return base.ParDMT;
        }
        set {
            base.ParDMT = value;
        }
    }
    public SprocTvTargetSF.currentSDTObjType SqlObjType { get; set; }
    public SqlMetaData[] Meta { get; set; }

    public SqlParTvDrecDefinitionVScl(int bsprpOrdinal, string bsprpParName, SprocTvTargetSF.currentSDTObjType ctrSqlObjType, SqlMetaData[] parGeneratedSqlMetaData, MSSTypesS bdprpTypeS, bool bsprpIsDbStuctured, bool bsprpIsReq = true, ParameterDirection bsprpDirection = ParameterDirection.Input)
                        : base(bsprpOrdinal, bsprpParName, bdprpTypeS, bsprpIsDbStuctured, bsprpIsReq, bsprpDirection)
    {
        this.SqlObjType = ctrSqlObjType;
        this.Meta = parGeneratedSqlMetaData;
    }
}

is there something unusual here or am i confused and missed some basic rule ?

Robb_2015
  • 377
  • 1
  • 3
  • 9
  • Not compile is a __compile error__ not a __runtime error__ which occurs during the runtime of a program – D. Ben Knoble Dec 31 '15 at 13:14
  • @BenKnoble i don't understand , it does compile but on runtime, in `AsSqlParTvDrecDefinitionVScl()` it runs till body of the method then it gives **runtime error** - _Unable to cast object of type_ which is runtime not compile..(**?**) – Robb_2015 Dec 31 '15 at 13:21
  • @BenKnoble or to be more accurate `System.InvalidCastException` – Robb_2015 Dec 31 '15 at 13:30
  • I misread i think (or the edit fixed it) it looked like you said it didnt compile but instead threw a runtime... :P oops – D. Ben Knoble Dec 31 '15 at 13:51
  • @BenKnoble I've removed unsuitable `virtual` kw from props, when added `sealed` kw to last descendant type, maybe that was problem of compile time virtual not suitable to sealed.. – Robb_2015 Dec 31 '15 at 14:00
  • 1
    Have you tried using the `as` operator and checking for null? – D. Ben Knoble Dec 31 '15 at 14:02
  • @BenKnoble as with your last comment on `as` **you have solved it** (and btw value to cast from was not null i usually make sure ahead, and not using null checks) please post the correct answer elaborating on the reason as it is really important to now that _small_ issue – Robb_2015 Dec 31 '15 at 14:09
  • I posted a quick workaround with as; i cant elaborate on __why__ its happening as i dont know – D. Ben Knoble Dec 31 '15 at 14:44

1 Answers1

2

I am unsure of the precise reasons a cast from Derived to MoreDerived fails here. However, a potential workaround (note: possibly code smell) is the as operator:

public static MoreDerived AsMoreDerived (this Derived d)
{
    return d as MoreDerived;
}

Note that as effectively attempts the cast and returns null, so you'll need an appropriate check there.

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
  • sorry for the early cheering comment on solved, as it now does not throw an exception, while parent type-value is with data, subtype returns without i.e `null` – Robb_2015 Dec 31 '15 at 14:50
  • @Robb_2015 Are there fields in `MoreDerived` not present in `Derived`? Your extension method may need to copy fields, and then initialize any other fields instead of doing a simple cast. – D. Ben Knoble Dec 31 '15 at 14:58
  • i am terribly terribly sorry about my mistake!! i WAS indeed confused here , the reason of my problem in code is, when loading a new element into the generic collection i have created an `AddParDef()` which was adding a new () parentType instead of derived into the `ParDefSet`(Collection) so no wonder that it couldn't be casted / converted into subType, so now the usual `(SubType)Parent` DownCast / Conversion does apply . thanks alot for your effort, it did bring me to the answer and really helped ! – Robb_2015 Dec 31 '15 at 15:14
  • Your very welcome @Robb_2015! Sometimes you just need a "rubber duck" to talk to :P – D. Ben Knoble Dec 31 '15 at 15:17
  • As a side note, after tonight @Robb_2015 should be Robb_2016 – D. Ben Knoble Dec 31 '15 at 15:21
  • yes.. you're funny(:, and i have to make a new `RobCs` Helpers namespace file **RobCs61a.cs**, 6-> 2016 ,1-> January, a-> week 1 – Robb_2015 Dec 31 '15 at 15:29