C#
C#

help

Root Question Message

HumNumWorld
HumNumWorld2/6/2023
❔ Adding to Arrays Via Methods

I am having trouble getting arrays to be changed by methods. I tried lists as well. Please send help.
Angius
Angius2/6/2023
You can't append items to arrays
Angius
Angius2/6/2023
They're fixed-size
Angius
Angius2/6/2023
Use a List<T>
HumNumWorld
HumNumWorld2/6/2023
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();
}
HumNumWorld
HumNumWorld2/6/2023
@85903769203642368 thanks but look at this
HumNumWorld
HumNumWorld2/6/2023
HumNumWorld
HumNumWorld2/6/2023
its error after error
Angius
Angius2/6/2023
The parameter is declared as a string[], so obviously it won't take a List<string>
Angius
Angius2/6/2023
Also, in your CreatePlant() method you crate a brand new array
Angius
Angius2/6/2023
You don't add anything, you replace
HumNumWorld
HumNumWorld2/6/2023
can you explain that
Angius
Angius2/6/2023
What's to explain there?
Angius
Angius2/6/2023
Square peg doesn't fit into a round hole
Angius
Angius2/6/2023
int and string are different types
Angius
Angius2/6/2023
So are boolean and UngaBunga
HumNumWorld
HumNumWorld2/6/2023
where is the square peg
Angius
Angius2/6/2023
So are string[] and List<string>
Angius
Angius2/6/2023
List<string> is the square peg
Angius
Angius2/6/2023
The method has a round hole, string[] type parameter
Angius
Angius2/6/2023
List<T> is not T[]
HumNumWorld
HumNumWorld2/6/2023
oh ignore the writeline
Angius
Angius2/6/2023
Nothing to do with any writeline
HumNumWorld
HumNumWorld2/6/2023
HumNumWorld
HumNumWorld2/6/2023
right here where is the []
HumNumWorld
HumNumWorld2/6/2023
HumNumWorld
HumNumWorld2/6/2023
and ignore line 27 for now
HumNumWorld
HumNumWorld2/6/2023
im just trying to get the errors off the first part until it functions
Angius
Angius2/6/2023
Aight, so, why do you try to pass ref List<string> plants as a parameter?
Angius
Angius2/6/2023
Do you also use Console.WriteLine(string "message")?
HumNumWorld
HumNumWorld2/6/2023
apparently to change a variable in a function you have to use ref
HumNumWorld
HumNumWorld2/6/2023
I tried just "plants" but it said it has to be written like a list
Angius
Angius2/6/2023
Yeah, in the parameter declaration
HumNumWorld
HumNumWorld2/6/2023
was there a better way of doing that
Angius
Angius2/6/2023
When you call the method, you don't supply a type
Angius
Angius2/6/2023
You just pass the param
HumNumWorld
HumNumWorld2/6/2023
wym
Angius
Angius2/6/2023
CreatePlant(plants)
HumNumWorld
HumNumWorld2/6/2023
oh
HumNumWorld
HumNumWorld2/6/2023
HumNumWorld
HumNumWorld2/6/2023
like this?
Angius
Angius2/6/2023
Not here
Angius
Angius2/6/2023
Jesus
Angius
Angius2/6/2023
$helloworld
HumNumWorld
HumNumWorld2/6/2023
im used to javascript
Angius
Angius2/6/2023
You really should brush up the basics
HumNumWorld
HumNumWorld2/6/2023
its much easier when I could do something like:

appendItem("plants", "");
HumNumWorld
HumNumWorld2/6/2023
which I dont even know if thats a real thing
Angius
Angius2/6/2023
And you can
HumNumWorld
HumNumWorld2/6/2023
because I learned from code.org
Angius
Angius2/6/2023
var plants = new List<string>();
plants.Add("carrot");
plants.Add("hydrangea");
plants.Add("juniper");
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy