Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4091852
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:25:07+00:00 2026-05-20T19:25:07+00:00

I am having trouble trying to reference the properties of an object that I

  • 0

I am having trouble trying to reference the properties of an object that I have inserted into an array using the following method.

    public static void AddEmployees()
    {
        string empID;
        decimal empWage;
        int count = 0;
        do
        {
            Console.Write("Please enter the employee ID number: ");
            empID = Convert.ToString(Console.ReadLine());

            Console.Write("Please enter the employee wage: ");
            empWage = Convert.ToDecimal(Console.ReadLine());

            var employeeObj = CreateEmployee(empID, empWage);
            employeeArray[count] = employeeObj;
            ++count;

        } while (count < 6);

    }

I would like to print out the information in this array in some sort of readable format, but I don’t know how to reference empWage or empID. Ideally I’d like to use some sort of foreach loop like follows:

    public static void DisplayEmployees()
    {
        foreach (var obj in employeeArray)
        {
            Console.WriteLine("Employee ID: {0}", empID);
            Console.WriteLine("Employee Wage: {0}", empWage);
            Console.WriteLine();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:25:08+00:00Added an answer on May 20, 2026 at 7:25 pm

    The quickest answer to your question is that you can cast the object in the array.

    ((Employee)obj).empID
    

    You haven’t shown how you defined the array; if it is an array of Employee then you can safely have the cast occur in the foreach but since you are getting errors I am guessing that your array might be of object or something higher in the inheritance hierarchy.

    If that’s the case, here is syntactically what I’m referring to:

    foreach (var obj in employeeArray)
    {
        Console.WriteLine("Employee ID: {0}", ((Employee)obj).empID);
        Console.WriteLine("Employee Wage: {0}", ((Employee)obj).empWage);
        Console.WriteLine();
    }
    

    Although casting will work for many cases, this kind of code is brittle since it relies on a bunch of assumptions, the biggest of which is that everything in your array is of type Employee. Rather than make the assumption, you can just have the compiler enforce this for you by using a generic list rather than a plain array. To do that your definition of the collection would be:

    List<Employee> employeeArray = new List<Employee>();
    

    One final technique you can use: C# has two operators used for casting that are quite robust: this is and as operators. The is operator will allow you to conditionally check whether something is a certain type and the as operator let’s you attempt a cast but get a null value if the type doesn’t match. Here are variations of your loop with both operators to give you a feel for how things might work:

    Below I use is to verify type before accessing properties:

    foreach (var obj in employeeArray)
    {
        if (obj is Employee)
        {
            Console.WriteLine("Employee ID: {0}", obj.empID);
            Console.WriteLine("Employee Wage: {0}", obj.empWage);
            Console.WriteLine();
        }
    }
    

    In the below I use as to attempt the cast, but I check if it’s null before attempting to access properties.

    foreach (var obj in employeeArray)
    {
        var emp = obj as Employee;
        if (emp != null )
        {
            Console.WriteLine("Employee ID: {0}", obj.empID);
            Console.WriteLine("Employee Wage: {0}", obj.empWage);
            Console.WriteLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently having trouble trying to understand linked lists. I have some code that
I am having trouble trying to hard encode a text document into my C#
I am trying to implement an MVC3 DatePicker but having trouble. I have the
I'm having some trouble trying to compile a program that uses exp function on
LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm
I am trying to generate a PDF using HTML template. i am having trouble
I have to simulate a process scheduler using SRTN algorithm and im having trouble
I`m having trouble trying to optimize this query with OVER (PARTITION BY ...) because
i'm having trouble trying to define this function-like macro, which takes 4 vector magnitudes
I am having trouble trying to print a table from a webpage. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.