We help IT Professionals succeed at work.

Cast object on the fly

Russ Suter
Russ Suter asked
on
I'm using the EEPlus library to create Excel documents.

http://epplus.codeplex.com/

It's very useful but one of the methods uses the data type to format the cell as an int, date, string, etc... This works fine for CLR data types but my dataset needs to be populated with the Sql Data Types because I need to know if it's a type money vs. decimal etc... When I do this the method just writes everything as text so I need a way to cast the parameter when I pass it into the method. If it's a Sql.Money type I need to cast it to a System.Decimal type.

Can anyone tell me how I'd do this on the fly?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Why not just create a function with overloads that take in each SQL type and return the corresponding CLR type? Then you have the same interface (i.e. function name) but with the intended behavior.

For example:

using System;
using System.Data.SqlTypes;

namespace _27830620
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlDateTime d1 = new SqlDateTime();
            DateTime d2 = ConvertType(d1);
        }

        static decimal ConvertType(SqlMoney value)
        {
            return value.ToDecimal();
        }

        static string ConvertType(SqlChars value)
        {
            return value.ToString();
        }

        static int ConvertType(SqlInt32 value)
        {
            return value.Value;
        }

        static double ConvertType(SqlDouble value)
        {
            return value.Value;
        }

        static DateTime ConvertType(SqlDateTime value)
        {
            return value.Value;
        }
    }
}

Open in new window

Just put up some type checking of the types.

if (*.GetType() == SQLMoney) { // Do this. }
else if (*.GetType() == SQL*) { // Do this. }
else { // I did not plan to have this value! } 

Open in new window


Catch my flow?

Ryan F
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
Neither proposed solution is appropriate for this case. The first won't work since the object being passed is always of type DataColumn. The Value property is always of type Object so overloading won't work.

The second proposed solution might work but it would create messy code. I was looking for a clean way to call the method without having to go through a lengthy if statement. The info is all there I'm just trying to work out how to properly implement it.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Can you provide an example of this data source? This is a DataTable that you are using, yes?
Alight, so you just need to have one method with an object param.  Then, you need to pass that object param into the method and use logic to figure out the type.  Once you know the type, you can do what you need to do with that data, be it add two decimal places and a dollar sign, or what have you.

Ryan F
Senior Software Developer
CERTIFIED EXPERT
Commented:
Russ SuterSenior Software Developer
CERTIFIED EXPERT

Author

Commented:
None of the other proposed solutions were able to solve the problem. This solution is elegant in only a couple of lines of code and able to handle all data types with no problems.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.