Jump to content

multipurpose C# function

Guest

im new to this section of the forum so sorry if this post is weird

So i have this line of code here:

else if (account == "origin")
{
foreach (var variable in origin)
{
Console.WriteLine(variable);
}
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
Console.Clear();
}

what I want to do is take the content of the string "account" and have it be used in the foreach section of the code, so it'd look something like this

else if (account == "origin")
{
foreach (var variable in (content of var account in string form))
{
Console.WriteLine(variable);
}
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
Console.Clear();
}

I want this function to be multipurpose, so I can enter anything into account and it will list the contents of the list. How would i do this?

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, NovaMan01 said:

		

		else if (account == "origin")
		{
		foreach (var variable in origin)
		{
		Console.WriteLine(variable);
		}
		Console.WriteLine("Press Enter to exit");
		Console.ReadLine();
		Console.Clear();
		}		

		what I want to do is take the content of the string "account" and have it be used in the foreach section of the code, so it'd look something like this		

		else if (account == "origin")
		{
		foreach (var variable in (content of var account in string form))
		{
		Console.WriteLine(variable);
		}
		Console.WriteLine("Press Enter to exit");
		Console.ReadLine();
		Console.Clear();
		}

 

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, Lichig0 said:

 

How very helpful.

 

If you want the function to be reusable, remove the checking for "origin". Also, don't use == for comparing strings, use

String1.Equals(String2);

 

Just read the input of the user, store it in a string and loop over that string. But instead of what you have there, try

foreach(char c in String s) {
	//todo
}

 

Ryzen 5 1600 @ 3.9 Ghz  | Gigabyte AB350M Gaming 3 |  PaliT GTX 1050Ti  |  8gb Kingston HyperX Fury @ 2933 Mhz  |  Corsair CX550m  |  1 TB WD Blue HDD


Inside some old case I found lying around.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 9/30/2018 at 12:39 PM, NovaMan01 said:

what I want to do is take the content of the string "account" and have it be used in the foreach section of the code, so it'd look something like this


else if (account == "origin")
{
    foreach (var variable in (content of var account in string form))
	{
		Console.WriteLine(variable);
	}
	Console.WriteLine("Press Enter to exit");
	Console.ReadLine();
	Console.Clear();
}

I want this function to be multipurpose, so I can enter anything into account and it will list the contents of the list. How would i do this?

The block of code you used as an example confuses me. The foreach loop implies that account is an object, its contents turned into a string, and you want to print the string itself.

 

Or rather, taking this in whole what I'm seeing is account is a string, and you're printing out the contents of an object named account. Which is a problem. Unless you mean to say in account == "origin" you mean some identifier member in an account object has the value "origin"

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×