❔ Adding to Arrays Via Methods

I am having trouble getting arrays to be changed by methods. I tried lists as well. Please send help.
82 Replies
Angius
Angius2y ago
You can't append items to arrays They're fixed-size Use a List<T>
HumNumWorld
HumNumWorldOP2y ago
string[] plants = {};

createPlant(ref plants);
createPlant(ref plants);
createPlant(ref plants);
Console.WriteLine(plants[0] + plants[1]);
Console.ReadLine();
// avolva aventine
static void createPlant(ref string[] plants)
{
Console.WriteLine("What is the Name of your Plant?");
plants = new string[] { Console.ReadLine() };
Console.Write("Completed! Push any Button to Exit.");
Console.ReadLine();
//may just have to for now.
string print = "";
for (int i = 0; i < plants.Length; i++)
{
Console.Write(plants[i] + ", ");
}
Console.WriteLine(print);
Console.ReadLine();
}
string[] plants = {};

createPlant(ref plants);
createPlant(ref plants);
createPlant(ref plants);
Console.WriteLine(plants[0] + plants[1]);
Console.ReadLine();
// avolva aventine
static void createPlant(ref string[] plants)
{
Console.WriteLine("What is the Name of your Plant?");
plants = new string[] { Console.ReadLine() };
Console.Write("Completed! Push any Button to Exit.");
Console.ReadLine();
//may just have to for now.
string print = "";
for (int i = 0; i < plants.Length; i++)
{
Console.Write(plants[i] + ", ");
}
Console.WriteLine(print);
Console.ReadLine();
}
@Angius thanks but look at this
HumNumWorld
HumNumWorldOP2y ago
HumNumWorld
HumNumWorldOP2y ago
its error after error
Angius
Angius2y ago
The parameter is declared as a string[], so obviously it won't take a List<string> Also, in your CreatePlant() method you crate a brand new array You don't add anything, you replace
HumNumWorld
HumNumWorldOP2y ago
can you explain that
Angius
Angius2y ago
What's to explain there? Square peg doesn't fit into a round hole int and string are different types So are boolean and UngaBunga
HumNumWorld
HumNumWorldOP2y ago
where is the square peg
Angius
Angius2y ago
So are string[] and List<string> List<string> is the square peg The method has a round hole, string[] type parameter List<T> is not T[]
HumNumWorld
HumNumWorldOP2y ago
oh ignore the writeline
Angius
Angius2y ago
Nothing to do with any writeline
HumNumWorld
HumNumWorldOP2y ago
HumNumWorld
HumNumWorldOP2y ago
right here where is the []
HumNumWorld
HumNumWorldOP2y ago
HumNumWorld
HumNumWorldOP2y ago
and ignore line 27 for now im just trying to get the errors off the first part until it functions
Angius
Angius2y ago
Aight, so, why do you try to pass ref List<string> plants as a parameter? Do you also use Console.WriteLine(string "message")?
HumNumWorld
HumNumWorldOP2y ago
apparently to change a variable in a function you have to use ref I tried just "plants" but it said it has to be written like a list
Angius
Angius2y ago
Yeah, in the parameter declaration
HumNumWorld
HumNumWorldOP2y ago
was there a better way of doing that
Angius
Angius2y ago
When you call the method, you don't supply a type You just pass the param
HumNumWorld
HumNumWorldOP2y ago
wym
Angius
Angius2y ago
CreatePlant(plants)
HumNumWorld
HumNumWorldOP2y ago
oh
HumNumWorld
HumNumWorldOP2y ago
HumNumWorld
HumNumWorldOP2y ago
like this?
Angius
Angius2y ago
Not here Jesus $helloworld
HumNumWorld
HumNumWorldOP2y ago
im used to javascript
Angius
Angius2y ago
You really should brush up the basics
HumNumWorld
HumNumWorldOP2y ago
its much easier when I could do something like: appendItem("plants", ""); which I dont even know if thats a real thing
Angius
Angius2y ago
And you can
HumNumWorld
HumNumWorldOP2y ago
because I learned from code.org
Angius
Angius2y ago
var plants = new List<string>();
plants.Add("carrot");
plants.Add("hydrangea");
plants.Add("juniper");
var plants = new List<string>();
plants.Add("carrot");
plants.Add("hydrangea");
plants.Add("juniper");
List has the .Add() method
HumNumWorld
HumNumWorldOP2y ago
yeah see I get that but I called plants in main and im trying to add in a method and its causing tons of errors to get it through the parameters like in js, everything is global pretty much I just say:
var x = 1

function addOne() {
x ++;
}
var x = 1

function addOne() {
x ++;
}
and var becomes 2 if I call the function in c# i'm just lost with methods and why they are so isolated you have to use ref for some reason for it to change anything, and even still its like a maze to get the errors to be broken
Angius
Angius2y ago
Angius
Angius2y ago
For reference types, you don't need to use ref
HumNumWorld
HumNumWorldOP2y ago
List<string> plants = new List<string>();
createPlant(ref List<string> plants);
Console.ReadLine();

static void createPlant(plants)
{
Console.WriteLine("What is the Name of your Plant?");
plants.Add(Console.ReadLine());
Console.Write("Completed! Push any Button to Exit.");
Console.ReadLine();
//may just have to for now.
string print = "";
for (int i = 0; i < plants.Length; i++)
{
Console.Write(plants[i] + ", ");
}
Console.WriteLine(print);
Console.ReadLine();
}
List<string> plants = new List<string>();
createPlant(ref List<string> plants);
Console.ReadLine();

static void createPlant(plants)
{
Console.WriteLine("What is the Name of your Plant?");
plants.Add(Console.ReadLine());
Console.Write("Completed! Push any Button to Exit.");
Console.ReadLine();
//may just have to for now.
string print = "";
for (int i = 0; i < plants.Length; i++)
{
Console.Write(plants[i] + ", ");
}
Console.WriteLine(print);
Console.ReadLine();
}
oh let me read this rq
Angius
Angius2y ago
And here's the fix to your issue You declare types when declaring the method There and only ever there When calling that method, you don't declare any types Just pass the params
HumNumWorld
HumNumWorldOP2y ago
Angius
Angius2y ago
Cool, what does the error say
HumNumWorld
HumNumWorldOP2y ago
argument 1 must be passed with the 'ref' keyword
Angius
Angius2y ago
Well, try that
HumNumWorld
HumNumWorldOP2y ago
oh wow ok brb ok yeah its cleared up next issue though
HumNumWorld
HumNumWorldOP2y ago
Angius
Angius2y ago
Again, what does the error say? probably says that .Length property doesn't exist on List<T>
HumNumWorld
HumNumWorldOP2y ago
Angius
Angius2y ago
That's because those are the properties List<T> has: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-7.0#properties .Length is not among them .Count, however, is
HumNumWorld
HumNumWorldOP2y ago
are there any substitutes ah thats weird i'll try it oh wow thats wonderful it works ok now I need a few clarifications so I really grasp this this is whats confusing me its the whole parameters and redeclarations thing
Angius
Angius2y ago
The method declares what it wants
void Foo(string x, int y) {}
void Foo(string x, int y) {}
like this one, that wants a string and an int When calling that method, you don't need to say that you're passing a string
HumNumWorld
HumNumWorldOP2y ago
hold up
Angius
Angius2y ago
No need to do
string x = "hello";
Foo(string x, int 69);
string x = "hello";
Foo(string x, int 69);
because the method will not let you pass anything else. You just do
string x = "hello";
Foo(x, 69);
string x = "hello";
Foo(x, 69);
HumNumWorld
HumNumWorldOP2y ago
wow so ref only needs to be sent in the declaration of the method
Angius
Angius2y ago
Admittedly, I haven't really worked with ref
HumNumWorld
HumNumWorldOP2y ago
it seems so obvious, swapping syntaxes really gets me a headache
Angius
Angius2y ago
To begin with, List<T> already is a reference type
HumNumWorld
HumNumWorldOP2y ago
a what?
Angius
Angius2y ago
You're already passing a reference to it, not a copy $refvsvalue
Angius
Angius2y ago
In C# there are value types — mostly the basic ones like int, bool, etc And reference types — custom classes and stuff Value types, when passed, create a copy Reference types pass a reference
HumNumWorld
HumNumWorldOP2y ago
oh so I only use ref when I need to make the copy change something outside
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
var list = new List<int> { 1, 2, 3 };

