• 0

Upcasting and Downcasting an Array


Question

Here there are 2 classes Employee(base class) and Manager(sub class) Manager inherits from Employee.I have added 2 Interfaces IName and ISalary so that i can downcast.

 

interface IName
{
    string Name { get; }
    void MyName();
}

 interface ISalary
{
    string Salary { get; }
    void MySalary();
}

class Employee : IName
{
  public Employee (string name)
    {
        this.name = name;
    }

    private string name;


    public string Name
    {
        get { return " My name is " + name; }
    }

    public void MyName()
    {
        MessageBox.Show(this.Name);
    }
}

class Manager : Employee, ISalary
{
    public Manager(string name, int salary) : base(name)
    {
        this.salary = salary;
    }

    private int salary;
    private int name;

    public string Salary
    {
        get { return name + " Salary : " + salary + " per month"; }
    }
    public void MySalary()
    {
        MessageBox.Show(this.Salary);
    }

}

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Manager[] manager = new Manager[2];
        manager[0] = new Manager("Joe", 400);
        manager[1] = new Manager("John", 500);

        Employee[] employee = manager;
        ISalary someemployee = employee as Manager;
        someemployee.MySalary();

    }
}

Here are the errors that i'm getting

 

Cannot convert type 'WindowsFormsApplication26.Employee[]' to 'WindowsFormsApplication26.Manager' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

 

When downcasting the conversion returns to null.

 

Also Under manager class i have to mention the line  

private string name ;

despite using the base keyword in the constructor above it. Otherwise the name parameter is not recognized by the class.

Link to comment
https://www.neowin.net/forum/topic/1322268-upcasting-and-downcasting-an-array/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
Employee[] employee = manager;
ISalary someemployee = employee as Manager;

employee is an array. someemployee is not an array. You're trying to assign a collection of values to a single value, which makes no sense. Maybe you meant:

 

Employee[] employee = manager;
ISalary someemployee = employee[0]; // get the first employee in the array

And that doesn't make sense either, because Employee doesn't implement ISalary. The compiler won't let you do that, and trying to work around the compiler error with a cast will just turn the compile-time problem into a run-time problem. You could do:

 

Employee[] employee = manager;
ISalary someemployee = employee[0] as Manager; // get the first employee in the array

And incidentally, in this case, it won't fail at run-time, because you've actually only put Manager objects in that Employee array, and Manager implements ISalary. But this is terrible. If you ever put an actual Employee in that Employee array, the code will fail.

 

In general, casting arrays is bad, don't do it. It's in C# because C# copied Java without thinking on that point.

 

It's unclear what you're trying to do and why you need casts at all (or even arrays or even any class besides the form here). But at least this is explanation of what's wrong with your code.

 

  Quote

Also Under manager class i have to mention the line  

private string name ;

despite using the base keyword in the constructor above it. Otherwise the name parameter is not recognized by the class.

Expand  

Because it's private in the base class. "private" means that it's only visible to that class and nothing else, including derived classes. If you want it to be visible in derived classes you need it to be "protected".

 

Realize that adding private string name ; in the Manager class means the Manager class now has two fields called name: one only visible in the base class and one only visible in the derived class, each with its own value. This is very confusing and definitely not what you want.

  • 0
  On 13/02/2017 at 20:41, Andre S. said:
Employee[] employee = manager;
ISalary someemployee = employee as Manager;

employee is an array. someemployee is not an array. You're trying to assign a collection of values to a single value, which makes no sense. Maybe you meant:

 

Employee[] employee = manager;
ISalary someemployee = employee[0]; // get the first employee in the array

And that doesn't make sense either, because Employee doesn't implement ISalary. The compiler won't let you do that, and trying to work around the compiler error with a cast will just turn the compile-time problem into a run-time problem. You could do:

 

Employee[] employee = manager;
ISalary someemployee = employee[0] as Manager; // get the first employee in the array

And incidentally, in this case, it won't fail at run-time, because you've actually only put Manager objects in that Employee array, and Manager implements ISalary. But this is terrible. If you ever put an actual Employee in that Employee array, the code will fail.

 

In general, casting arrays is bad, don't do it. It's in C# because C# copied Java without thinking on that point.

 

It's unclear what you're trying to do and why you need casts at all (or even arrays or even any class besides the form here). But at least this is explanation of what's wrong with your code.

 

Because it's private in the base class. "private" means that it's only visible to that class and nothing else, including derived classes. If you want it to be visible in derived classes you need it to be "protected".

 

Realize that adding private string name ; in the Manager class means the Manager class now has two fields called name: one only visible in the base class and one only visible in the derived class, each with its own value. This is very confusing and definitely not what you want.

