Link to home
Start Free TrialLog in
Avatar of MichaelDavidCarr
MichaelDavidCarr

asked on

How to create a variable based on its type...

First, thank you for helping me.

I need a function that has the following signature and that creates a variable of the passed in type and then assigns that variable to the incoming value and then returns the new variable.  This function should do something similar to this:
 
        
        public Object CreateVariable(string value, string castTo)
        {
            IFormatProvider provider = new CultureInfo("en-US");
            Object obj = Convert.ChangeType(value, castTo, provider);   //Btw, this is wrong.
            return obj;
        }

Open in new window


Again, thank you for helping me.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelDavidCarr
MichaelDavidCarr

ASKER

Thank you for getting back to me so quickly.

The answer to your question might require a bit of explanation.

As an example, assume I have two tables:  1) Drink Categories and 2) Drinks.  Further assume I have a 1 to many relationship between Drink Categories and Drinks.  Values in the Drink Categories table are "Wine" and "Soda".  Values in the Drinks table are: "Coke" and "Soda", "Pepsi" and "Soda", "Merlot" and "Wine", "Chablis" and "Wine".

I am writing a custom csv import facility.  Assume in the csv file I have one additional record that I want to import:  "Dr. Pepper" and "Soda".

While reading the csv file the values are always strings.  But, the relationship between the two tables is of type "Drink Category".  

var rec = new Drinks();
rec.DrinkName = "Dr. Pepper";
rec.Category = "Soda";                     // This line is where my question lies!

Open in new window


Since rec.Category is of type DrinkCategory and it is not of type string, the above line fails.

So, it seems to me that I need to do something like this:
var rec = new Drinks();
rec.DrinkName = "Dr. Pepper";
rec.Category = CreateVariable("Soda", "DrinkCategory");

Open in new window


Again, thank you for your help.
I want to make sure I have a good understanding of your issue (which I can't really do at work right now), so if no one else chimes in before this evening, then I'll respond back with some suggestions.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've requested that this question be closed as follows:

Accepted answer: 0 points for MichaelDavidCarr's comment #a39574411

for the following reason:

After two days of investigating this issue, this problem turned out to be more complicated than I originally stated.  It is not just a matter of casting or converting one type into another.  It also requires that I do some more linq statements.  Sorry about that and thank you all for your time.  I will evenly distribute the points.
Hello Mr. Moderator,

Please see my above comment and evenly distribute the points among the experts.  Thank you very much.  By the way, I selected "Accept Multiple Solutions" and didn't see how to select multiple solutions.  So now I need your help to distribute the points.  Thank you again.
Thank you very much for helping me.  The problem wasn't as simple as I originally thought.