Search code examples
c#ienumerableinstantiation

Why does my code say my object is not defined?


I'm new to C# and can't figure out why my class objects "do not exist" in the current contents. I've tried multiple ways to reorganize and call my objects, but still get "The name 'ExcuteObject' does not exist in the current context.

namespace DBTest4
{
    class Program
    {
        class MacroInfo
        {
            public int MsgSN { get; set; }
            public int FormID { get; set; }
            public int Leg { get; set; }
            public int Stop { get; set; }

            public MacroInfo(DataRow row)
            {
                this.MsgSN = Convert.ToInt32(row["MsgSN"]);
                this.FormID = Convert.ToInt32(row["FormID"]);
                this.Leg = Convert.ToInt32(row["Leg"]);
                this.Stop = Convert.ToInt32(row["Stop"]);
            }
            public DataTable Ch(string commandsqlstring, bool isStoredProcedure = false)
            {
                DataTable dataTable = new DataTable();
                ......
                        return dataTable;
            }
            public IEnumerable<T> ExcuteObject<T>(string storedProcedureorCommandText, bool isStoredProcedure = true)
            {
                List<T> items = new List<T>();
                      .....
                return items;
            }

        }
        static void Main(string[] args)
        {          
            string commandsqlstring = "Select top 10 MsgSN,FormID,Leg,Stop from tmail.MacroSendReceiveHistory order by ID desc";
            bool SP = false;
            List<MacroInfo> macroInfos = new List<MacroInfo>();
            macroInfos = ExcuteObject<MacroInfo>(commandsqlstring, SP).ToList();
            Console.ReadLine();
        }
    }
}

Why is it not seeing Excute Object?


Solution

  • ExcuteObject is method of class MacroInfo and not method of class Program. Your current implementation would have worked if ExcuteObject method was static and was method of class "Program".

    To fix this issue. You can do either of the following:

    1. Move ExcuteObject method to Program class and make it static.
    2. Keep ExcuteObject in MacroInfo class but make ExcuteObject method static and call it in Program class as MacroInfo.ExcuteObject(...)
    3. Create an instance of MacroInfo class and call the the method on instance of MacroInfo class.