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 trial

C#

Liam Andersson
Liam Andersson
1,396 Points

Having 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!

https://pastebin.com/2ATK8Yzg

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();
    }



}

}

Steven Parker
Steven Parker
231,110 Points

It would help to see the complete code, do you have it in a Github repo? Or a workspace?

1 Answer

Steven Parker
Steven Parker
231,110 Points

Your 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
Liam Andersson
1,396 Points

I'll do it, can't access workspace right now but I'll link pastebin If I can

EDIT: Added the pastebin link

Steven Parker
Steven Parker
231,110 Points

By "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
Liam Andersson
1,396 Points

Does it block pastebin? wow