不调用构造函数方法的类的新实例。 [英] New instance of a class not calling the constructor method.

查看:105
本文介绍了不调用构造函数方法的类的新实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从文件读取数据时,数据按预期读入,但是当读入所有数据时,窗口表单只是加载并且操作完成。它没有创建一个类的新实例(在这种情况下是一个带有吸引力的事件),但只是加载空白表单而不读取文件的下一部分。我一直在踩到最后一小时的断点,我无法弄清楚为什么它没有构建新事件。任何帮助?



When I read data in from a file, the data is read in as expected but when all data is read in, the windows form just loads up and the operation is finished. It has not created a new instance of a class (in this case an event with attractions) but simply loaded up the blank form without reading the next part of the file. I have been stepping in to breakpoints for the last hour and I cannot figure out why it is not constructing the new event. Any help?

public static void readEvents(string theFile, ArrayList allEvents)
        {
            //local variables
            StreamReader inEvents = null;
            bool anyMoreEvents = false;
            string[] eventData = new string[frmEVMHome.numEventItems];
            string[] attractionData = new string[frmEVMHome.numAttractionItems];
            Event tempEvent;
            Attraction tempAttraction;
            int numOfAttractionsInEvent;

            //if file opened ok proceed
            if (Utilities.fileOpenForReadOK(theFile, ref inEvents))
            {
                //read first event attractions
                anyMoreEvents = Utilities.getNext(frmEVMHome.numEventItems, inEvents, eventData);

                //loop for all events in file
                while (anyMoreEvents == true)
                {
                    numOfAttractionsInEvent = Convert.ToInt32(eventData[11]);
                    //EVENT ORDER = 1eventDescription 2eventDate 3eventStartTime 4eventEndTime 5eventAddress 6eventNumOfGuests
                    //7eventContactName 8eventContactemail 9eventContactPhone 10eventContactMobile 11eventCost 12eventNumOfAttractions 13eventAttractions.
                    tempEvent = new Event(eventData[0], Convert.ToDateTime(eventData[1]), Convert.ToInt32(eventData[2]),
                        Convert.ToInt32(eventData[3]), eventData[4], Convert.ToInt32(eventData[5]), eventData[6], eventData[7],
                            Convert.ToInt32(eventData[8]), Convert.ToInt32(eventData[9]), Convert.ToInt32(eventData[10]), numOfAttractionsInEvent);

                    //Read all attractions into event
                    for (int i = 0; i < numOfAttractionsInEvent; i++)
                    {
                        getNext(frmEVMHome.numAttractionItems, inEvents, attractionData);
                        //ATTRACTION ORDER = attractionDescription, attractionStaff, attractionLength, attractionWidth,
                            // attractionHeight, attractionCost.
                        tempAttraction = new Attraction(attractionData[0], Convert.ToInt32(attractionData[1]), Convert.ToInt32(attractionData[2]),
                            Convert.ToInt32(attractionData[3]), Convert.ToInt32(attractionData[4]), Convert.ToInt32(attractionData[5]));
                        tempEvent.addAttractionToEvent(tempEvent.getEventAttractions(), tempAttraction);
                    }

                    //Add Events (including all its attractions) into the data structure
                    allEvents.Add(tempEvent);
                    anyMoreEvents = getNext(frmEVMHome.numEventItems, inEvents, eventData);
                }





和类构造函数



And the Class constructor

public Event(string ineventDescription, DateTime ineventDate, int ineventStartTime,
                  int ineventEndTime, string ineventAddress, int ineventNumOfGuests,
                   string ineventContactName, string ineventContactEmail, int ineventContactPhone,
                    int ineventContactMobile, int ineventCost, int ineventNumOfAttractions)
     {
         eventDescription = ineventDescription;
         eventDate = ineventDate;
         eventStartTime = ineventStartTime;
         eventEndTime = ineventEndTime;
         eventAddress = ineventAddress;
         eventNumOfGuests = ineventNumOfGuests;
         eventContactName = ineventContactName;
         eventContactEmail = ineventContactEmail;
         eventContactPhone = ineventContactPhone;
         eventContactMobile = ineventContactMobile;
         eventCost = ineventCost;
         eventNumOfAttractions = 0;
         eventAttractions = new ArrayList(); //empty list for new object
     }

推荐答案

我做的第一件事是敲一个框架并创建一个实例:

The first thing I did was knock up a framework and create an instance:
public void DoIt()
    {
    Event e = new Event("", DateTime.Now, 0, 0, "", 0, "", "", 0, 0, 0, 0);
    }
public class Event
    {
    private string eventDescription;
    private DateTime eventDate;
    private int eventStartTime;
    private int eventEndTime;
    private string eventAddress;
    private int eventNumOfGuests;
    public Event(string ineventDescription, DateTime ineventDate, int ineventStartTime,
                int ineventEndTime, string ineventAddress, int ineventNumOfGuests,
                 string ineventContactName, string ineventContactEmail, int ineventContactPhone,
                  int ineventContactMobile, int ineventCost, int ineventNumOfAttractions)
        {
        eventDescription = ineventDescription;
        eventDate = ineventDate;
        eventStartTime = ineventStartTime;
        eventEndTime = ineventEndTime;
        eventAddress = ineventAddress;
        eventNumOfGuests = ineventNumOfGuests;
        //eventContactName = ineventContactName;
        //eventContactEmail = ineventContactEmail;
        //eventContactPhone = ineventContactPhone;
        //eventContactMobile = ineventContactMobile;
        //eventCost = ineventCost;
        //eventNumOfAttractions = 0;
        //eventAttractions = new ArrayList(); //empty list for new object
        }
    }



调试器显示正在调用的构造函数,以及相应的值调用后的实例属性。



从逻辑上讲,它必须是你的实例创建的代码:


And the debugger showed the constructor being called, and the appropriate values in the instance properties after the call.

So logically, it must be the code around your instance creation:

if (Utilities.fileOpenForReadOK(theFile, ref inEvents))
{
...
    while (anyMoreEvents == true)
    {
...
        tempEvent = new Event(eventData[0], Convert.ToDateTime(eventData[1]),



所以从调试器开始,断点高于 if 并确切地看到发生了什么 - 几乎可以肯定你没有进入构造函数只是因为围绕它的代码没有创建instanc es。



如果你到达构造函数,然后进入它 - 并确保你到达正确的位置。如果你可以;不介入,或者你得到奇怪的地方,你需要开始查看Event类的位置:如果它在不同的程序集中,那么你可能会引用一个旧版本。


So start with the debugger and a breakpoint above the if and see exactly what happens - almost certainly you aren't getting into the constructor simply because the code round it isn't creating instances.

If you do get to the constructor, then set into it - and make sure that you get to the right place. If you can;'t step in, or you get somewhere odd, you need to start looking at where the Event class is: if it's in a different assembly then you may be referencing an old version for example.


这篇关于不调用构造函数方法的类的新实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