none
Returning List<T> from method Invalid cast - Need Help! RRS feed

  • Question

  • Need help to resolve this issue!

    Any help is appreciated!

    Paul

    ........

    So here is first part of the issue highlighted....

                         

    namespace InspectStationLibrary
    {
        public class AnalysisPlanner
        {


            public static string connString = "Integrated Security=true;Initial Catalog=InspectionStation;server=oscar\\SQLEXPRESS";

            private Int16 analysisPlanID = 0;
            private String aPlanName = "";
            private String aPlanDesc = "";



            public string ConnString { get => connString; set => connString = value; }
            public short AnalysisPlanID { get => analysisPlanID; set => analysisPlanID = value; }
            public string APlanName { get => aPlanName; set => aPlanName = value; }
            public string APlanDesc { get => aPlanDesc; set => aPlanDesc = value; }

            public static void ASetup() { }
            public static void CreateARec() { }

             public AnalysisPlanner(short vplanId, string vplanName, string vplandesc)
                {
                    analysisPlanID = vplanId;
                    aPlanName = vplanName;
                    aPlanDesc = vplandesc;
                }
           


    public static List<AnalysisPlanner> GetAPlanList() {
                var PlanList = new List<AnalysisPlanner>();

                string queryString =
                    "SELECT AnalysisPlanID, APlanName,APlanDesc,Version FROM dbo.AnalysisPlan ";

                DataSet ds = new DataSet();
                AnalysisPlanDataTable(ds);

                using (SqlConnection connection = new SqlConnection(connString))
                {

                    SqlCommand command = new SqlCommand(queryString, connection);

                    try
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader != null && reader.Read())
                            {

                                AnalysisPlanner chkList = new AnalysisPlanner(reader.GetInt16(1),reader.GetString(2), reader.GetString(3));
                                PlanList.Add(chkList);

                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                }
                    return PlanList;

            }

    Here is the unit test method the highlighted line  is reporting an invalid cast

            [TestMethod]
            public void GetAnalysisPlanIDs()
            {
                string rez = " ";
                List<AnalysisPlanner> apLS = AnalysisPlanner.GetAPlanList();

                foreach (var itemS in apLS)
                {
                    rez = itemS.AnalysisPlanID.ToString();
                    Console.WriteLine(rez.ToString());
                }


            }

    Tuesday, November 21, 2017 6:50 PM

Answers

  •  

    List<AnalysisPlanner> apLS = AnalysisPlanner.GetAPlanList().Tolist();

    You can lookup what ToList()  is doing, because you are retuning a collection out of a method, then you can apply Linq to the method.  

    Actually in this case above, you are casting the retuned results to a List<T>.

     List<AnalysisPlanner> apLS = AnalysisPlanner.GetAPlanList().Where(x => x.name.Contains("a").Tolist();

    http://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html

    Also this.....

    http://www.artima.com/weblogs/viewpost.jsp?thread=126923

    Hitting a DB is not unit testing, which is explained in the above link.

    A test is not a unit test if:

    • It talks to the database
    • It communicates across the network
    • It touches the file system
    • It can't run at the same time as any of your other unit tests
    • You have to do special things to your environment (such as editing  config  files) to run it.

    What should be happening  with GetAPlanList() is that you make objects manually, loading the List<T> with the objects and return the List<T>.

    http://defragdev.com/blog/?p=783

    • Marked as answer by PaulFlores Tuesday, November 21, 2017 11:30 PM
    Tuesday, November 21, 2017 11:08 PM

All replies

  • Maybe there are several declarations of AnalysisPlanner or List classes. Show the error messages and clarify the classes, files and references included into your projects.

    And try this:

       var apLS = AnalysisPlanner.GetAPlanList();

    Tuesday, November 21, 2017 8:13 PM
  • No change using var apLS

    Using a C# testing project. 

    The unit test outcome is "Specified cast is not valid" related to the highlighted test case line . 

    Here is the entire unit test.

    Paul

    -------------- unit test---------------

    using System.Collections.Generic;
    using InspectStationLibrary;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;

    namespace UnitTestProject1
    {
        [TestClass]
        public class UnitTest1
        {
            //create class objects

            [TestMethod]
            public void CreateInspectionPlanner()
            {
                InspectionPlanner testIP = new InspectionPlanner();
            }
            [TestMethod]
            public void CreateAnalysisPlanner()
            {

                AnalysisPlanner testAP = new AnalysisPlanner();
            
            }
            [TestMethod]
            public void CreateInspection()
            {

                Inspection testIS = new Inspection();
                
            }
        }
     [TestClass]
     public class UnitTest2
        {
            //Return Item List
            [TestMethod]
            public void GetInspectionPlanIDs()
            {
                List<InspectionPlanner> ipList = InspectionPlanner.GetIPlanList();
            }

            [TestMethod]
            public void GetAnalysisPlanIDs()
            {
                string rez = " ";
               var apLS = AnalysisPlanner.GetAPlanList();

                foreach (var itemS in apLS)
                {
                    rez = itemS.AnalysisPlanID.ToString();
                    Console.WriteLine(rez.ToString());
                }


            }

            [TestMethod]
            public void GetJobNumbers()
            {
                List<Inspection> jobList = Inspection.GetJobRecs();
            }
        }
    }

    Tuesday, November 21, 2017 9:07 PM
  • Hello,

     I am not familiar with CodeTest but looks interesting. However, here is my

    thoughts;   Methods in UnitTest both declare variables that go out of scope.

     Example

    public class UnitTest1
    {
        //create class objects
    
        // Default class Scope
        InspectionPlanner testIP;
        AnalysisPlanner testAP;
        Inspection testIS;
        //
        // Consider, private or internal
    
        [TestMethod]
        public void CreateInspectionPlanner()
        {
            this.testIP = new InspectionPlanner();
        }
        [TestMethod]
        public void CreateAnalysisPlanner()
        {
            this.testAP = new AnalysisPlanner();
        }
        [TestMethod]
        public void CreateInspection()
        {
            this.testIS = new Inspection();
        }

     The above code is show corrections. 

     

     Thanks :)


    • Edited by User3DX Tuesday, November 21, 2017 10:27 PM spelling
    Tuesday, November 21, 2017 10:25 PM
  • Thanks User3DX for the insight but it did not address the issue.
    • Edited by PaulFlores Tuesday, November 21, 2017 10:37 PM syntax
    Tuesday, November 21, 2017 10:36 PM
  • I can't see any invalid cast on that line. In fact, I can't see a cast at all. What exactly does the error say?
    Tuesday, November 21, 2017 10:47 PM
  • Hello,

     Sorry to hear that.  Another thought is Class Inheritance,  I do not

    see where UnitTest2 has access to UnitTest1 class methods.

     Thanks :)

    Tuesday, November 21, 2017 10:59 PM
  •  

    List<AnalysisPlanner> apLS = AnalysisPlanner.GetAPlanList().Tolist();

    You can lookup what ToList()  is doing, because you are retuning a collection out of a method, then you can apply Linq to the method.  

    Actually in this case above, you are casting the retuned results to a List<T>.

     List<AnalysisPlanner> apLS = AnalysisPlanner.GetAPlanList().Where(x => x.name.Contains("a").Tolist();

    http://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html

    Also this.....

    http://www.artima.com/weblogs/viewpost.jsp?thread=126923

    Hitting a DB is not unit testing, which is explained in the above link.

    A test is not a unit test if:

    • It talks to the database
    • It communicates across the network
    • It touches the file system
    • It can't run at the same time as any of your other unit tests
    • You have to do special things to your environment (such as editing  config  files) to run it.

    What should be happening  with GetAPlanList() is that you make objects manually, loading the List<T> with the objects and return the List<T>.

    http://defragdev.com/blog/?p=783

    • Marked as answer by PaulFlores Tuesday, November 21, 2017 11:30 PM
    Tuesday, November 21, 2017 11:08 PM
  • Test Name: GetAnalysisPlanIDs
    Test FullName: UnitTestProject1.UnitTest1.GetAnalysisPlanIDs
    Test Source: C:\Users\pflores\source\repos\UnitTestProject1\UnitTestProject1\UnitTest1.cs : line 48
    Test Outcome: Passed
    Test Duration: 0:00:00.0068628

    Result StandardOutput: Specified cast is not valid.
    Tuesday, November 21, 2017 11:09 PM
  • Put everything into the same class but that did not make any difference.
    Tuesday, November 21, 2017 11:10 PM