[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 748
  • Last Modified:

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?
0
Russ Suter
Asked:
Russ Suter
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:
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

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now