Can Derived class hide the accessors - Whats wrong with code below - c#

I am trying to hide the accessors in derived class, is it valid? My system did not respond back on executing.
class BaseCS
{
private string name;
public string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public new string Name
{
set { Name = "Der " + value; }
get { return Name; }
}
}
public static void Main()
{
BaseCS one = new DerivedCS();
one.Name = "One";
Console.WriteLine("Name of object one is {0} ", one.Name);
((BaseCS)one).Name = "On1";
Console.WriteLine("Name of object one is {0} ", one.Name);
}
Shouldn't I expect output to be,
Name of object one is Base Der One
Name of object one is Base On1

This code:
public new string Name
{
set { Name = "Der " + value; }
get { return Name; }
}
Will lead to stack overflow, because Name in getters and setters will refer to Name in DerivedCS, not in BaseCS. Name property will call itself forever, until crashing. You need to use base.Name.
What you probably want is polymorphysm. You should make property virtual and override setter in the derived class.
If you do not use virtual properties, the following object will not behave the way you want:
BaseCS one = new DerivedCS();
one.Name = "name"; // base implementation is called
Changing property value in setter is usually a bad idea. Users expect the following contract to be followed:
var a = new A();
a.Foo = "bar";
Debig.Assert(a.Foo == "bar");

Here is what you should be doing
class BaseCS
{
private string name;
public virtual string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public override string Name
{
set { base.Name = "Der " + value; }
get { return base.Name; }
}
}
OR with a new keyword
class BaseCS
{
private string name;
public string Name
{
get { return name; }
set { name = "Base " + value; }
}
}
class DerivedCS : BaseCS
{
public new string Name
{
set { base.Name = "Der " + value; }
get { return base.Name; }
}
}
now you should create the object as the derived type to get your expected result
DerivedCS one = new DerivedCS();

Related

C# add items to list from class then add list items to listbox

