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.
MichaelDavidCarrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What kinds of things are you expecting to pass in value?
0
MichaelDavidCarrAuthor 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.
0
käµfm³d 👽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.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Jacques Bourgeois (James Burger)PresidentCommented:
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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MichaelDavidCarrAuthor 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.
0
MichaelDavidCarrAuthor 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.
0
MichaelDavidCarrAuthor Commented:
Thank you very much for helping me.  The problem wasn't as simple as I originally thought.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.