Code Review: App Interfaces

Hi everyone! For the HeaderSymbol property, I added it to the interface and then in TodoList.cs with the following getter:

public string HeaderSymbol
    {
      get { return "--------"; }
    }

But I see @psmilliorn used the same => that is used to create lambda expressions for methods. I didn’t know you could use it for properties. How come this syntax also works here? I’m a code newbie and C# is my first venture into coding, so I’m sorry if this should be obvious.

I was also struggling with fixing the case where more than 5 items were added with the Add method, as I first tried if (Todos.Length <= 5) and it gave me an error when I added the sixth item. if (nextOpenIndex < 5) is the way to go, thanks for helping me figure this out @adamstackhouse203307!

1 Like

I also had an issue with ChangePassword() not working after having changed the set method of the Password property so that it requires the password to be at least eight characters in length.

I instead modified the PasswordManager():

public PasswordManager(string password, bool hidden)
    {
      if (password.Length >= 8)
      {
        Password = password;
        Hidden = hidden;
      }
      else
      {
        Console.WriteLine("Your password is too short. Type at least 8 characters");
      }
    }

And also the ChangePassword() method like this:

public bool ChangePassword(string oldPass, string newPass)
    {
      if (oldPass == Password && newPass.Length >= 8)
      {
        Password = newPass;
        Console.WriteLine("Your password was successfully changed.");
        return true;
      }
      else
      {
        Console.WriteLine("Please enter your old password and make sure your new password is at least 8 characters long.");
        return false;
      }
    }

Maybe it would be even better to use switch cases to display a different message depending if the old password doesn’t match, and/or the new password is too short.

I sweated a lot to try and solve the extra challenges of part 13 of this exercise, but I think I finally got everything working :smiley:

I think it is a refactor that Visual Studio Code suggested so I went with it.

1 Like

Came here just to point this out.

The hint that tells you to check Todos.Length is simply wrong - the length is always 5, the index is what you need to check :slight_smile:

See the docs here for more information on them:

Im having issues with this project as well.
Specifically this part

Add a method ChangePassword() to PasswordManager. It should:

have two string parameters
if the first argument matches the existing Password, reset the Password to the second argument
return true if the password change was a success (the first argument matched the old password), and false otherwise

In passwordmanager.cs my change password code is this:

public bool ChangePassword(string oldPass, string nuPass)
    {
      if(oldPass==nuPass){
         Password=nuPass;
      }
     return oldPass==nuPass;
    }

Im calling it with and trying to print the boolean result with in program.cs

      bool passSuccess=PasswordManager.ChangePassword("iluvpie","iluvpie");
      Console.WriteLine("Passsword change is: ", passSuccess);

I get the following error:

Program.cs(20,24): error CS0120: An object reference is required for the non-static field, method, or property 'PasswordManager.ChangePassword(string, string)' [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-app-interfaces/SavingInterface.csproj]

The build failed. Fix the build errors and run again.

Im at a loss on how to fix this. Can anybody help me? Thank you in advance.
Andy

There is not enough code here to fix your problem. However…

Program.cs(20,24): error CS0120: An object reference is required for the non-static field, method, or property ‘PasswordManager.ChangePassword(string, string)’ [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-app-interfaces/SavingInterface.csproj]

What this is saying is that

 bool passSuccess=PasswordManager.ChangePassword("iluvpie","iluvpie");

Is the problem. Why? You defined it without an object. It’s not static. What you did was if it was static. What it is wanting is for you to use the constructor from that class to construct an object, an then call it with that object. That’s the part that is missing in order to help you understand.

Now, if you want to use that method without an object then simply add static keyword to your method.

Hi
Thank you I’ll look at making the change.
Regards
Andrew

HI Ive came back to this after a couple of weeks (due to family issues). Ive tried to trace back what I had previously done. This is IDisplayable.

using System;

namespace SavingInterface
{
  interface IDisplayable{
   string HeaderSymbol {get;}
   void Display();
  }
}

Im getting the following errors
IDisplayable.cs(8,28): error CS8180:
Indicating there is a missing : or ; on the line with the get but as far as I can see there is ; after the get
IDisplayable.cs(8,28): error CS1513:
IDisplayable.cs(8,28): error CS1513:
As far as I can tell this means there is a missing { bracket but as far as I can see I have 3 pairs of {} brackets there isnt a missing one.
Im at a loss to why these errors are appearing.
Now instead of the above Im getting a stack overflow exception Im really confused by this.
Can anybody help?
Thanks in advance.
Andrew

Can you post the code where this is interface is implemented? I don’t see any difference between what’s here an what I did.

// Define IDisplayable in this file
//Every app must have a display feature. In IDisplayable.cs, define an empty IDisplayable interface.
//Within IDisplayable declare one method that: is named Display(), returns nothing.

namespace SavingInterface
{
    interface IDisplayable
    {
        //Add a get-only property to IDisplayable called HeaderSymbol.
        //This should be used in Display() as a header.
        string HeaderSymbol { get; }

        void Display();
    }
}

Hi Nick,

Extending the password manager to only accept passwords of length 8 or greater ends up affecting the reset method. What is the intended solution for this?

Below is my class:

namespace AppInterfaces
{
    class PasswordManager : IDisplayable
    {
        private string pw;

        private string Password
        { 
            get
            {
                return pw;
            }
            set
            {
                if(value.Length >= 8)
                {
                    pw = value;
                }
                else
                {
                    Console.WriteLine("Password must be at least 8 chars long. Setting password to default.");
                    pw = "password";
                }
            }
        }

        public bool Hidden
        { get; private set; }

        public char HeaderSymbol { get; }

        public PasswordManager(string password, bool hidden)
        {
            Password = password;
            Hidden = hidden;
            HeaderSymbol = '-';
        }

        public void Display()
        {
            Console.WriteLine("Password:");
            Console.WriteLine(new String(HeaderSymbol, 12));
            if (Hidden == false)
            {
                Console.WriteLine(Password);
            }
            else
            {
                Console.WriteLine(new String('*', Password.Length));
            }
        }

        public void Reset()
        {
            Password = String.Empty;
            Hidden = false;
        }

        public bool ChangePassword(string password, string newpassword)
        {
            if (password == Password)
            {
                Password = newpassword;
                return true;
            }
                return false;
        }
    }
}

I see the video has not been added yet as well. Will it be added?

Thanks in advance!

What also would be really helpful is having the ability to see the entire final working code.

Try this:

//The lenght of the Todos array property is defined inside a constructor by creating an instance of it…

public void Add(string todo)

if (nextOpenIndex < Todos.Length)
{

Todos[nextOpenIndex] = todo;
nextOpenIndex++;

}

i agree with you…i really would love to see the complete working code

Could someone post the whole working code for each of the scripts here? Because this exercise’s extra tasks are a pure mess and, since it still hasn’t received a video solution, a working one from someone that has done it - would be a blessing.

Thank you!

@okeyrollas101 @sulh3x it would serve you best to ask targeted questions on what you are stuck on instead of asking for the code. However, this is a gist of my code. No warranty if it is correct according to what CC was asking of us to do.

Just want to say thanks for this. It’s 10/2022 and CC still doesn’t have a solution or a walkthrough. I’d be completely lost if I wasn’t able to use this as reference (despite having gone through the lesson a few times now).

2 Likes

Now its March 23 and still no video walkthrough.
Same as gohanburner - i would be a little lost without this whole post

1 Like

Any luck on this walk through video?

1 Like