Sign in to follow this  

[.net] OOP Problem

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I would like to ask how to use the object class destructor in VC++ Express Edition and solve my problem below. Here is my object: ref class Support : public System:bject { public: Support() { SupportDate = DateTime::Now; SupportName = ""; SupportTime = DateTime::Now; CustComp = ""; CustName = ""; SupportType = ""; FinishTime = DateTime::Now; } public: Support(DateTime ^sdate, String ^sname,DateTime ^stime, String ^ccomp, String ^cname, String ^stype, DateTime ^ftime) { SupportDate = sdate; SupportName = sname; SupportTime = stime; CustComp = ccomp; CustName = cname; SupportType = stype; FinishTime = ftime; } //here is the problem: public: String^ writeLine() { return String::Concat(SupportDate->ToString(),",",SupportName,",",SupportTime->ToString(),",", CustComp,",",CustName,",",SupportType,",",FinishTime->ToString()); } public: DateTime ^SupportDate; String ^SupportName; DateTime ^SupportTime; String ^CustComp; String ^CustName; String ^SupportType; DateTime ^FinishTime; }; What I want to do with this Support object class is to load a text file of CSV format into objects depending on the amount of text file lines and populate them in arraylist. After that, I will have to display these data on a listView and must be able to accept data addition, data edit and data deletion. When the application closed, I must save all these changes back to the text file of CSV format. So in other words, I will use the object as a temporary data storage in memory when my application run. I have been successful in running my application. The chronology were: 1. The first time I ran my application, the file has not been created yet. So I must do data addition. The steps are: I fill the textBoxes related with the object then I click Button1. If I want to add 10 data then I must fill the textBoxes 10 times and click the Button1 10 times, too. At last, I click Button2 to save the data into text file. OR you can also do after clicking Button1, click Button2 but it is not efficient remembering that the file must be deleted, and recreated. There was no problem until these steps finished. After adding data, I close my application. 2. I ran my application again. There was no problem and all data from previous addition were successfully loaded. Then I tried to add new data, with the same steps like number 1, and AT LAST I click Button2, and the error occured. The error was: Object reference not set to an instance of an object. ************** Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at Asm2.Form1.Support.writeLine() in c:\chris\assignment2\assignment2oop\asm2\form1.h:line 55 is the problem with method writeLine on the Support object at Asm2.Form1.button2_Click(Object sender, EventArgs e) in c:\chris\assignment2\assignment2oop\asm2\form1.h:line 202 is the problem with method writeLine on Button2_Click event. Did I miss something or I have to add the public: ~Support(); to my Support object? But the problem is different if I use the public: ~Support(); on my object. It was error compile like this: Asm2.obj : error LNK2020: unresolved token (0600000C) Support::~Support Is it possible for someone to fix my code sample here? Thank you very much. Here is the source: Button1 handles the data addition. So everytime I want to add new data, I create a new instance of the Support object. private: ArrayList ^lstsupport; private: System::Void button1_Click(System:bject^ sender, System::EventArgs^ e) { //add data Support^ objsupport = gcnew Support; objsupport->SupportName = textBox1->Text; objsupport->CustComp = textBox2->Text; objsupport->CustName = textBox3->Text; objsupport->SupportType = textBox4->Text; //MessageBox:how(objsupport->writeLine()); lstsupport->Add(objsupport); MessageBox:how(lstsupport->Count.ToString()); } Button2 handles saving data to the text file. private: System::Void button2_Click(System:bject^ sender, System::EventArgs^ e) { Support ^supp; String ^fileName = "\\support.txt"; String^ path = String::Concat(Application:tartupPath,fileName); if (File::Exists(path)) File:elete(path); StreamWriter ^sw = File::CreateText(path); try { for(int i = 0; i < lstsupport->Count; i++) { supp = dynamic_cast<Support^>(lstsupport); sw->Write(supp->writeLine()); <--------Here is the problem. sw->WriteLine(); } //for } //try finally { if ( sw ) delete (IDisposable^)sw; } } On Form Load, what I did is reading the text file and load them into the Support object. private: System::Void Form1_Load(System:bject^ sender, System::EventArgs^ e) { lstsupport = gcnew ArrayList; //tempsupport = gcnew ArrayList; Support ^tsupp = gcnew Support; // String^ filename = "\\support.txt"; String ^path = String::Concat(Application:tartupPath,filename); if (File::Exists(path)) { StreamReader^ sr = File:penText( path ); try { String^ buffer = ""; String^ temp; while ( buffer = sr->ReadLine() ) { temp = buffer; array<Char>^chars = {','}; //code snippet from String:plit array<String^>^ arr = temp->Split(chars); //isi Object for (int j = 0; j < arr->Length ; j++) { //supp = gcnew Support(dynamic_cast<DateTime^>(arr[0]),arr[1],dynamic_cast<DateTime^>(arr[2]),arr[3],arr[4],arr[5],dynamic_cast<DateTime^>(arr)); tsupp->SupportDate = dynamic_cast<DateTime^>(arr[0]); tsupp->SupportName = arr[1]; tsupp->SupportTime = dynamic_cast<DateTime^>(arr[2]); tsupp->CustComp = arr[3]; tsupp->CustName = arr[4]; tsupp->SupportType = arr[5]; tsupp->FinishTime = dynamic_cast<DateTime^>(arr); } //MessageBox:how(supp->CustName); lstsupport->Add(tsupp); //MessageBox:how(supp->writeLine()); //isi Object } //while buffer //MessageBox:how("Loaded: " + lstsupport->Count.ToString() + " data."); } //try finally { if ( sr ) delete (IDisposable^)sr; } //finally } //if File::Exists(path) // }

Share this post


Link to post
Share on other sites

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this