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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:56:39+00:00 2026-05-21T22:56:39+00:00

In C# from time to time the issue creeps out. Usually I get an

  • 0

In C# from time to time the issue creeps out. Usually I get an object value and then I have to call "real" function for it. Something like this:

if (type==typeof(byte))
  val = rs.GetByte(col);
else if (type==typeof(int))
  val = rs.GetInt32(col);
...

or

if (type==typeof(byte))
  Call((byte)val);
else if (type==typeof(int))
  Call((int)val);
...

I can see a textual pattern here, but I didn’t come up with solution which would deal once for good with all the dirty work.

How do you deal it? You receive the object value and you have to either set it or pass it but not as an object but concrete type (PODs, Nullable of PODs and string) and…?

Edits

Complete example (this one is the cause of the question):

    protected IRecord ReadRecord()
    {
        if (!ResultSet.Read())
            return null;

        IRecord record = CreateIRecord();

        var type = record.GetType();
        int column = -1;

        foreach (var prop in type.GetProperties())
        {
            ++column;
            object val;

            if (prop.PropertyType == typeof(byte))
                val = ResultSet.GetByte(column);
            else if (prop.PropertyType == typeof(byte?))
                val = ResultSet.GetSqlByte(column).ToNullable();
            else if (prop.PropertyType == typeof(int))
                val = ResultSet.GetInt32(column);
            else if (prop.PropertyType == typeof(int?))
                val = ResultSet.GetSqlInt32(column).ToNullable();
            else if (prop.PropertyType == typeof(double))
                val = ResultSet.GetDouble(column);
            else if (prop.PropertyType == typeof(double?))
                val = ResultSet.GetSqlDouble(column).ToNullable();
            else if (prop.PropertyType == typeof(DateTime))
                val = ResultSet.GetDateTime(column);
            else if (prop.PropertyType == typeof(DateTime?))
                val = ResultSet.GetSqlDateTime(column).ToNullable();
            else if (prop.PropertyType == typeof(string))
                val = ResultSet.GetString(column);
            else
                throw new ArgumentException("Invalid property type {0}".Expand(prop.PropertyType.ToString()));

            prop.SetValue(record, val, null);
        }

        return record;


    }
  • 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-21T22:56:40+00:00Added an answer on May 21, 2026 at 10:56 pm

    You could use a strategy pattern. Just put the strategies for each type in a hashtable and then you don’t need the switches. However, you’ll need one class per strategy. It is especially useful if you have complex logic.

    If the logic is rather simple you could also use a Hashtable containing delegates.

    //pseudo code
    handlers.put(typeof(int), delegate(object value) { return something.GetInt32(value); });
    
    //like this
    var handler = handlers[type];
    handler.Invoke(val);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue from time to time, I have a few StreamReader s
We have an issue that occurs from time to time. We have a web
From time to time, I run into communications issue with other programmers, when we
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
From time to time I read that Fortran is or can be faster then
In my application, I have two queries that occur from time to time (from
I'm having a time issue with looping through like 1 million potential rows from
from time to time I get feedback from my web portal customers that they
When I zoom in/zoom out from time to time in my map activity I
On our MOSS Enterprise environment, we have a strange problem. From time to time,

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.