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

MichaelDavidCarr
MichaelDavidCarr used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
What kinds of things are you expecting to pass in value?

Author

Commented:
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.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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.
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Top Expert 2015
Commented:
Add a constructor to your DrinkCategory that accepts a string as a parameter.

You will then be able to assign your values the following way:
var rec = new Drinks();
rec.DrinkName = "Dr. Pepper";
rec.Category = New DrinkCategory("Soda")

Open in new window

I have been working on this as well and here is some new information.  I now have access to the type of the variable that I want not just the name of the type; unfortunately it is of type "IEntityType" and not of type "Type".

So the signature changes a bit and the body should look something similar to this:
public Object CreateVariable(string value, IEntityType entityType)
        {
            var obj = Activator.CreateInstance(entityType);    // Doesn't compile.  Parameter should be of type "Type" and not "IEntityType"
            obj.Value = value;
            return obj;
        } 

Open in new window


As always, thank you for helping me.

Author

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

Author

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

Author

Commented:
Thank you very much for helping me.  The problem wasn't as simple as I originally thought.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial