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

  • Home
  • SEARCH
  • 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 7419387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:03:43+00:00 2026-05-29T08:03:43+00:00

I know the title is confusing so I’ll try to explain better. Here’s basically

  • 0

I know the title is confusing so I’ll try to explain better. Here’s basically what I want to do inside a method:

if (record["id"] != DBNull.Value) _id = Convert.ToInt32(record["id"]);
else id = -1;

I want this to work for multiple types that I have stored in my database. (So if it’s a string it converts it to a string and so forth). Any way to do this is fine, and I was trying to do it with a method. I got this far but C# won’t automatically convert int to object. ideas?

    private void Load(ref object var, object obj, object def)
    {
        if (var is int)
        {
            var = Convert.ToInt32(obj);
        }
    }

   int _id;
   Load(ref _id, record["id"], -1);

Just to clarify, my error is “cannot convert from ref int to ref object”.
Thanks for any help.

  • 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-29T08:03:44+00:00Added an answer on May 29, 2026 at 8:03 am

    You can use Convert.ChangeType() and make your method generic:

    private void Load<T, U>(out T value, U obj, T defaultValue)
    {
        if (obj is DBNull)
           value = defaultValue;
        else
            value = (T)Convert.ChangeType(obj, typeof(T));
    }
    

    Now you can just use it like this (simplified example, not sure what you need def for):

    int id;
    object foo = 42;
    Load(out id, foo, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know title is a bit confusing ;D but basically what I want to
World's most convuluted title I know, an example should explain it better. I have
I know the title of this question is a bit confusing, but here it
I know the title is bit confusing for my question. Let me explain:- There
I know the title is bit confusing for my question. Let me explain:- There
Extremly confusing title, i know. Hello, i am facing some funky issues here. I
A confusing title I know. Let me explain. I have to marshal an array
Apologies for the confusing title, didn't know how to express it better. This is
Ok I know the title is a bit confusing but here is my problem:
The title may be a little confusing, but I don't know how else to

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.