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 693411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:45:04+00:00 2026-05-14T02:45:04+00:00

I’m coming from a background in ColdFusion, and finally moving onto something modern, so

  • 0

I’m coming from a background in ColdFusion, and finally moving onto something modern, so please bear with me.

I’m running into a problem casting objects. I have two database tables that I’m using as Models – Residential and Commercial. Both of them share the majority of their fields, though each has a few unique fields. I’ve created another class as a container that contains the sum of all property fields. Query the Residential and Commercial, stuff it into my container, cunningly called Property. This works fine.

However, I’m having problems aliasing the fields from Residential/Commercial onto Property. It’s quite easy to create a method for each property: fillPropertyByResidential(Residential source) and fillPropertyByCommercial(Commercial source), and alias the variables. That also works fine, but quite obviously will copy a bunch of code – all those fields that are shared between the two main Models.

So, I’d like a generic fillPropertyBySource() that takes the object, and detects if it’s Residential or Commercial, fills the particular fields of each respective type, then do all the fields in common. Except, I gather in C# that variables created inside an If are only in the scope of the if, so I’m not sure how to do this.

    public property fillPropertyBySource(object source)
    {
        property prop = new property();
        if (source is Residential)
        {
            Residential o = (Residential)source;
            //Fill Residential only fields

        }
        else if (source is Commercial)
        {
            Commercial o = (Commercial)source;
            //Fill Commercial only fields
        }
        //Fill fields shared by both
        prop.price = (int)o.price;
        prop.bathrooms = (float)o.bathrooms;

        return prop;
    }

“o” being a Commercial or Residential only exists within the scope of the if. How do I detect the original type of the source object and take action?

Bear with me – the shift from ColdFusion into a modern language is pretty….. difficult. More so since I’m used to procedural code and MVC is a massive paradigm shift.

Edit: I should include the error: The name ‘o’ does not exist in the current context
For the aliases of price and bathrooms in the shared area.

  • 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-14T02:45:04+00:00Added an answer on May 14, 2026 at 2:45 am

    Because of the artificial constraints imposed on you by using database-generated object types instead of hierarchically defined ones, there’s really no elegant solution to this at all. The best solution I can offer is to use a partial class to attach a known interface and then use overloading to do the minimum amount of work required when copying the object:

    interface IPropertyBase
    {
        decimal Price { get; set; }
        int Bathrooms { get; set; }
    }
    
    partial class Residential : IPropertyBase
    {
    }
    
    partial class Commercial : IPropertyBase
    {
    }
    
    class Property : IPropertyBase
    {
     public decimal Price { get; set; }
     public int Bathrooms { get; set; }
    
        // Commercial
        public int Offices { get; set; }
    
        // Residential
        public int Bedrooms { get; set; }
    
        private void CopyFromBase(IPropertyBase o)
        {
            Price = o.Price;
            Bathrooms = o.Bathrooms;
        }
    
        public void CopyFrom(Commercial o)
        {
            CopyFromBase(o);
    
            Offices = o.Offices;
        }
    
        public void CopyFrom(Residential o)
        {
            CopyFromBase(o);
    
            Bedrooms = o.Bedrooms;
        }
    }
    

    As a general note, “union” objects of this type are often a bad idea. You’re better off defining the common properties in an IPropertyBase interface and leaving the Residential/Commercial objects in their “native” form as you work with them. If you need to create a combined collection of all properties in an area, for example, create a List<IPropertyBase> and work directly with that — it will keep the objects in the list intact (allowing you to determine their original type later using the is/as operators if you need) and it won’t have the overhead of lots of empty, meaningless fields on each object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.