1

In a reporting application, I have a number of different types of graphs, each of which is output with code in a specific class. Eg. clsPieChart, clsBarChart, etc. Each of those is derived from a base class, clsChartBase.

Now, the report can be put together dynamically, so there is a string ID code in the database for each type of chart, eg. "pie", "bar", etc.

My solution has been to use a common static variable in each class to match it up with the database code, eg:

Class clsPieChart
  Inherits clsChartBase
  Public ReadOnly Shared _ChartID As String = "pie" ' Each class has one of these.

This is how I can identify which class needs to be instantiated to handle that part of the report. Is this a reasonable approach, or is there a better way to do this sort of thing?

2 Answers 2

1

I would suggest having a Dictionary(Of String, Type) instead - that keeps all the information in a single place, and means you can perform the mapping programmatically. You could make another mapping in the other direction as well, of course - and that can be autogenerated from the first (e.g. using ToDictionary() in LINQ).

I'd also strongly advise you to get rid of the cls prefix for your class names - it's worth reading Microsoft's naming conventions so that your code is idiomatic.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Yeah The Hungarian is just a byproduct of my working with javascript, php, etc in text editors. Tough habit to break.
@ingredient_15939: It's well worth trying to break it. Following the conventions of the language you're working in is crucial to writing code which looks and feels right to others.
True. :) Even with a dictionary, the class still needs the static string though, yes? Or are you saying I don't need it with that method?
@ingredient_15939: No, there's no need for a static string variable here. What benefit do you think it would have?
Sorry, I'm being a dill.. the string in the dictionary is all I need. :) Thanks again!
0

You could do TypeOf obj Is YourClass to identify the class of your object. You will get rid of keeping a shared variable within the class.

3 Comments

Thanks, but there's no object at that point, there's only the text field from the database, which I need to compare with something before instantiating the corresponding class.
I'm not sure I understand that correctly. At some point you must traverse through a decision tree to create an object of the corresponding class. If you have a db field that lets you decide the chart class, then Jon's method of creating a Dictionary(Of String, Type) is the best for you. That way, you can create a one-liner that will give you the corresponding type when you pass the class identifier (e.g. "pie") as key to it.
Yep that's correct. Jon's solution is neat and easily understandable to another reader what's going on when they see the assignments in one place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.