SRL Archive - Help

Quick question


Footy

2012-10-19 19:41:56

Okay, so I have a string declared named String1. I have a TStringArray declared named StringArray1. How do I add String1 into StringArray1?

Thanks!
Footy

Nebula

2012-10-19 19:44:58

SetLength(StringArray1, Length(StringArray1) + 1);
StringArray1[ ] := String1;

First you have to set the length to one above the current length to make room for a new variable, then set the value of that new space as the string.

Footy

2012-10-19 19:46:11

Great, thanks!

Rezozo

2012-10-19 20:13:24

Nin…Ja…

Quick answer!

litoris

2012-10-20 02:17:36

Wait, can't you add it to the array without extending the length first?

riwu

2012-10-20 02:28:28

From: litoris
Wait, can't you add it to the array without extending the length first?

Nope, unless the array already has empty elements (which it shouldn't unless you extend/declare it).
You could, however, reassign the variable freely to of any length.

Footy

2012-10-20 17:57:09

An addition the the above question:

I'm making a form, and I'm trying to add the text in edit1 to the UsernameArray. However, when I run the below code, I get a syntax error. I'm putting a string(Edit1.Text) into a TStringArray(UsernameArray). Any Idea what the problem is?

Procedure Button2Click(Sender: TObject);
Begin
SetLength(UsernameArray, Length(UsernameArray) + 1);
UsernameArray[ ] := frmdesign.edit1.TEXT;
End;

Nebula

2012-10-20 18:06:25

From: Footy
An addition the the above question:

I'm making a form, and I'm trying to add the text in edit1 to the UsernameArray. However, when I run the below code, I get a syntax error. I'm putting a string(Edit1.Text) into a TStringArray(UsernameArray). Any Idea what the problem is?

Procedure Button2Click(Sender: TObject);
Begin
SetLength(UsernameArray, Length(UsernameArray) + 1);
UsernameArray[ ] := frmdesign.edit1.TEXT;
End;


Semi-my fault for assuming you knew something that you don't


say you have an array of integers such as:
[4, 3, 2, 6, 5, 8]

and lets say you want to add a 9 to the end. SetLength(UsernameArray, Length(UsernameArray) + 1) creates a new space for a new value to be added:
[4, 3, 2, 6, 5, 8, ]

That new space is 6th in the array.

To give the 6th place a new value, put a 6 in the brackets.

Array[6] := 9;

of course you won't be using a 6, you would be using the last point in the array, so use Length(UsernameArray)

UsernameArray[Length(UsernameArray)] := frmdesign.edit1.TEXT;

Footy

2012-10-20 18:08:53

Sorry, I've never worked with array's apart from TPA's. That makes more sense, thanks!

I actually still can't rep you… not sure why, I havn't repped you in weeks.

Nebula

2012-10-20 18:11:59

It's because you have to give other people rep. (I think) it's not time based. That's why it says "you have to spread around more reputation before…". I know you love me, but other people deserve rep too.

riwu

2012-10-20 21:42:02

From: Nebula
Semi-my fault for assuming you knew something that you don't


say you have an array of integers such as:
[4, 3, 2, 6, 5, 8]

and lets say you want to add a 9 to the end. SetLength(UsernameArray, Length(UsernameArray) + 1) creates a new space for a new value to be added:
[4, 3, 2, 6, 5, 8, ]

That new space is 6th in the array.

To give the 6th place a new value, put a 6 in the brackets.

Array[6] := 9;

of course you won't be using a 6, you would be using the last point in the array, so use Length(UsernameArray)

UsernameArray[Length(UsernameArray)] := frmdesign.edit1.TEXT;

That would result in an out of range error, either use Length(Array) - 1, or simply, High(Array). Always remember that when accessing the data elements they start from 0 ;)

Footy

2012-10-20 21:57:55

That might be the source of the runtime errors I've been getting. Riwu, are you any good with forms? I'm experiencing a few "problems" with mine, it would be great if you could help me out. :)

riwu

2012-10-20 22:03:32

I've read a few tutorials about them but never really used them since i don't find them necessary (consider it an anti-leech if someone can't fill out simple settings ;)). Probably just post your questions on forum so that others could help you as well.