jedbartlet Posted September 13, 2004 Posted September 13, 2004 I'm trying to display a number of panels on a form. The number of panels is determined by the user. I would like to create the panels dynamicly. The code i came up with is as follows: void MainForm1::setLayout(int no_boxes) { Panel *box[]; for(int i=0; i<=boxes; i++) { box = new Panel(); box->BorderStyle = BorderStyle::FixedSingle; //more properties set here.... } this->SuspendLayout(); this->tsetGbox->Controls->AddRange(box); this->ResumeLayout(); } When i run the app i get the following error message at runtime: Object reference not set to an object instance. Am i forgetting to use the this pointer somewhere or is it something else. I'm relatively new to visual C++ in case you can't allready tell! Any help apreciated. Quote
Administrators PlausiblyDamp Posted September 13, 2004 Administrators Posted September 13, 2004 C++ isn't my strong point (never mind managed C++) but I think the problem is Panel *box[]; as you are declaring the array but not sizing it. You will need to allocate an array big enough to hold no_boxes number of elements. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 13, 2004 Author Posted September 13, 2004 That's the problem, i don't know the size of the array because it is set at runtime via the function parameter no_boxes. I don't really want to have to create an array of size 100 when my default size is closer to 6! Any other suggestions?! Quote
Administrators PlausiblyDamp Posted September 13, 2004 Administrators Posted September 13, 2004 Really not sure if this syntax is correct but try something like Panel *box[] = new *box[no_boxes]; you may have to experiment with that as I am on dodgy ground with my C++ here ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 13, 2004 Author Posted September 13, 2004 Panel *box[] = new *box[no_boxes]; That was the exact code i came up on my first attempt. Unfortunately it doesn't compile. A syntax error * is displayed. Initially i thought that the * after new wasn't needed but removing it gives rise to a lot more syntax errors. Quote
Administrators PlausiblyDamp Posted September 13, 2004 Administrators Posted September 13, 2004 try Panel *box[] = new box*[no_boxes]; Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 13, 2004 Author Posted September 13, 2004 No joy! A syntax error :identifier 'box' stops compilation. _________________________________________ it's amazing how stupid the little things make you feel when you can't get them working! Quote
Administrators PlausiblyDamp Posted September 14, 2004 Administrators Posted September 14, 2004 Can't believe the simple mistake I made before :( try this Panel *box[] = new Panel*[no_boxes]; Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 14, 2004 Author Posted September 14, 2004 The code compiles but again as with my initial code i get a runtime error. Panel *box[] = new Panel *[no_boxes]; for(int i=0; i<no_boxes; i++) { box->BorderStyle = BorderStyle::FixedSingle; //more properties set here } this->SuspendLayout(); this->tsetGbox->Controls->AddRange(box); this->ResumeLayout(); Runtime error message: An unhandled exception has occurred......... Object reference not set to an instance of an object. It sounds to me like i've gone over the bounds of the array but i can't see how thats possible. This is really startin to bug me now! Once again any help appreciated. Quote
Administrators PlausiblyDamp Posted September 14, 2004 Administrators Posted September 14, 2004 Which line do you get the error on? Alsowhat are the values for no_boxes and i when you get this error? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 14, 2004 Author Posted September 14, 2004 Which line do you get the error on? Alsowhat are the values for no_boxes and i when you get this error? The error seems to occur whenever i try and change the first property of the panel which occurs at the first line in the for loop. I'm giving no_boxes the value 5, i've tried other values but the result is the same. I think the error is to do with pointers because when i run the program in the watch window i can see that an array of the correct size is being created. The watch also gives the following info- box [0], [1].....[4] value is <undefined value>. Does this mean that the panels are not being created properly?! Quote
Administrators PlausiblyDamp Posted September 14, 2004 Administrators Posted September 14, 2004 You will probably need to create a valid instance for each entry in the array for(int i=0; i{ box[i] = new Panel(); box[i]->BorderStyle = BorderStyle::FixedSingle; //etc. this is a bit of a guess based on C# - much easier than managed C++ ;) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jedbartlet Posted September 14, 2004 Author Posted September 14, 2004 And voila! got it working! Ironicly if you look at the code in my first post i pretty much had it right to begin with, just didn't create the array properly. Won't be making that mistake again in a hurry! Thanks for the help. One last question is it much of a leap from C++ to c#. You're about the tenth person who's suggested C#. Out of choice i'd have chosen c# but at uni we're taught delph initially then c++. The code samples i've seen seem straight forward enough but trying to learn the two languages at the same time could get confusing..... Quote
HJB417 Posted September 16, 2004 Posted September 16, 2004 And voila! got it working! Ironicly if you look at the code in my first post i pretty much had it right to begin with' date=' just didn't create the array properly. Won't be making that mistake again in a hurry! Thanks for the help. One last question is it much of a leap from C++ to c#. You're about the tenth person who's suggested C#. Out of choice i'd have chosen c# but at uni we're taught delph initially then c++. The code samples i've seen seem straight forward enough but trying to learn the two languages at the same time could get confusing.....[/quote'] There are times when I'd rather use c++ over c# and vice versa but here's my #1 rule, if the application is going to be purely .net and not rely on non-dotnet objects, c# is the winner. If, for some reason, you need to build a .net application that relies on com objects, or will need to do pinvokes, or want to mix native code with .net code in a single assembly, then c++ is the only way in some cases, and in general c++ is the way to go here. Now, some things are going to change in the .net 2.0. The most significant, is, .net objects can be created as stack variables, and once they leave scope, they will have their dispose method called automatically, something c# doesn't have. the using statement doesn't count as that's an explicit action. They did syntax changes in hopes of making the .net syntax prettier. If you're comfortable with c++, stick with it, you're not missing much in c# because in .net 2.0, I don't think there will be anything c# can do that c++.net can't. Quote
jedbartlet Posted September 16, 2004 Author Posted September 16, 2004 Out of curiosity what is the preferred language in industry, C++ or C#. Or does it vary from organisation to organisation. Where does jave fit in other than in web based applications. Quote
HJB417 Posted September 16, 2004 Posted September 16, 2004 Out of curiosity what is the preferred language in industry' date=' C++ or C#. Or does it vary from organisation to organisation. Where does jave fit in other than in web based applications.[/quote'] I don't know. But I'd go will the latter. My friend's workplace started with cc++ and is now using java. At my workplace we use .net. I know there's still demand for c++ and other languages though. Java fits in where you want portable/platform independent applications. There are others, but that's the 1st one that comes to mind. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.