I'm a student in a C# class and this is my introductory assignment to Classes, so please bear with me. When the New button is pressed, a CPerson object will be created using the name and phone values and the object will be added to a List<>.
class CPerson
{
private string m_sName;
private string m_sPhone;
public string Name
{
get { return this.m_sName; }
set
{
this.m_sName = value;
}
}
public string Phone
{
get { return this.m_sPhone; }
set
{
this.m_sPhone = value;
}
}
}
public partial class Form1 : Form
{
private List<CPerson> PhoneNum = new List<CPerson>(); //<CPerson> or <string>?
public Form1()
{
InitializeComponent();
newbutton.Enabled = false;
changebutton.Enabled = false;
savebutton.Enabled = false;
}
private void newbutton_Click(object sender, EventArgs e)
{
changebutton.Enabled = true;
savebutton.Enabled = true;
PhoneNum.Add(new CPerson { Name = Namebox.Text + " : ", Phone = phonebox.Text });
listBox1.Items.Add(PhoneNum); //text = "Collection"
}
The assignment says "The CPerson ToString() override will be used to display the name and phone number in the listbox" as shown in the above image, which I don't necessarily understand, but I'm guessing I have to use something like this?
CPerson data = new CPerson();
data.ToString();
Either way, as the code is now, all I get in my listbox is "(Collection)". Any help would be appreciated!
That is asking to override the ToString() method. You can do it like this:
class CPerson
{
private string m_sName;
private string m_sPhone;
public string Name
{
get { return this.m_sName; }
set
{
this.m_sName = value;
}
}
public string Phone
{
get { return this.m_sPhone; }
set
{
this.m_sPhone = value;
}
}
public override string ToString()
{
return Name + ": " + Phone;
}
I did not get right the part of adding to the list, but I assume you can do the following using ToString():
listBox1.Items.Add(data.ToString());
Close...
class CPerson
{
private string m_sName;
private string m_sPhone;
public string Name
{
get { return this.m_sName; }
set
{
this.m_sName = value;
}
}
public string Phone
{
get { return this.m_sPhone; }
set
{
this.m_sPhone = value;
}
}
public override string ToString()
{
return Name + ": " + Phone;
}
}
}

C# Using Custom class in a List

I have a class and I am using it inside a LIST
List<user> listWithCustomClass = List<user>();
myClass.cs
public class user
{
public user(string fullname, string city, string state, int age, int type)
{
name = fullname;
citi = city;
estate = state;
tipe = type;
}
private string name = string.Empty;
private string citi = string.Empty;
private string estate = string.Empty;
private int tipe = 0;
public string getFullname
{
get { return name; }
set { name = value;}
}
public string getCity
{
get { return citi; }
set { citi = value;}
}
public string getState
{
get { return state; }
set { state = value;}
}
public int getType
{
get { return type; }
set { type = value;}
}
}
How can I add a custom toString() without having to override generic toString(). I would like to add something like showDate().
For example, in a combobox I would like the output of the inserted information to be:
--> Hello, your name is {name} and your age is {age}
Like this:
foreach(var item in user)
{
user.ShowData();
}
Add this in your class:
public string ShowData()
{
return "Hello, your name is " + name + " and your age is " + age.ToString();
}
but you must also define age first. Which, following your style, would be:
private int age = 0;
and then in the constructor add:
this.age = age;
EDIT
foreach(var item in listWithCustomClass)
{
item.ShowData();
}

Why isn't my property doing the check?

My constructor get info from the form. The 'name' is addressed with 'aName' (from the constructor), then there has to be a check done with prop 'NameCheck'. but if I compile it, it return an empty string. Any ideas?
Code:
---- Class ----
//Fields
private List<Create> Characters;
private string name;
private int health;
private int mSpeed;
private Role role;
private Speciality speciality;
//Properties
public string NameCheck
{
get {
return name;
}
set
{
if (string.IsNullOrEmpty(name))
{
name = "Name not specified";
}
else
{
name = value;
}
}
}
//Constructor
public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality)
{
this.name = aName;
this.health = aHealth;
this.mSpeed = aMSpeed;
this.role = aRole;
this.speciality = aSpeciality;
Characters = new List<Create>();
}
---- Form ----
Create character = new Create(tbName.Text, health, mspeed, aLane, aSpecial);
Characters.Add(character);
cbSummary.Items.Add(character.ToString());
PS: cbSummary is a combobox.
EDIT:
public override string ToString()
{
return "Name: " + NameCheck + " - Health: " + health + " - MovementSpeed: " + mSpeed + " - Role: " + role + " - Speciality: " + speciality;
}
You should set this.NameCheck instead of this.name in your constructor
public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality)
{
this.NameCheck = aName;
also you should check value for emptiness or being null instead of name in your property setter
set
{
if (string.IsNullOrEmpty(value))
{
You have a small typo in your property:
public string NameCheck
{
get {
return name;
}
set
{
// You need to check value, not name.
if (string.IsNullOrEmpty(value))
{
name = "Name not specified";
}
else
{
name = value;
}
}
}
In your constructor you should use your NameCheck property rather than the name field:
public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality)
{
this.NameCheck = aName;
this.health = aHealth;
this.mSpeed = aMSpeed;
this.role = aRole;
this.speciality = aSpeciality;
Characters = new List<Create>();
}
Also as Paddy said you are doing an incorrect check in the set body of NameCheck:
public string NameCheck
{
get
{
return name;
}
set
{
if (string.IsNullOrEmpty(value))
{
name = "Name not specified";
}
else
{
name = value;
}
}
}

Inconsistent accesibility: property type is less accesible than property [duplicate]

Please can someone help with the following error:
Inconsistent accessibility: property type 'Test.Delivery' is less accessible than property 'Test.Form1.thelivery'
private Delivery thedelivery;
public Delivery thedelivery
{
get { return thedelivery; }
set { thedelivery = value; }
}
I'm not able to run the program due to the error message of inconsistency.
Here is my delivery class:
namespace Test
{
class Delivery
{
private string name;
private string address;
private DateTime arrivalTime;
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public DateTime ArrivlaTime
{
get { return arrivalTime; }
set { arrivalTime = value; }
}
public string ToString()
{
{ return name + address + arrivalTime.ToString(); }
}
}
}
make your class public access modifier,
just add public keyword infront of your class name
namespace Test
{
public class Delivery
{
private string name;
private string address;
private DateTime arrivalTime;
public string Name
{
get { return name; }
set { name = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public DateTime ArrivlaTime
{
get { return arrivalTime; }
set { arrivalTime = value; }
}
public string ToString()
{
{ return name + address + arrivalTime.ToString(); }
}
}
}
Your class Delivery has no access modifier, which means it defaults to internal. If you then try to expose a property of that type as public, it won't work. Your type (class) needs to have the same, or higher access as your property.
More about access modifiers: http://msdn.microsoft.com/en-us/library/ms173121.aspx
Your Delivery class is internal (the default visibility for classes), however the property (and presumably the containing class) are public, so the property is more accessible than the Delivery class. You need to either make Delivery public, or restrict the visibility of the thelivery property.

how to add properties to class without changing return type in c#

I have a class which i can read but not write because of the company policy. I have following structures in my project.
public class Name // can not touch this class OR modify it
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetNames()
{
return FirstName + " " + LastName;
}
}
public class Details
{
// some methods and properties.
public Name LoadName() // This method return type i can not change as this method is used in ObjectDataSource for GridView
{
var names = new Name();
if (txtInpput.Text == "Jermy")
{
names.FirstName = "Jermy";
names.LastName = "Thompson";
}
else
{
names.FirstName = "Neville";
names.LastName = "Vyland";
}
return
names;
}
}
No I want to add extra property in class Name called "Email Address" and use class Details LoadName() method to return the type which includes the **Email Address** as well. as i am bound to use that method i can not change the return type Name to something else.
I can extend the class Name and overrides the GetNames() method to include the newly created property but that wont help as i can not change the return type in class Details LoadName() method.
I am not sure this is even possible BUT just wondering if there are any solutions.
I am suing
.NET 3
VS 2010
If you cannot change the signature of the method, your callers would need to do some casting. This is not optimal, but you can still do it:
public class NameWithEmail : Name {
public string EMail {get;set;}
}
...
public Name LoadName() {
...
return new NameWithEmail(); // OK because NameWithEmail extends Name
}
Now the caller would need to know the new type, do a cast, and access the email through it:
NameWithEmail name = Details.LoadName() as NameWithEmail;
if (name != null) {
Console.WriteLine("{0} {1} : {2}", name.FirstName, name.LastName, name.EMail);
}
The trickiest part is binding the new property to a data grid. This answer explains how this can be done.
Try this:
public class Name // can not touch this class OR modify it
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string GetNames()
{
return FirstName + " " + LastName;
}
}
public class Name1:Name {
public string EmailAddress { get; set; }
public override string GetNames()
{
return FirstName + " " + LastName+" "+EmailAddress;
}
}
public class Details
{
// some methods and properties.
public Name LoadName() // This method return type i can not change as this method is used in ObjectDataSource for GridView
{
TextBox txtInpput = new TextBox();
var names = new Name();
if (txtInpput.Text == "Jermy")
{
names.FirstName = "Jermy";
names.LastName = "Thompson";
}
else
{
names.FirstName = "Neville";
names.LastName = "Vyland";
}
return
names;
}
}
public class Details1:Details {
public override Name LoadName()
{
TextBox txtInpput = new TextBox();
var names = new Name();
if (txtInpput.Text == "Jermy")
{
names.FirstName = "Jermy";
names.LastName = "Thompson";
}
else
{
names.FirstName = "Neville";
names.LastName = "Vyland";
}
return
names;
}
}

Categories

Resources