Board index » cppbuilder » Re: Probably a stupid question about attached objects in a combobox

Re: Probably a stupid question about attached objects in a combobox


2003-09-19 01:24:07 PM
cppbuilder68
"mickey" < XXXX@XXXXX.COM >wrote in message
Quote
//then i try to access it somewhere
//but im not sure about this part
int iNum[2];
iNum = (int *)ComboBox1->Items->Objects[ComboBox1->ItemIndex];
You are casting correctly, you just don't have the variable declared
correctly. Just declare a pointer, nothing else:
int *iNum;
Gambit
 
 

Re:Re: Probably a stupid question about attached objects in a combobox

hello again! :)
thanks Gambit for the reply.
i did what u said, but now im having trouble with it.
(again? stupig me).
Here's my code (just a project that does nothingelse but this)
############################################################
void __fastcall TForm2::FormCreate(TObject *Sender)
{
int iNum[] = {1,0};
for (int i=0;i<5;i++) {
iNum[0]= i;
cb->AddItem("Num"+IntToStr(i), (TObject *)iNum);
}
}
//------------
void __fastcall TForm2::cbChange(TObject *Sender)
{
int *aNum = (int *)cb->Items->Objects[cb->ItemIndex];
ShowMessage(IntToStr(aNum[0]));
}
//------------
############################################################
Everytime i change the combobox, the ShowMessage gives me a 7-digit number (sumthing like 1244928)
Im really not sure how to do this.
Maybe im attaching it incorrectly, or in the way i access it, or its just plain carelessness.
but please do help.
Thanks.
 

Re:Re: Probably a stupid question about attached objects in a combobox

"mickey" < XXXX@XXXXX.COM >wrote in message
Quote
void __fastcall TForm2::FormCreate(TObject *Sender)
{
int iNum[] = {1,0};

for (int i=0;i<5;i++) {
iNum[0]= i;
cb->AddItem("Num"+IntToStr(i), (TObject *)iNum);
}
That won't work. Your array is declared on the stack locally to
FormCreate() only (which, BTW, you should not be using to begin with, but
that is a separate issue) . As soon as FormCreate() returns, the array will
be out of scope and no longer valid. Any code that tries to access the
array memory later on will have undefined behavior.
You have three options:
1) dynamically allocate the array on the heap instead, so that its memory
remains allocated and remains in scope after FormCreate() returns. It will
be your responsibility to free the memory before your application
terminates.
2) move the array into the form's header file as a member of the form class.
That way the array remains in memory and scop for the lifetime of the form
instance.
3) move the array declaration out of the class altogether and declare it as
static global memory. That way it will remain in memory and scope for the
lifetime of the application.
Quote
Everytime i change the combobox, the ShowMessage gives me a 7-digit number
(sumthing like 1244928)
Consider yourself lucky that your program didn't simply crash altogether.
Technically, you are accessing invalid memory. However, the access is
actually undefined, so anything can happen. Sounds like you just happen to
be lucky enough that the physical memory where the array used to reside is
still readible by the process, so the code did not crash, although the
memory contained meaningless data, at least.
Gambit
 

{smallsort}

Re:Re: Probably a stupid question about attached objects in a combobox

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
You have three options:
Given the nature of the earlier example, if I interpreted correctly what you
are trying to do, I would suggest the following for you:
typedef struct
{
int i1;
int i2;
} MyStruct;
class TForm2
{
__published:
void __fastcall cbChange(TObject *Sender);
private:
MyStruct Num[5];
public:
__fastcall TForm2(TComponent *Owner);
};
__fastcall TForm2::TForm2(TComponent *Owner)
: TForm(Owner)
{
for(int i = 0; i < 5; ++i)
{
Num[i].i1 = i;
cb->AddItem("Num"+AnsiString(i), (TObject *)&Num[i]);
}
}
void __fastcall TForm2::cbChange(TObject *Sender)
{
MyStruct *pNum = (MyStruct *)cb->Items->Objects[cb->ItemIndex];
ShowMessage(pNum->i1);
}
Gambit
 

Re:Re: Probably a stupid question about attached objects in a combobox

Yes! Now i get it!
now im using this code:
##############################################################
void __fastcall TForm2::FormCreate(TObject *Sender)
{
int *iNum;
for (int i=0;i<5;i++) {
iNum = new int[2];
iNum[0]= i;
iNum[1]= i*2;
cb->AddItem("Num"+IntToStr(iNum[0]), (TObject *)iNum);
}
}
##############################################################
and now it works! :D
dear mr.Gambit,
thank you, thank you, thank you! :)