search an array

I want make a function that will search an array.
Can I make just one function to search all data types (using the variant
data type) or do I need to make a seperate function for each type. ie one
for string, longint, double, etc.
I would like to use just one function but am not sure how you distinguish what data type has been passed as the variant array - or if in fact you can do this. I am using delphi 3.02
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You certainly could use variants, both for the thing you want to search for and for the array, however, even Borland points out that variants are not very effecient.  And as you get into them you will find them not easy either. I suggest you would be much better off doing several seperate functions within a single class and using overloaded methods.

TSearchClass = class
  function Search(ForAnInteger : integer; IntArray : array of Integer): boolean; overload;

  function Search(forAChar : string; CharArray : array of Char): boolean; overload;

Hi everyone...

since clyde is using Delphi 3, he can't use overload. I don't know if the command "VarType" exists in D3, but you can check anyway. This would give you the type of the variant. If you want to convert your variant to a certain type, simply use the .AsInteger, .AsString, etc. after your variable. Like
"if MyVariant.AsString = 'Hello World' then DoSomething;"

Hope that helps...

Kind regards, Jonny...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.