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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:13:23+00:00 2026-05-16T02:13:23+00:00

I have a class that returns an object type to a variable. The variable

  • 0

I have a class that returns an object type to a variable. The variable Must know what the real type is when operations are performed on it:

public object Data 
    {
        get
        {
            switch (CriteriaID)
            {
                case (int)matrix2.enums.NodeTypeEnums.Enums.MultiLineText:
                    return (string)_Data;
                case (int)matrix2.enums.NodeTypeEnums.Enums.SingleLineText:
                    return (string)_Data;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Number:
                    int temp = 0;
                    return int.TryParse((string)_Data, out temp) ? (int?)temp : null;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Price:
                    decimal temp1 = 0;
                    return decimal.TryParse((string)_Data, out temp1) ? (decimal?)temp1 : null;
                case (int)matrix2.enums.NodeTypeEnums.Enums.PullDown:
                    return (string)_Data;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Checkbox:
                    bool temp2 = false;
                    return bool.TryParse((string)_Data, out temp2) ? (bool?)temp2 : null;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Date:
                    DateTime temp3 = DateTime.MinValue;
                    return DateTime.TryParse((string)_Data, out temp3) ? ((DateTime?)temp3).Value.ToString("MM/dd/yyyy") : null;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Link:
                    return (string)_Data;
                case (int)matrix2.enums.NodeTypeEnums.Enums.Image:
                    return (string)_Data;
                default:
                    return (string)_Data;
            }
        }
        set
        {
            _Data = value;   
        }
    }

The data property is used like this:

temp.Count() > 0 ? temp.FirstOrDefault().Data : " "

Using it like this works but I am not sure if this is the best implementation and/or the most efficient. Is their a better way to do this?

  • 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-16T02:13:24+00:00Added an answer on May 16, 2026 at 2:13 am

    To be honest, seeing as your dealing with a finite number of possible results, you might as well stick to using a switch statement. What I would do, is change a lot of what you are doing…

    Firstly, don’t put any complex operations with getters or setters. You should push these out to a seperate method:

    public object Data
    {
      get
      {
        return FormatData(_Data);
      }
    }
    

    Secondly, you don’t need to cast in your case blocks:

    case (int)matrix2.enums.NodeTypeEnums.Enums.MultiLineText:
    

    Thirdly,

    temp.Count() > 0 ? temp.FirstOrDefault().Data : " "
    

    …there are a few issues here to, for instance:

    temp.Count() > 0
    

    …will cause the entire enumerable to be enumerated, its much more efficient to do:

    temp.Any()
    

    …as it will return after it encounters the first element, next:

    temp.FirstOrDefault().Data
    

    if you are calling .Count() > 0 (or now hopefully .Any()), you can change this to .First(), as you’ve already established that there must be an instance for it to hit this logic path.

    temp.FirstOrDefault().Data : "&nbsp";
    

    Because your method potentially returns types other than string, the result of the complete tertiary operation can only be assigned to Object, because the compiler won’t know which argument type it can be assigned to….imagine you are returning an Int32 or a String, which one?

    UPDATE
    Now I think about it, a really important change you should make is simply:

    public object Data { get; set;}
    

    The property should really just return the raw data. It should be outside the problem domain of the model that you present the data (and hence convert it into other types).

    It looks like you are essentially writing the object out to html, so why all the work to do with formatting it? For the most part you can use .ToString() to get the string representation of the object:

    temp.Any() ? temp.First().Data.ToString() : "nbsp;"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, it's a really bad idea to use… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer If you are not dead set on using a listbox,… May 16, 2026 at 9:17 am
  • Editorial Team
    Editorial Team added an answer killproc will terminate programs in the process list which match… May 16, 2026 at 9:17 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have this class public class Item { public Coordinate coordinate { get; set;
I have a Perl class that contains a hash instance variable for storing other
Imagine that I have a general class Person . Then I have specializations of
I have a business object class BusinessObject which implements an interface IAlternateInterface. I already
I have a custom object that I am trying to bind to a control.
In my business layer, I need many, many methods that follow the pattern: public
I must honestly say that I do not really understand much about casting, but
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have to do an HTML page with 2 text boxes, one for name
In the class fooBase there is SomeProperty . In the class barBase , 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.