Log in

View Full Version : Quick question



Footy
10-19-2012, 11:41 PM
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
10-19-2012, 11:44 PM
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
10-19-2012, 11:46 PM
Great, thanks!

Rezozo
10-20-2012, 12:13 AM
Nin...Ja...

Quick answer!

litoris
10-20-2012, 06:17 AM
Wait, can't you add it to the array without extending the length first?

riwu
10-20-2012, 06:28 AM
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
10-20-2012, 09:57 PM
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
10-20-2012, 10:06 PM
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
10-20-2012, 10:08 PM
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
10-20-2012, 10:11 PM
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
10-21-2012, 01:42 AM
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
10-21-2012, 01:57 AM
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
10-21-2012, 02:03 AM
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.