static void AddNumber(List<int> l)
{
l.Add(Random.Shared.Next(0, 100));
}

AddNumber(list);
AddNumber(list);
AddNumber(list);

list
var list = new List<int> { 1, 2, 3 };

static void AddNumber(List<int> l)
{
l.Add(Random.Shared.Next(0, 100));
}

AddNumber(list);
AddNumber(list);
AddNumber(list);

list
Result: List<int>
[
1,
2,
3,
25,
48,
93
]
[
1,
2,
3,
25,
48,
93
]
Compile: 606.838ms | Execution: 36.419ms | React with ❌ to remove this embed.
Angius
Angius2y ago
ref coerces value types to be passed as reference
HumNumWorld
HumNumWorldOP2y ago
wait so saying a variable prints it? I see you wrote list
Angius
Angius2y ago
That list without a semicolon is just something the repl of the bot does
HumNumWorld
HumNumWorldOP2y ago
ah I had trouble earlier with printing arrays
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
int x = 96;

void Foo(int num) {
num = 420;
Console.WriteLine($"num is {num}");
}
Foo(x);
Console.WriteLine(x);

void Bar(ref int num) {
num = 420;
Console.WriteLine($"num is {num}");
}
Bar(ref x);
Console.WriteLine(x);
int x = 96;

void Foo(int num) {
num = 420;
Console.WriteLine($"num is {num}");
}
Foo(x);
Console.WriteLine(x);

void Bar(ref int num) {
num = 420;
Console.WriteLine($"num is {num}");
}
Bar(ref x);
Console.WriteLine(x);
Console Output
num is 420
96
num is 420
420
num is 420
96
num is 420
420
Compile: 681.702ms | Execution: 78.655ms | React with ❌ to remove this embed.
Angius
Angius2y ago
Here's how ref works for value types Without ref, the change happened inside of the method, the copy of the number was changed With ref, the change happened inside and outside of the method
HumNumWorld
HumNumWorldOP2y ago
Sorry my network is acting up. So I get it now! reference = ref. if I did val to a ref then would it act like a value?
Angius
Angius2y ago
No You can pass a value type as a reference, but you can't turn a reference type into a value type
HumNumWorld
HumNumWorldOP2y ago
hm and I know this is off topic but what do you do for the " cannot implicitly convert type 'string' to 'int' " error
Angius
Angius2y ago
int.Parse() for example Or better yet, int.TryParse() But this one's a little more complex
HumNumWorld
HumNumWorldOP2y ago
what what do those mean
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
string str = "69";
int num = int.Parse(str);
num
string str = "69";
int num = int.Parse(str);
num
Result: int
69
69
Compile: 475.533ms | Execution: 31.322ms | React with ❌ to remove this embed.
HumNumWorld
HumNumWorldOP2y ago
what does the word parse mean
Angius
Angius2y ago
It's the name of the method...? Like Console.WriteLine()
HumNumWorld
HumNumWorldOP2y ago
I'm guessing over all it converts
Angius
Angius2y ago
It parses a string into a number, yeah You could say converts, sure
HumNumWorld
HumNumWorldOP2y ago
and one last question how do you close the program through code
Angius
Angius2y ago
Once it completes, it'll just close IIRC you can just return from it as well
HumNumWorld
HumNumWorldOP2y ago
ah ty thats everything, thank you for your help!
Angius
Angius2y ago
Anytime
Accord
Accord2y 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