April 25, 2024, 02:18:37 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Variables and silly question time

Started by Andy, April 28, 2015, 05:50:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Yes, this is probably a silly question... but let me explain:

I'm trying to cut down on code - I've already coded what I need to do, but a large section is really doing the same thing, just two things are different.... and I have 90 of these!

Basically I have some variables (int's) called
DID01, DID02, DID03...... which are set to 1 or zero.

And I check for text in a string:

IF x$ = "0101".... then I check if DID01 = 1 and so on....
IF x$ = "0102".... then I check if DID02 = 1
IF x$ = "0103".... then I check if DID03 = 1

Where the "DID"s are set by checkboxes on a screen prior to the above....

If I could get the "0101" and then ask if DID + the 01 was equal to 1, likewise...
If I could get the "0102" and then ask if DID + the 02 was equal to 1

So, can we take a part of a string - say the 02 from "0102" and ask if DID + the "02" equals 1 ???

The DID01, DID02, DID03 etc variable have to be int's, otherwise that's a heck of a lot of re-coding for me.

Think it's worth asking... I've often wondered if you can do it, manipulating strings and ints.

Any ideas anyone.

Thanks,
Andy.
:)

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

billhsln

April 28, 2015, 08:49:15 AM #1 Last Edit: April 28, 2015, 11:52:08 AM by billhsln
Can you change the DIDxx to DID[xx]?  Since you need 90, DEF did[91]:INT gives you 90 spots, remember we start at 0, you will be wasting one spot, but who cares.

You were also saying that the did's are from checkboxes.  Define them starting at one number (1001 thru 1090).  Then you can use a loop to check and load the values.

DEF did[91]:INT

ENUM CONTROLIDs
w1_CHECKBOX01 = 1001
w1_CHECKBOX02
....
ENDENUM

FOR i = w1_CHECKBOX01 TO w1_CHECKBOX90
IF GETSTATE(win,i)
did[i] = 1
ELSE
did[i] = 0
ENDIF
NEXT i

i = VAL(MID$(x$,3,2))
IF did[i] = 1 then ...


Bill
When all else fails, get a bigger hammer.

LarryMc

Quote from: billhsln on April 28, 2015, 08:49:15 AM
Can you change the DIDxx to DID[xx]?
If I understand him correctly, he doesn't want to change:
QuoteThe DID01, DID02, DID03 etc variable have to be int's, otherwise that's a heck of a lot of re-coding for me.
There's a way to do it pointer math that I'll work up an example and post after while.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

Couldn't it be changed via a RegEx expression?

Bill
When all else fails, get a bigger hammer.

LarryMc

Scratch my idea about pointer math; it only works when you create an array with the NEW command.

Here's what I would do; and what you said here is key:
QuoteWhere the "DID"s are set by checkboxes on a screen prior to the above....
you set the checkboxes on one screen and then you go to another to to check the strings.
Bill is indeed spot on
except don't change DIDxx to DID[xx]
instead
at the beginning of the code for the screen where you start doing these test
IF x$ = "0101"....

put in this code
int did[91]
RtlZeroMemory(did,len(did)) 'sets everything to zero
for i=cb1 to cb90
  IF GETSTATE(win,i) then did[i] = 1 'no need to worry about zero because we zeroed everything out
next i


and then do your checking
i = VAL(MID$(x$,3,2))
IF did[i] = 1 then ...


and of course it all depends on your checkbox ids being numbered in consecutive order as Bill stated

and you don't have to change any code on the checkbox page
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

Is there a way to have DID01...DID90 use the same space as DID[00]?  Like what I would call an overlay?

Bill
When all else fails, get a bigger hammer.

LarryMc

After my initial try didn't work I took a stab at that and it crashed.
If there is a way I'm not smart enough. :-\
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

I was thinking it was some thing like UNION, but don't see how to make it do more than 1 variable.

Bill
When all else fails, get a bigger hammer.

billhsln

If all the DID01 -> DID90 are defined together, would their storage space be in a linear manner like an Array?  If so, maybe you could send to a subroutine the address of DID01 and then in that subroutine have it defined as DID[90]:INT?

Just trying to think outside of the box.

Bill
When all else fails, get a bigger hammer.

Andy

Thnaks guys for your suggestions.

My checkboxes are numbered (defined) consecutively, but I will have to use the 'Hex2Dec' routine rather than the VAL command as the numbers are hexadecimal.

The range goes from 01 to 5F.

I will give it a go and post back when I've tried it.

Thanks very much,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.