Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLiam Andersson
1,396 PointsHaving trouble with public strings
Hey guys! Doing a small project here but I got into a problem.
I have a string called password stored in another class and I want to use it in another class. How do I do this?
Someone had said: public password = "password"; but I get 76 problems when I write this, it also starts a new method? (like public static void Main () )
If I am being unclear I can try to explain further!
using System; //It's a really simple code
namespace Project{
public static class Password {
public static void Main() {
Console.WriteLine("Please enter your new password");
string password = Console.ReadLine(); //I want to make this one public and then use it in the other class..
Console.WriteLine("Are you sure you want to change your password to: " + password + "? (Yes/No)");
string input = Console.ReadLine();
if(input.ToLower() == "yes")
{
Console.WriteLine("Your password has been changed to " + password + "!");
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
Console.Clear();
Username.Name ();
}
else if(input.ToLower() == "no")
{
Console.WriteLine(password + " will not be saved as your password");
Console.WriteLine("Press any key to restart");
Console.ReadKey();
Console.Clear();
Main();
//System.Environment.Exit(0);
}
else
{
Console.WriteLine("ERROR! Press any key to restart");
Console.ReadKey();
Console.Clear();
Main ();
}
}
}
public static class Username
{
public static void Name ()
{ //And that is here.
Console.WriteLine("Please enter a username you would like");
Console.ReadLine();
}
}
}
1 Answer
Steven Parker
231,248 PointsYour declaration is missing a type, I you could explicitly declare it string or use var and let the compiler do it.
If that's not the whole issue, you'll need to show more of the code.
Liam Andersson
1,396 PointsI'll do it, can't access workspace right now but I'll link pastebin If I can
EDIT: Added the pastebin link
Steven Parker
231,248 PointsBy "done" do you mean the issue is resolved? If not, I'm running a security filter called Zscaler that blocks your link. But perhaps someone else can take a look.
Liam Andersson
1,396 PointsDoes it block pastebin? wow
Steven Parker
231,248 PointsSteven Parker
231,248 PointsIt would help to see the complete code, do you have it in a Github repo? Or a workspace?