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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 97
Excel/Word Add-in in what language? 5 144
C++ question 3 75
Microsoft C++ code failing in executable that worked 9 124
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 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