C
C#3d ago
Sammy

how to add inheritance between two c# files?

ive created tw c# files one called Pirate.cs and Gang.cs . I have John Adams in Gang.cs and i want pirate.cs to print it out but it doesnt work. could anyone help me find out the proper way to do inheritance in c# and how to get my code to work? Pirate.cs using System; namespace PirateApp { class Pirate : Gang { public string Name { get; set; } protected bool HasEyepatch { get; set; } private DateTime Birthday { get; set; } public Pirate(string name) { Name = name; } public void Swear(string[] swearWords) { foreach (var word in swearWords) { Console.WriteLine($"Arrr! {word}"); } } } class Program { static void Main(string[] args) { Console.WriteLine($"The name of the pirate is {Pirate.Name}"); } } } Gamg.cs namespace PirateApp { public class Gang { private string _territory = "Caribbean"; public string Name { get; set; } = "John Adams"; public int MemberCount { get; set; } = 30; public string Territory { get => _territory; protected set => _territory = value; } public Gang(string name, int memberCount, string territory) { Name = name; MemberCount = memberCount; _territory = territory; } public void UpdateTerritory(string newTerritory) { _territory = newTerritory; } } }
No description
29 Replies
wasabi
wasabi3d ago
What "doesn't work?"
Sammy
SammyOP3d ago
the data from gangs.cs i want it to be printed out in Pirate.cs but it doesnt work
Pobiega
Pobiega3d ago
class Pirate : Gang means a pirate IS a gang
Angius
Angius3d ago
Inheritance is between classes, not files Just so we're clear
ero
ero3d ago
you need to instantiate a Pirate for you to be able to print any information about a Pirate
Pobiega
Pobiega3d ago
also, your program will "start" in Main so the only thing it does atm is that console writeline your Gang and Pirate classes are just that - class declarations they dont actually do anything in your current code, as you dont use thoses classes
ero
ero3d ago
and Main will of course not compile
Sammy
SammyOP3d ago
what i want from the console is it to write 'The name of the pirate is John Adams' the probelm is im not sure how can i make Pirate.cs get the value name from gang.cs and print it out.
Pobiega
Pobiega3d ago
I mean, first, stop focusing on the files and filenames
Angius
Angius3d ago
Is the pirate a whole gang? Like, a gang of dwarves in a trench coat with a pirate hat?
Pobiega
Pobiega3d ago
"pirate.cs" is just a file, it doesnt do anything on its own. Just like your pirate class is just a class, it doesnt do anything on its own either so it cant "get a value"
Sammy
SammyOP3d ago
no its just a name
Pobiega
Pobiega3d ago
based on the UML you posted above, I think Pirate is supposed to be an individual pirate.
Sammy
SammyOP3d ago
just so you guys know im trying to implement this class uml from my school
No description
Pobiega
Pobiega3d ago
mhm
Sammy
SammyOP3d ago
oh okay
Pobiega
Pobiega3d ago
there is no inheritance specified in that UML and you have missed a few things, such as "is led by" and "is member of" etc remember, inheritance is for x IS A y relationships
Sammy
SammyOP3d ago
but doesnt the arrow mean in the uml that Pirate inherits from gang?
Pobiega
Pobiega3d ago
class Goat : Animal for example no it just shows a relationship in this case, "is led by" would probably be represented by a public Pirate Leader {get; set;} property makes sense?
Sammy
SammyOP3d ago
kind of so i would have to encapsulate the name in Pirate Leader and then send it to Pirate ?
Pobiega
Pobiega3d ago
? lets take a step back do you know how to create instances of a class?
Sammy
SammyOP3d ago
yes
Pobiega
Pobiega3d ago
Can you show me how?
Sammy
SammyOP3d ago
like this?Person person = new Person();
Pobiega
Pobiega3d ago
excellent so, how do we make a pirate? maybe one who will be the leader of a gang? also: $codegif
Sammy
SammyOP3d ago
ok thanks now i understand
Sammy
SammyOP3d ago
i just have one question how would i make this ?
No description
Pobiega
Pobiega3d ago
A pirate has an (optional) relationship to another Pirate

Did you find this page helpful?