BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Add v. =

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Jim Taylor
Guest





PostPosted: Mon Nov 07, 2005 1:07 pm    Post subject: Add v. = Reply with quote



Why does this work:

str1=MemoF->Lines->Strings[a];
b=str1.Pos(",");
str2=str1.SubString(1,b-1);
MemoX->Lines->Add(str2);

and this doesn't:

str1=MemoF->Lines->Strings[a];
b=str1.Pos(",");
str2=str1.SubString(1,b-1);
MemoX->Lines->Strings[a++]=str2;

MemoX starts the exercise blank. Portions of MemoF - 106 lines - are being
edited and transferred to MemoX. The first two lines are transferred by the
last code, and then the transfer stops. I have checked, and every line does
what it is supposed to do after the first two entries except the last line
of code. The "Add" does the job properly.

Just curious.

Jim Taylor





Back to top
Alan Bellingham
Guest





PostPosted: Mon Nov 07, 2005 1:47 pm    Post subject: Re: Add v. = Reply with quote



"Jim Taylor" <drivingiron (AT) bellsouth (DOT) net> wrote:

Quote:
Why does this work:

[snip]

Quote:
MemoX->Lines->Add(str2);

This adds a string to the container.

Quote:
and this doesn't:

MemoX->Lines->Strings[a++]=str2;

This sets a given string in the container to a new value.

Quote:
MemoX starts the exercise blank.

Or, in other words, the given string doesn't exist. Attempting to assign
to it is therefore prone to error.

Some containers are nice - and will create an entry if necessary. Array
style ones usually don't, perhaps because it doesn't usually help very
much - the most usual error condition in my experience is for the index
to be negative, or when cast to unsigned, a number so vast that creating
all the intermediate values blows the whole system out of the water.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK

Back to top
liz
Guest





PostPosted: Mon Nov 07, 2005 2:01 pm    Post subject: Re: Add v. = Reply with quote



On Mon, 7 Nov 2005 08:07:01 -0500, Jim Taylor wrote:

Quote:
and this doesn't:

Because Add() adds a string. and Strings[Index] sets an existing
string. If that string doesn't exist, that is, if there aren't at
least Index+1 elements in Strings, then you haven't done anything.

--
liz

Back to top
Jim Taylor
Guest





PostPosted: Mon Nov 07, 2005 3:35 pm    Post subject: Re: Add v. = Reply with quote

Many thanks, Liz.

Jim Taylor

(PS: I still think it would be nice if it worked that way.)
"liz" <lizalbinNotThis (AT) yahoo (DOT) com> wrote

Quote:
On Mon, 7 Nov 2005 08:07:01 -0500, Jim Taylor wrote:

and this doesn't:

Because Add() adds a string. and Strings[Index] sets an existing
string. If that string doesn't exist, that is, if there aren't at
least Index+1 elements in Strings, then you haven't done anything.

--
liz



Back to top
liz
Guest





PostPosted: Mon Nov 07, 2005 3:37 pm    Post subject: Re: Add v. = Reply with quote

On Mon, 7 Nov 2005 10:35:24 -0500, Jim Taylor wrote:

Quote:
Many thanks, Liz.

You're welcome, now thank Alan, who also explained why the mechanism
works as it does.

You might also look at std::vector (or any of the other standard
containers) to check how containers are implemented and the choices
that are typically made
--
liz

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.