The Object of your Affection

I am trying to print out the name that is giving to sam but the program says:

Program.cs(10,11): error CS0122: 'Profile.name' is inaccessible due to its protection level [/home/ccuser/
workspace/csharp-dating-profile/DatingProfile.csproj]

This is my code if you want to know what I should do to it:

using System;

namespace DatingProfile
{
  class Program
  {
    static void Main(string[] args)
    {
      Profile sam = new Profile();
      sam.name = "Sam Drakkila";
    }
  }
}

The build failed. Please fix the build errors and run
again.

Just keep going. That error message is part of the project. You can’t assign a name that way because the .name property is private. The next couple of steps in the project show you how to assign values to a new profile object by using a constructor.

1 Like

I have got another error:

/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error MSB3883: Unexpected
exception:  [/home/ccuser/workspace/csharp-dating-
profile/DatingProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : System.ComponentMo
del.Win32Exception (11): Resource temporarily unav
ailable [/home/ccuser/workspace/csharp-dating-prof
ile/DatingProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at Interop.Sys.For
kAndExecProcess(String filename, String[] argv, St
ring[] envp, String cwd, Boolean redirectStdin, Bo
olean redirectStdout, Boolean redirectStderr, Bool
ean setUser, UInt32 userId, UInt32 groupId, Int32&
 lpChildPid, Int32& stdinFd, Int32& stdoutFd, Int3
2& stderrFd, Boolean shouldThrow) [/home/ccuser/wo
rkspace/csharp-dating-profile/DatingProfile.csproj
]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at System.Diagnost
ics.Process.StartCore(ProcessStartInfo startInfo)
[/home/ccuser/workspace/csharp-dating-profile/Dati
ngProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at System.Diagnost
ics.Process.Start() [/home/ccuser/workspace/csharp
-dating-profile/DatingProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at Microsoft.Build
.Utilities.ToolTask.ExecuteTool(String pathToTool,
 String responseFileCommands, String commandLineCo
mmands) [/home/ccuser/workspace/csharp-dating-prof
ile/DatingProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at Microsoft.CodeA
nalysis.BuildTasks.ManagedCompiler.HandleResponse(
BuildResponse response, String pathToTool, String
responseFileCommands, String commandLineCommands)
[/home/ccuser/workspace/csharp-dating-profile/Dati
ngProfile.csproj]
/usr/share/dotnet/sdk/2.2.300/Roslyn/Microsoft.CSh
arp.Core.targets(59,5): error : at Microsoft.CodeA
nalysis.BuildTasks.ManagedCompiler.ExecuteTool(Str
ing pathToTool, String responseFileCommands, Strin
g commandLineCommands) [/home/ccuser/workspace/csh
arp-dating-profile/DatingProfile.csproj]

The build failed. Please fix the build errors and
run again.

Explain what this means or if it means anything.

That error is beyond me. Did you change your code somehow? If not I’d refresh your browser. The error seems to be unrelated to your actual code, but I could be wrong.

1 Like

This is my true error:

Profile.cs(16,5): error CS1519: Invalid token '{' in class, struct, or interface member declaration [/home/ccuser/workspace/csharp-dating-pro
file/DatingProfile.csproj]
Profile.cs(16,16): error CS1519: Invalid token '=' in class, struct, or interface member declaration [/home/ccuser/workspace/csharp-dating-pr
ofile/DatingProfile.csproj]
Profile.cs(16,21): error CS1519: Invalid token ';' in class, struct, or interface member declaration [/home/ccuser/workspace/csharp-dating-pr
ofile/DatingProfile.csproj]
Profile.cs(17,5): error CS1022: Type or namespace definition, or end-of-file expected [/home/ccuser/workspace/csharp-dating-profile/DatingPro
file.csproj]
Profile.cs(17,7): error CS1031: Type expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,11): error CS1003: Syntax error, '[' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,11): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,17): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,23): error CS1003: Syntax error, ',' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,25): error CS1003: Syntax error, ']' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(17,25): error CS1514: { expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,5): error CS1022: Type or namespace definition, or end-of-file expected [/home/ccuser/workspace/csharp-dating-profile/DatingPro
file.csproj]
Profile.cs(18,7): error CS1031: Type expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,11): error CS1003: Syntax error, '[' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,11): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,20): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,29): error CS1003: Syntax error, ',' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,31): error CS1003: Syntax error, ']' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(18,31): error CS1514: { expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,5): error CS1022: Type or namespace definition, or end-of-file expected [/home/ccuser/workspace/csharp-dating-profile/DatingPro
file.csproj]
Profile.cs(19,7): error CS1031: Type expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,11): error CS1003: Syntax error, '[' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,11): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,21): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,31): error CS1003: Syntax error, ',' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,33): error CS1003: Syntax error, ']' expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(19,33): error CS1514: { expected [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]
Profile.cs(21,1): error CS1022: Type or namespace definition, or end-of-file expected [/home/ccuser/workspace/csharp-dating-profile/DatingPro
file.csproj]

The build failed. Please fix the build errors and run again.

And here is my code again:

using System;

namespace DatingProfile
{ 
  class Profile 
  {
    private string name;
    private int age;
    private string city;
    private string country;
    private string pronouns;
    private string[] hobbies;
    
    public Profile(string name, int age, string city, string country, string pronouns = "they/them")
    { this.name = name; } 
    { this.age = age; } 
    { this.city = city; } 
    { this.country = country; }
    { this.pronouns = pronouns; }
  }
}

Ah. Ok. You just need to remove the { }'s around your property assignments inside the constructor. The constructor is a block, but each property is just a statement inside the block:

public Constructor( string prop1, int prop2) //This is just an example. Don't use the name Constructor.
{
  this.prop1 = prop1;
  this.prop2 = prop2;
}
1 Like

I’ve got another error:

Program.cs(9,25): error CS7036: There is no argument give
n that corresponds to the required formal parameter 'name
' of 'Profile.Profile(string, int, string, string, string
)' [/home/ccuser/workspace/csharp-dating-profile/DatingPr
ofile.csproj]
Program.cs(10,11): error CS0122: 'Profile.name' is inacce
ssible due to its protection level [/home/ccuser/workspac
e/csharp-dating-profile/DatingProfile.csproj]
Program.cs(11,11): error CS0122: 'Profile.age' is inacces
sible due to its protection level [/home/ccuser/workspace
/csharp-dating-profile/DatingProfile.csproj]
Program.cs(12,11): error CS0122: 'Profile.city' is inacce
ssible due to its protection level [/home/ccuser/workspac
e/csharp-dating-profile/DatingProfile.csproj]
Program.cs(13,11): error CS0122: 'Profile.country' is ina
ccessible due to its protection level [/home/ccuser/works
pace/csharp-dating-profile/DatingProfile.csproj]
Program.cs(14,11): error CS0122: 'Profile.pronouns' is in
accessible due to its protection level [/home/ccuser/work
space/csharp-dating-profile/DatingProfile.csproj]

