C
C#15mo ago
Emelie

❔ How do I take textbox input and make an object out of it, then add it to an array?

BlazeBin - nojduzpnglow
A tool for sharing your source code with the world!
16 Replies
Jimmacle
Jimmacle15mo ago
$code
MODiX
MODiX15mo ago
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/
Denis
Denis15mo ago
$details
MODiX
MODiX15mo ago
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)
Denis
Denis15mo ago
Is this WPF or WinForms or ...?
Emelie
EmelieOP15mo ago
Emelie
EmelieOP15mo ago
@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:
Emelie
EmelieOP15mo ago
BlazeBin - nojduzpnglow
A tool for sharing your source code with the world!
Angius
Angius15mo ago
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 element
Emelie
EmelieOP15mo ago
ahh 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.
Angius
Angius15mo ago
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
Emelie
EmelieOP15mo ago
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🙃
MODiX
MODiX15mo ago
Angius
REPL Result: Success
var arr = new int[20];
arr.Length
var arr = new int[20];
arr.Length
Result: int
20
20
Compile: 420.436ms | Execution: 29.540ms | React with ❌ to remove this embed.
Angius
Angius15mo ago
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
Pobiega
Pobiega15mo ago
Must it be an array? Most C# developers would use a list here instead of an array.
Accord
Accord15mo ago
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.
Want results from more Discord servers?
Add your server