Solved

switching variable types in the same code

Posted on 1998-06-09
6
186 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 219
Fully specialized class template function 21 130
C++ Language error 28 197
Unable to start eclipse ? 17 134
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now