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
Solved

switching variable types in the same code

Posted on 1998-06-09
6
202 Views
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...  
0
Comment
Question by:Reid
  • 3
  • 3
6 Comments
 

Author Comment

by:Reid
ID: 1165528
Edited text of question
0
 
LVL 22

Accepted Solution

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

Expert Comment

by:nietod
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 nietod@theshop.net that would be great.  From there I'll try to give you an idea what direction to go in.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:nietod
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.  
0
 

Author Comment

by:Reid
ID: 1165532
Adjusted points to 150
0
 

Author Comment

by:Reid
ID: 1165533
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

860 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