The build failed. Please fix the build errors and run aga
in.

And this is the code:

using System;

namespace DatingProfile
{
  class Program
  {
    static void Main(string[] args)
    {
      Profile sam = new Profile();
      sam.name = "Sam Drakkila";
      sam.age = 30;
      sam.city = "New York";
      sam.country = "USA";
      sam.pronouns = "he/him";
    }
  }
}

Since you now have a constructor in place, you pass arguments to the constructor when you instantiate a new Profile object. Trying to assign properties like this: sam.name = "Sam Drakkila"; still doesn’t work for the same reason it didn’t work earlier. Those properties of the object are private. The constructor method is public though, so we use it to create new instances of the object with its property values passed in as arguments like so:

Profile sam = new Profile("Sam Drakkila", 30, "New York", "USA", "he/him");

The order of the arguments needs to match the order that the parameters appear in the constructor declaration: public Profile(string name, int age, string city, string country, string pronouns = "they/them")

1 Like

So nothing get printed right.

That’s right. At this point nothing should appear in the console. You’ll have to complete the next step to code the ViewProfile() method. Then you’ll be able to print sam's profile to the console.

1 Like

I have a new error:

Profile.cs(17,12): error CS1520: Method must have a return type [/home/ccuser/workspace/csharp-dating-profile/DatingProfile.csproj]

The build failed. Please fix the build errors and run again.

This is my code again:

using System;

namespace DatingProfile
{ 
  class Profile 
  {
    private string name;
    private int age;
    private string city;
    private string country;
    private string pronouns;
    private string[] hobbies;
    
    public Profile(string name, int age, string city, string country, string pronouns = "they/them")
    { this.name = name; this.age = age; this.city = city; this.country = country; this.pronouns = pronouns; }
    
    public ViewProfile()
    {
      string bio = $"Name: {name},\nAge: {age},\nCity: {city},\nCountry: {country}";
      return bio;
    }
  } 
}

You have an error message. Did you read it? Then what?
If you identify what stops you then that gives you something to find out more about.
It seems like there are still things that you can do yourself, at least from what you’ve said about it.
It won’t be useful to you to have other people tell you the conclusion of what to do to fix something. The thing to learn is how to reach the conclusion of what to do, isn’t it? So you have to go through all the steps that somebody else would, even if it takes time, especially if it takes time.

1 Like