❔ How do I take textbox input and make an object out of it, then add it to an array?
Here is my code : https://paste.mod.gg/nojduzpnglow/0
BlazeBin - nojduzpnglow
A tool for sharing your source code with the world!
16 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Is this WPF or WinForms or ...?
BlazeBin Basic - cwqxefuawskh
A tool for sharing your source code with the world!
@Denis sorry, does this link work? its winforms, and basically I want to retrieve the data from the textboxes and make an object (Person) out of it and add to an array
Here is an updated link:
BlazeBin - nojduzpnglow
A tool for sharing your source code with the world!
And what doesn't work?
I see you create a new
Person
successfully
At least I assume so, since you didn't mention any errors
And adding it to an array... well, it's a little tricky since you would also need to keep track on the last index you inserted into, so you can then add the object to the next index
Or just use a list instead of an array and .Add()
the elementahh thank you! I was trying to use the .Add() but I didn´t realize it doesnt work for array. In an array it must be append right?
@ZZZZZZZZZZZZZZZZZZZZZZZZZ if I would to keep it as an array, then I would have to use a for loop right? to make sure that each index is a new one.
No, arrays are fixed size. There's no add, no append, you can only set the value at specific index
Not necessarily, no
You can keep the index in a field, and then increment the value whenever you add a new person
So there is no way to add to an array other than specifying the index?
I was thinking for example
String [] persons = [20];
For (int i =0; i<=persons.Length; i++)
{
persons[i] = new person (values)
}
Dont mind the syntax, just trying to convey the idea🙃
Angius
REPL Result: Success
Result: int
Compile: 420.436ms | Execution: 29.540ms | React with ❌ to remove this embed.
The lenght always is... the length of the array
So it will not change with adding new elements, if that was what you were hoping
You can, of course, add elements in a loop
But you get individual, singular inputs, right?
One element with one click of a button
There's no loop involved
Must it be an array? Most C# developers would use a list here instead of an array.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.