Making my code smaller...
Making my code smaller...
I need some help making my code smaller. I have somewhere around 220 variables for the factors of different conversions. I have found a way to check and see if 2 different option boxes are checked and that a button is pressed, when this happens it converts a number into another using the 2 checked boxes and the conversion factor that goes a long with them. So my problem is that if I want to do it the way I have now, it would be around 4000 lines of code, which I don't want, so how can I make my code to where it doesn't have to be as big?
Last edited by Antdizzle on Sun Aug 08, 2004 6:16 am, edited 1 time in total.
-
- Enthusiast
- Posts: 252
- Joined: Fri Feb 20, 2004 5:43 pm
Ok, here's a few things I'd do. Firstly, instead of having a constant for each possible converison, I'd have a factor that converted each unit to the SI unit, and maybe another that converted the SI unit to each unit. Then, to convert for exaxmple, inches to feet, I'd multiply the inches measurement by the InchesToSI factor, then multiply by the SIToFeet fatcor. So this would cut down on the number of conversion factors you'd need, only two for each unit.
Also, I think it'd be a good idea to put all the factors in an array, rather than use constants. Here's the way I'd do it, but possibly not the best way. I'd rename all your #OptionBox constants to things like #OptionBox_LeftInches and #OptionBox_RightCentimetres, things like that. Then I'd move them to the beginning of your enumeration so that they were numbered starting at 0. Then I'd create an array with an element for each of these OptionBox constants. Then, to save the factors it'd be something like this
The advantage of this is that when you're converting at the end, you simply get the GadgetId's of the checkboxes on the left and right, then multiply the input by the two respective array entries.
Hope some of this was some use.
Also, I think it'd be a good idea to put all the factors in an array, rather than use constants. Here's the way I'd do it, but possibly not the best way. I'd rename all your #OptionBox constants to things like #OptionBox_LeftInches and #OptionBox_RightCentimetres, things like that. Then I'd move them to the beginning of your enumeration so that they were numbered starting at 0. Then I'd create an array with an element for each of these OptionBox constants. Then, to save the factors it'd be something like this
Code: Select all
Dim SIConversion(NumberOfOptionBoxes)
SIConversion(#OptionBox_LeftCentimetres)=
SIConversion(#OptionBox_LeftInches)=
SIConversion(#OptionBox_LeftFeet)=
SIConversion(#OptionBox_LeftYards)=
etc
(the Left ones would convert to SI, the Right ones from SI)
The advantage of this is that when you're converting at the end, you simply get the GadgetId's of the checkboxes on the left and right, then multiply the input by the two respective array entries.
Hope some of this was some use.