I need some help to simplify this huge code that contains hundreds of lines, and i don't really know how to do it. The code looks really messy, and what i need is to return the model with predefined text color. Is there any simple method ?
I must explain a little bit more: - There is a list of phones with many models, and if any of this models are available, i need to be printed out with the model number and the color. The list is changing every hour/day...that is how it works. So then i need to fill out my code with all models, then to search for available ones. Also, all models "EndsWith" a specific code(e.g A23X), and i think is faster to search like this.
if (text.EndsWith("model-1")) model.Add(new Line { text = "Sony, Smartphone(model-1)", color = () => Sony.Color1 });
if (text.EndsWith("model-2")) model.Add(new Line { text = "Sony, Smartphone(model-2)", color = () => Sony.Color2 });
if (text.EndsWith("model-3")) model.Add(new Line { text = "Sony, Smartphone(model-3)", color = () => Sony.Color3 });
if (text.EndsWith("model-4")) model.Add(new Line { text = "Sony, Smartphone(model-4)", color = () => Sony.Color4 });
if (text.EndsWith("model-5")) model.Add(new Line { text = "Sony, Smartphone(model-5)", color = () => Sony.Color5 });
if (text.EndsWith("model-6")) model.Add(new Line { text = "Sony, Smartphone(model-6)", color = () => Sony.Color6 });
if (text.EndsWith("model-7")) model.Add(new Line { text = "Sony, Smartphone(model-7)", color = () => Sony.Color7 });
if (text.EndsWith("model-8")) model.Add(new Line { text = "Sony, Smartphone(model-8)", color = () => Sony.Color8 });
if (text.EndsWith("model-9")) model.Add(new Line { text = "Sony, Smartphone(model-9)", color = () => Sony.Color9 });
if (text.EndsWith("model-10")) model.Add(new Line { text = "Nokia, Smartphone(model-10)", color = () => Nokia.Color10 });
if (text.EndsWith("model-11")) model.Add(new Line { text = "Nokia, Smartphone(model-11)", color = () => Nokia.Color11 });
if (text.EndsWith("model-12")) model.Add(new Line { text = "Nokia, Smartphone(model-12)", color = () => Nokia.Color12 });
if (text.EndsWith("model-13")) model.Add(new Line { text = "Nokia, Smartphone(model-13)", color = () => Nokia.Color13 });
if (text.EndsWith("model-14")) model.Add(new Line { text = "Nokia, Smartphone(model-14)", color = () => Nokia.Color14 });
if (text.EndsWith("model-15")) model.Add(new Line { text = "Nokia, Smartphone(model-15)", color = () => Nokia.Color15 });
if (text.EndsWith("model-16")) model.Add(new Line { text = "Nokia, Smartphone(model-16)", color = () => Nokia.Color16 });
if (text.EndsWith("model-17")) model.Add(new Line { text = "Nokia, Smartphone(model-17)", color = () => Nokia.Color17 });
if (text.EndsWith("model-18")) model.Add(new Line { text = "Nokia, Smartphone(model-18)", color = () => Nokia.Color18 });
You could create such a dictionary to lookup the Line
instance from the model-name:
Dictionary<string, Line> ModelNames = new Dictionary<string, Line>
{
{"model-1", new Line { text = "Sony, Smartphone(model-1)", color = Sony.Color1 }},
{"model-2", new Line { text = "Sony, Smartphone(model-2)", color = Sony.Color2 }},
{"model-3", new Line { text = "Sony, Smartphone(model-3)", color = Sony.Color3 }},
// ...
{"model-10", new Line { text = "Nokia, Smartphone(model-10)", color = Nokia.Color10 }},
{"model-11", new Line { text = "Nokia, Smartphone(model-11)", color = Nokia.Color11 }},
{"model-12", new Line { text = "Nokia, Smartphone(model-12)", color = Nokia.Color12 }},
// ...
};
Now you could look if there's a known model:
Line matchingModelNameLine = ModelNames
.Where(kv => text.EndsWith(kv.Key, StringComparison.InvariantCultureIgnoreCase))
.Select(kv => kv.Value)
.FirstOrDefault();
if (matchingModelNameLine != null)
{
model.Add(matchingModelNameLine);
}
I dunno, maybe try it this way.
Regex model = new Regex(@"?<=model-)\d+");
int model;
String description;
object sColor; //Take the proper type here, no object
if (model.IsMatch("") && int.TryParse(model.Match("").Value, out model)) {
if (model > 0 && model < 10) {
description = String.Format("Sony, Smartphone(model-{0})", model);
sColor = Enum.Parse(typeof(Sony), String.Format("Color{0}", model))
}
else if (model > 9 && model < 19) {
description = String.Format("Nokia, Smartphone(model-{0})", model);
sColor = Enum.Parse(typeof(Nokia), String.Format("Color{0}", model))
}
model.Add(new Line { text = description, color = sColor });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With