Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TStringList as a paramater

Status
Not open for further replies.

LastCyborg

Programmer
Feb 7, 2003
256
MX
Is there a way to pass as a parameter an instance of a TStringList Class???
In other words I need to create a class constructor that has it as parameter.

Here is some code of what I'm doing
//---------------------------------------------------------------------------
.h
//---------------------------------------------------------------------------
class TMyClass : public TObject
{
public:
TStringList *TheList;
__fastcall TMyClass (TStringList *StringList);
__fastcall ~TMyClass ();
};
//---------------------------------------------------------------------------

.cpp
//---------------------------------------------------------------------------
__fastcall TMyClass::TMyClass (TStringList *StringList)
{
TheList = new TStringList ();
TheList = StringList;
}
//---------------------------------------------------------------------------
__fastcall TMyClass::~TMyClass ()
{
delete TheList;
}
//---------------------------------------------------------------------------

Using my Object
//---------------------------------------------------------------------------
any event handler
{
MyObject = new TMyClass (dynamic_cast<TStringList *>(RichEdit1->Lines));
...
// some operations with the StringList
...
// sending the StringList to the RichEdit
...
delete MyObject;
}
//---------------------------------------------------------------------------
The problem is in the constructor, because when the object receives the parameter this is NULL (before the asign of TheList = StringList).

So, the question is about the StringList as a Parameter, why is NULL?
 
You are passing StringList into the Constructor, but not using it. When you use TheList to make a new TStringList, that makes TheList equal to the new TStringList that you created. Then you just set TheList equal to StringList, losing the memory and pointer for that new TStringList that you just created. Generally, if you pass a pointer in to a function, you would use it for something.

i.e. void __fastcall MyClass::MyClass(TStringList *List)
{
PrivateList = List;
}
where PrivateList is a private TStringList pointer defined in the class header.

Then you would use this function from another function like such:

void __fastcall TMyForm::SomeFunction(void)
{
TStringList *NewList = new TStringList();
NewMyClass = new MyClass(NewList);
}

Now that you've done this, the MyClass has access to that new StringList. If you make PrivateList to a public variable, say we call it PublicList instead, then you can access the list at any time like this:

void __fastcall TMyForm::SomeOtherFunction(void)
{
NewMyClass->PublicList->Strings[x] = &quot;Hello world&quot;;
}

This assumes NewMyClass is global and was created in the first function above.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top