Expand  

Ok i changed the code as you mentioned and i was able to get the output, So if i want the variables of employee[1] to be displayed do i need to create another instance of  ISalary like

 

ISalary someotheremployees = employee[1] as Manager;

someotheremployee.MySalary(); 

 

or is there another way of representing employee[0] and employee[1]  in a single line so that the output message box displays  both of their values one after the other

 

  • 0
  On 14/02/2017 at 13:14, Ch33f said:

Ok i changed the code as you mentioned and i was able to get the output, So if i want the variables of employee[1] to be displayed do i need to create another instance of  ISalary like

 

ISalary someotheremployees = employee[1] as Manager;

someotheremployee.MySalary(); 

 

or is there another way of representing employee[0] and employee[1]  in a single line so that the output message box displays  both of their values one after the other

Expand  

The basic problem is that you have 2 Managers you want to display, but for some reason, you insist on storing them inside an Employee array, only to be forced to cast them back to Managers (a perilous operation, as I mentioned) before displaying them.

 

This begs the question: why store them in an Employee array at all? Why not just use your original Manager array? It's unclear why you're doing things this way.

 

By the way you are not "creating another instance of ISalary" with the code you showed. You are creating another variable pointing to the same instance.

 

If you want to display the values of the array one after the other, just use a loop to go through the array (ex.: foreach).

This topic is now closed to further replies.
  • Posts

    • BurnAware 18.7 by Razvan Serea Free burning software to create CDs, DVDs, and Blu-ray discs of all types. BurnAware is a full-fledged, easy-to-use burning software which allows users to write all types of files such as digital photos, archives, documents, music and videos to CDs, DVDs and Blu-ray Discs, including BDXL and M-Disc. With BurnAware, you also be able to create boot or multisession discs, high-quality Audio CDs and Video DVDs, make and burn ISO images, copy and backup discs, extract audio tracks, verify and recover data from multisession or unreadable discs. BurnAware is available in three editions - Free, Premium and Professional. Compare and pick edition which is suitable for you. Features Burn files and folders to CD, DVD or Blu-ray Discs. Append or update multisession discs. Burn standard or boot disc images. Burn ISO files to multiple recorders simultaneously. Create boot CDs or DVDs. Create Audio CDs. Create DVD-Video discs. Create MP3 CD / DVD / Blu-ray Discs. Make standard or boot disc images. Copy CD, DVD or Blu-ray Discs to ISO images. Copy from discs to discs. Verify discs byte by byte. Recover files from damaged discs or different sessions. Extract audio tracks from Audio CDs. Erase and format re-writable discs. View detailed disc and drive information. Supports All media types (CD/DVD/Blu-ray Disc) including Double Layer All current hardware interfaces (IDE/SCSI/USB/1394/SATA) including AHCI UDF/ISO9660/Joliet file systems (any combination) On-the-fly writing (no staging to hard drive first) Verification of written files Multisession DVD-RW/DVD+RW Unicode CD-Text (tracks and disc) Compatible with Windows Vista 7, 8, 10, 11 (32-bit or 64-bit) BurnAware 18.7 changelog: Updated translations. Updated disc burning SDK. Improved indirect disc copying (CD <-> DVD). Optimized overall performance on 64-bit systems. Download: BurnAware Free 18.7 | 11.8 MB (Freeware) View: BurnAware Free Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, as a general rule I prefer standard XBox-like controllers that work like usual out-of-the-box. additional software tends to be overkill. p.s. even my 8BitDo Ultimate 2C wired, while I had to tweak something (setup a udev rule) on Linux for it to work 'out-of-the-box' (which is required for Xinput mode to work in kernels prior to 6.12), I can slightly tweak a small amount of additional buttons/features on the controller itself, no software needed.
    • Are you saying that you don’t think they will be adding the “.” versioning? They definitely will.
    • What will you find out about me?? Are you not going to answer this question as well? Empty threats for just asking questions😂
    • My Alexa Show is just a glorified Alarm clock and weather reporter.
  • Recent Achievements

    • Apprentice
      Adrian Williams went up a rank
      Apprentice
    • Reacting Well
      BashOrgRu earned a badge
      Reacting Well
    • Collaborator
      CHUNWEI earned a badge
      Collaborator
    • Apprentice
      Cole Multipass went up a rank
      Apprentice
    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
  • Popular Contributors

    1. 1
      +primortal
      535
    2. 2
      ATLien_0
      266
    3. 3
      +Edouard
      193
    4. 4
      +FloatingFatMan
      184
    5. 5
      snowy owl
      135
  • Tell a friend

    Love Neowin? Tell a friend!