Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"If is class" Condition Failing

Hi I have 3 base interfaces:

public interface IKlarfDefect
{
    int TEST { get; set; }
    int DEFECTID { get; set; }

}

public interface IKlarfDefect <TImageListInfo> : IKlarfDefect
{

    List<TImageListInfo> KlarfImageList { get; set; }
}

public interface IHasKlarfImageList<TImageListInfo>
{
    List<TImageListInfo> KlarfImageList { get; }
}

These classes implement it:

public class CIMKlarfDefect : IKlarfDefect, IHasKlarfImageList<CIMKlarfImageListInfo>
{
    public int TEST { get; set; }
    public int DEFECTID { get; set; }

    public List<CIMKlarfImageListInfo> KlarfImageList { get; set; } = new List<CIMKlarfImageListInfo>();
    
}

public class RMTKlarfDefect : IKlarfDefect, IHasKlarfImageList<CIMKlarfImageListInfo>
{
    public int TEST { get; set; }
    public int DEFECTID { get; set; }
    
    public List<RMTKlarfImageListInfo> KlarfImageList { get; set; } = new List<RMTKlarfImageListInfo>();
}

Then I have this function in another class that tries to read through these classes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

internal static string CreateCIMDefectListString(IEnumerable<IKlarfDefect<IKlarfImageListInfo>> klarfDefectList)
{
    StringBuilder defectListString = new StringBuilder();
    defectListString.AppendLine("");
    foreach (var klarfDefect in klarfDefectList) { 
        defectListString.Append(klarfDefect.TEST).Append(" ");
        defectListString.Append(klarfDefect.DEFECTID).Append(" ");
        
        if (klarfDefect is IHasKlarfImageList<IKlarfImageListInfo> grcKlarfDefect) 
        {
            if (grcKlarfDefect.KlarfImageList.Count == 0)
            {
                defectListString.Append("N;");
                defectListString.AppendLine();
            }
            
        }
    }
    return defectListString.ToString();

}

It compiles but the if statement fails when I am passing in either RMTKlarfDefect or CIMKlarfDefect. Might anyone know why this is?

if klarfDefect is IHasKlarfImageList grcKlarfDefect
fails as in, it doesn’t return true which would allow it to process the imagelist

>Solution :

Your if statement says

if (klarfDefect is IHasKlarfImageList<IKlarfImageListInfo> grcKlarfDefect)

But your classes implement

IHasKlarfImageList<CIMKlarfImageListInfo>

So you need to update your if statement accordingly:

 if (klarfDefect is IHasKlarfImageList<CIMKlarfImageListInfo> grcKlarfDefect)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading