Jump to content
Willicious

How to declare an entire set of numbers as a condition

Recommended Posts

I need to use a full set of numbers as a condition.

 

So, for example, rather than:

 

var
  i: Integer;

for i := 0 to 5 do
begin
  if PosY = Y - i then
  //whatever
end;

 

I need it to be:

 

var
  i: {not sure what to declare i as};

i := [0..5];

if PoxY := Y - i then
begin
  //whatever
end;

 

The code needs to execute iff all conditions are met (Y - 0 and Y - 1 and Y - 2 and Y - 3, etc) and not just if any of the possible 6 conditions are met (Y - 0 or Y - 1 or Y - 2 or Y - 3, etc).

 

I've tried declaring i as "array of Integer;" but get the error "Incompatible types: dynamic array and set". I can't declare it as a set either, so, what do I need to declare i as in order to be able to specify all values between 0 and 5 as the condition?

Share this post


Link to post

A set has its limitations but can work in some cases. But you also can use Case:

 

Case myInteger Of:

  0, 15, 1999, 65535:

  Begin

    // DoSomething

  End;

  Else

    Exit;

End;

  • Thanks 1

Share this post


Link to post
5 hours ago, Willicious said:

The code needs to execute iff all conditions are met (Y - 0 and Y - 1 and Y - 2 and Y - 3, etc) and not just if any of the possible 6 conditions are met (Y - 0 or Y - 1 or Y - 2 or Y - 3, etc).

 

I don't understand how this can even work at all. If PosY equals Y - 1 then PosY cannot be equal to Y - 2 nor Y - 3 and so on. There can either one condition be met or none, but not all.

  • Like 1

Share this post


Link to post

I remember exactly the same topic some time ago. Was it yours? But the exact problem I can't understand either. Plz specify it more concretely.

You can use arrays , dictionaries, lists and any other container that have unlimited length. Or VarInRange. Or, if the ranges are static, "case" (though internally it will be not more than multiple if's).

I'm not aware of a class that handles multiple ranges (like ranges.Add(3, 300); ranges.Add(40000, 50000000); if ranges.Includes(i)) but probably someone had created such one. Otherwise it seems relatively easy to create from scratch. If you need exactly this, try to search in the area of Unicode handling. This is the 1st application that comes to mind which could require multiple ranges (f.ex., to define letters of some alphabet that are not coming in single block in the Unicode table)

Edited by Fr0sT.Brutal
  • Like 1

Share this post


Link to post
16 hours ago, Uwe Raabe said:

I don't understand how this can even work at all. If PosY equals Y - 1 then PosY cannot be equal to Y - 2 nor Y - 3 and so on. There can either one condition be met or none, but not all.

Apologies, I didn't explain that properly. I'm not looking for a single point to be true, but rather a number of points along a straight line. So (X = 0), (Y = 0 and 1 and 2 and 3, etc...) would draw the line required. But, the entire line needs to be drawn for the condition to be met.

 

16 hours ago, Fr0sT.Brutal said:

I remember exactly the same topic some time ago. Was it yours?

Yes, to be honest I do now remember asking this same question a while back. Here it is. I didn't fully understand the answer given in that topic, but now that I look at it again I can see what's being suggested.

 

It seems that a boolean needs to be set, then each number is iterated over until one returns false (which breaks the loop). If all numbers return true, then we can run the code.

 

That could work, and using "case ... of" could also work, again to set the bool value.

 

Thanks everyone, I'm sure I'll figure it out.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×