A little help needed!

jedbartlet

Member
Joined
Sep 10, 2004
Messages
20
Location
UK
Im 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. Im relatively new to visual C++ in case you cant allready tell! Any help apreciated.
 
C++ isnt my strong point (never mind managed C++) but I think the problem is
Code:
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.
 
Thats the problem, i dont know the size of the array because it is set at runtime via the function parameter no_boxes. I dont really want to have to create an array of size 100 when my default size is closer to 6!

Any other suggestions?!
 
Really not sure if this syntax is correct but try something like
Code:
Panel *box[] = new *box[no_boxes];

you may have to experiment with that as I am on dodgy ground with my C++ here ;)
 
Panel *box[] = new *box[no_boxes];

That was the exact code i came up on my first attempt. Unfortunately it doesnt compile. A syntax error * is displayed. Initially i thought that the * after new wasnt needed but removing it gives rise to a lot more syntax errors.
 
No joy! A syntax error :identifier box stops compilation.

_________________________________________
its amazing how stupid the little things make you feel when you cant get them working!
 
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 ive gone over the bounds of the array but i cant see how thats possible. This is really startin to bug me now! Once again any help appreciated.
 
PlausiblyDamp said:
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. Im giving no_boxes the value 5, ive 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?!
 
You will probably need to create a valid instance for each entry in the array
Code:
 for(int i=0; i<no_boxes; 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++ ;)
 
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 didnt create the array properly. Wont 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#. Youre about the tenth person whos suggested C#. Out of choice id have chosen c# but at uni were taught delph initially then c++. The code samples ive seen seem straight forward enough but trying to learn the two languages at the same time could get confusing.....
 
jedbartlet said:
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 didnt create the array properly. Wont 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#. Youre about the tenth person whos suggested C#. Out of choice id have chosen c# but at uni were taught delph initially then c++. The code samples ive seen seem straight forward enough but trying to learn the two languages at the same time could get confusing.....

There are times when Id rather use c++ over c# and vice versa but heres 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# doesnt have. the using statement doesnt count as thats an explicit action. They did syntax changes in hopes of making the .net syntax prettier. If youre comfortable with c++, stick with it, youre not missing much in c# because in .net 2.0, I dont think there will be anything c# can do that c++.net cant.
 
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.
 
jedbartlet said:
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.

I dont know. But Id go will the latter.
My friends workplace started with cc++ and is now using java. At my workplace we use .net. I know theres still demand for c++ and other languages though.
Java fits in where you want portable/platform independent applications. There are others, but thats the 1st one that comes to mind.
 
Back
Top