Really this is all a list. I would like to make it not seem like a list.

private void GetCarType(string Text)
        {
           
            if (Text == "76705F76775F676F6C665F3833")
                txtType.Text = "Golf GTI";
            if (Text == "76705F6175645F72385F303800")
                txtType.Text = "Audi R8";
            if (Text == "76705F6368765F636F62616C74")
                txtType.Text = "Cobalt SS";
            if (Text == "76705F6475635F393939725F30")
                txtType.Text = "999R";
            if (Text == "76705F6D69745F65636C697073")
                txtType.Text = "99 Eclipse";
            if (Text == "76705F736C6E5F73375F303600")
                txtType.Text = "Saleen S7";
            if (Text == "76705F6368765F636F72766574")
                txtType.Text = "Z06";
            if (Text == "76705F6D7A645F7278375F3935")
                txtType.Text = "RX7";
            if (Text == "76705F6D7A645F7278385F3036")
                txtType.Text = "RX8";
            if (Text == "76705F6C616D5F6D7572636965")
                txtType.Text = "Murcielago";
            if (Text == "76705F6175645F7273345F3037")
                txtType.Text = "RS4";
            if (Text == "76705F76775F676F6C665F3037")
                txtType.Text = "R32";
            if (Text == "76705F76775F736369726F6363")
                txtType.Text = "Scirocco";
            if (Text == "76705F6368765F63616D61726F")
                txtType.Text = "Camaro Concept";
            if (Text == "76705F6467655F6368616C6C65")
                txtType.Text = "Challenger Concept";
            if (Text == "76705F6672645F67745F303600")
                txtType.Text = "Ford GT";
            if (Text == "76705F6E736E5F3335307A5F30")
                txtType.Text = "350 Z";
            if (Text == "76705F736C6E5F733238315F30")
                txtType.Text = "S302";
            if (Text == "76705F706F6E5F7472616E7361")
                txtType.Text = "Firebird";
            if (Text == "7670645F6C725F73706F72745F")
                txtType.Text = "Range Rover";
            if (Text == "76705F6D69745F333030306774")
                txtType.Text = "3000GT";
            if (Text == "76705F6D627A5F733630305F30")
                txtType.Text = "S600 " + "DUB";
            if (Text == "7670645F6368765F6368657665")
                txtType.Text = "Chevelle SS";

            if (Text == "76705F6672645F6D757374616E")
                txtType.Text = "Mustang GT";
            if (Text == "7670645F706F6E5F67746F5F36")
                txtType.Text = "66 GTO";
            if (Text == "76705F6467655F636861726765")
                txtType.Text = "Charger SRT-8";
            if (Text == "76705F6368765F696D70616C61")
                txtType.Text = "Impala SS";
            //if (Text == "")
            //    txtType.Text = "";

            if (Text == "76705F6372795F333030635F30")
                txtType.Text = "300C SRT-8";
            if (Text == "76705F6672645F636F6272615F")
                txtType.Text = "Cobra Concept";

            if (Text == "7670645F6475635F313039385F")
                txtType.Text = "1098R";
            if (Text == "7670645F616D5F6462395F3038")
                txtType.Text = "DB9";
            if (Text == "7670645F6D69745F65766F785F")
                txtType.Text = "Evolution X";
            if (Text == "76705F6D69745F65766F5F3036")
                txtType.Text = "Lancer";
            if (txtType.Text == String.Empty)
                txtType.Text = Text;
            if (Text == "76705F6C616D5F67616C6C6172")
                txtType.Text = "Gallardo";
            if (Text == "76705F6475635F6D6F6E737465")
                txtType.Text = "Monster S4RS";
            if (Text == "76705F6E736E5F736B796C696E")
                txtType.Text = "Skyline";
        }

Dani AI

Generated

The current GetCarType in is essentially a lookup table implemented as dozens of if statements. That works, but it is hard to read, hard to maintain and slow to change. As suggested, the cleanest in-process solution for a fixed mapping is a Dictionary — initialize it once and do O(1) lookups. If you need to edit mappings later, keep the pairs in a small external store and load them into a cached Dictionary at startup (this follows the idea from and but avoids scanning the file on every call).

A compact pattern to follow (replace placeholders with your real key/value pairs):

static readonly Dictionary<string,string> CarMap =
    new Dictionary<string,string>(StringComparer.OrdinalIgnoreCase)
{
    { "HEXCODE1", "Car A" },
    { "HEXCODE2", "Car B" }
};

string GetCarType(string text)
{
    if (string.IsNullOrWhiteSpace(text)) return text;
    return CarMap.TryGetValue(text, out var name) ? name : text;
}

Practical tips: canonicalize keys before lookup (trim whitespace, normalize hex case or strip padding bytes) so small formatting differences do not prevent matches. If keys are really binary values, convert the hex to a canonical byte representation and use a hashed key or custom comparer. For large or dynamically edited lists, persist pairs in a JSON/XML/DB and load them once into memory (use caching or a reload-on-change pattern for updates). Add unit tests to protect against accidental changes and keep the mapping data out of UI code so it’s easy to maintain.

Recommended Answers

All 5 Replies

So basically you want to replace that ~80 line long text substitution with something better?

Try using a database... solution found, have a great day :)

If that's not what you wanted, then how about including more details and less redundant code in your next post :twisted: might help get responses and cut down on misunderstanding.

If your not going to add/remove items then you could hard code it as a dictionary.
Otherwise you'll need a storage method that allows add, delete and query..database being the most powerful, or a textfile with relevant methods being an alternative.

If you just want to tidy up the code you have you could start by looking at a switch.

Agree with both of you but unless GAME wants to be more specific I guess we'll never know what he's looking to do anyway :P

You could simply create an xml that looks like this:

<cars>
  <car number="76705F6E736E5F736B796C696E" name="Skyline" />
  [...]
</cars>

And then simply iterate over your car elements and break if found.

XDocument xdoc = XDocument.Load("cars.xml");

foreach(XElement x in xdoc.Descendants("car"))
{
    if(x.Attribute("number").Value = Text)
    {
           txtType.Text = x.Attribute("name").Value;
           break;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.