switching variable types in the same code

Posted on 1998-06-09
Last Modified: 2010-04-01
(Visual C++) if I am writing a database, and I want the user to be able to search based on different fields, is there a way to write the code once with a variable that can change?  For example: I'd like then to be able to search for int ID, char name, or int age - all under the same class.  I have a statement (in a member function) "if(targetID != searchptr -> ID)..... "  I want to be able to use that same statement for the different fields without writing the code 3 different times.  Can I set up a variable that can be set to int ID, char name, etc based on what the user wants and just put that variable in my code?  This question is from someone learning C++, so answers can't be too vague...  
Question by:Reid
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Author Comment

ID: 1165528
Edited text of question
LVL 22

Accepted Solution

nietod earned 150 total points
ID: 1165529
Unfortunately, the question is a little vague...  but I think I know what you need.  Answer coming.
LVL 22

Expert Comment

ID: 1165530
Now that I think about it.  There are two possible ways to go here.  

One is templates, but I suspect that is not what you really need.   Templates allow you to write algorithms (code) that are independant of data types, then the templates are "expanded" or converted into code for the data types you need.  For example you cou might want to create a max function that returns the maximum of two integers and another that returns the maximum of two doubles.  The two functions have same algorithm, they just work with different types of data.  You can write the function one time as a template and the compiler will create a version for integers and a second one for doubles.

The other option is to place you field types in a class hierarchy.  This is more advanced programming, But I suspect it is what you really need.  You would define an interface in the base class that allows you to compare types.  This is going over your head isn't it?

I'll tell you what, can you try to give me a better idea of what you've got or need?  If you could post some code or e-mail it to me at that would be great.  From there I'll try to give you an idea what direction to go in.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 1165531
I'm not pesterig you.  I'm just posting this to keep the question active.  Inactive questions are "autograded" after 21 days.  This comment us an "extension".

Hope the studying is going well.  

Author Comment

ID: 1165532
Adjusted points to 150

Author Comment

ID: 1165533

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question