Cast object on the fly

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?
LVL 20
Russ SuterAsked:
Who is Participating?
 
käµfm³d 👽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

0
 
Friman001Commented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Russ SuterAuthor 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.
0
 
käµfm³d 👽Commented:
Can you provide an example of this data source? This is a DataTable that you are using, yes?
0
 
Friman001Commented:
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
0
 
Russ SuterAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.