0

I've been struggling for a while to find a way of calling an instance of a class from a user defined input.

Essentially I want to call an instance from my StandardVehicle class that has already been defined, however the problem is that the name of the instance that I'm calling is defined by a users selection on a combo box on a form.

This is how I assumed it would be formatted, however I get an error when I try calling the instance saying that I cannot complicitly convert from string to StandardVehicle.

        StandardVehicle VauxhallB = new StandardVehicle();
        VauxhallB.Model = "B";
        VauxhallB.Manufacturer = "Vauxhall";
        VauxhallB.Doors = 5;
        VauxhallB.HorsePower = 200;
        VauxhallB.Transmission = "Manual";
        VauxhallB.Year = 2009;
        VauxhallB.Cylinders = 6;
        VauxhallB.Seats = 7;
        VauxhallB.Price = 17000;
        VauxhallB.Registration = "abc1243";

        StandardVehicle objname = comboBox1.Text;

I'm assuming it's just a case of formatting the string, but I've been searching for hours and can't figure out what format it needs to be in to define it as an instance.

Any help would be appreciated.

Regards, S.

2
  • That is not how it works. There is no implicit conversion from a string to StandardVehicle, unless you provide one yourself.
    – driis
    Commented Apr 12, 2011 at 17:58
  • You can not convert a string to an object like that. Do you have a list of StandardVehicle instances you bind to the combobox?
    – manji
    Commented Apr 12, 2011 at 17:58

6 Answers 6

4

No, it's definitely not a case of just formatting the string. It sounds like you want a

Dictionary<string, StandardVehicle>

Populate that to map from strings to objects, then look up the object corresponding to the name given by the user.

1

VauxhallB is just a symbol to the computer. The user shouldn't reference your variable names -- although it is probably possible to do so via reflection, but that's a complicated aspect of programming.

A better way to do this would be to associate each instance with a string "name" in a Dictionary<string, StandardVehicle>, and look up with that. Something like:

Dictionary<string, StandardVehicle> nameMap = new Dictionary<string, StandardVehicle>();
nameMap["VauxhallB"] = VauxhallB;
StandardVehicle objname = nameMap[comboBox1.Text];

Also, it's generally more readable if you only use lower case names for local variables, to better distinguish them from classes, public methods, and properties.

0
1

You cannot convert string to an object unless you have

  1. Constructor that takes string and creates required object
  2. An explicit/implicit cast operator

and another option

Use Activator.CreateInstace

3rd option is only valid if string contains valid type name, for your example you can do

Type type = Type.GetType(comboBox1.Text);
StandardVehicle objname = Activator.CreateInstace(type);

this will work if comboBox1.Text == "StandardVehicle".

More information here:

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

http://msdn.microsoft.com/en-us/library/wccyzw83.aspx

0

Try this:

StandardVehicle selectedObject = (StandardVehicle)comboBox1.SelectedItem;

Of course, your comboBox should be bound to a collection of StandardVehicles... If it's not, then go for Dictionary as was already suggested.

0

First I also thought you mean reflection but you want to use your class StandardVehicle always. Just the properties should be set by a single string you receive from the user.

Write yourself a constructor. You define the format of the single string, for example fixed position within that string:

public StandardVehicle(String data)
{
    this.Model = data.Substring(0, 1);
    this.HorsePower = Int32.Parse(data.Substring(1, 4);
}

Not very comfortable but might work. Add some error handling. Or XML loading or other formats.

0

It's a little more complicated than your sample. I'm going to start with the quick and easy approach, which, while IS quick and easy, is kind of an anti-pattern, but I 'll explain that at the end:

IBritishAuto car;

switch
{
    case "Vauxhall":
    {
        car = new Vauxhall(standardVehicle);    
        break;
    }
    case "Jaguar":
    {
        car = new Jaguar(standardVehicle);
    }
    ...etc.
}

The reason why this is sub-optimal is that every time you want to add a new car type, you have to modify the original code block, and risk regression. If you're happy with this solution, and don't care, you can stop reading now.

Still there?

This pattern is called a "conditional dispatcher". There are two main strategies to improve it: refactor to a Command pattern (that's capped because it's a Gang of Four pattern), or a broader implementation of a concept called 'inversion of control'. If you're interested, here are your Google terms:

"refactor conditional dispatcher" "inversion of control".

Good luck.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.