0

In my code I change properties of items very often - almost 100 times in one void. These are the most common: checkBox.Enabled = true and checkBox.Checked = true.

I need my application to have a small file size, so I want to be able to type checkBox.E(t) to total an 11 byte saving (minus the initial declaration of *.E and bool t = true.

Is there a way to shorten access to these properties like I have shown above? This also means that checkBox.E() would have return true or false as the property is actually set?

Thanks in advance!

  • What do you mean with _shorten_? Technically these are properties not functions. – Steve Mar 05 '17 at 12:47
  • @dasblinkenlight Yes, something like that. –  Mar 05 '17 at 12:51
  • @Steve I changed the question. I thought function was the wrong thing. –  Mar 05 '17 at 12:52
  • 1
    Why on earth do you want to change a perfectly understandable property name to something that requires an effort to understand what's going on? Do you have some background on C++ macros with #define? – Steve Mar 05 '17 at 12:54
  • @Steve No, I don't know C++. Understandability is less important for me than file size. I'm the only one who will view this code. –  Mar 05 '17 at 12:55
  • 4
    No, I really cannot understand your requirement. E() instead of Enabled saves 4 bytes and are four SOURCE bytes, not on the compiled executable. – Steve Mar 05 '17 at 12:57
  • 1
    So i guess you will change the variable name `checkBox` to `cb` as well ? – Ofir Winegarten Mar 05 '17 at 12:58
  • @Steve I use `Enabled` about 100 times. That means 400 bytes total. If I do the same with other properties, I could probably save approx. 500 KB –  Mar 05 '17 at 12:59
  • @OfirWinegarten Yep, I've made the names small enough to distinguish them, but not long enough to be 25 chars each –  Mar 05 '17 at 13:00
  • 2
    If you use the same code in a hundred places then extract a pattern from that and apply that. Don't go around changing well-readable member names to gibberish. – CodeCaster Mar 05 '17 at 13:01
  • Why are 500 kb so important to you? – Ofir Winegarten Mar 05 '17 at 13:02
  • @CodeCaster I'm not sure to how get a pattern. I set every checkbox on my form to checked and disable it. –  Mar 05 '17 at 13:07
  • Yeah, so, put them in a list and use `foreach (var checkBox in checBoxList) { checkBox.Enabled = true; }`? [Or even `foreach (var checkBox in this.Controls.OfType()) { ... }`](http://stackoverflow.com/questions/4630391/get-all-controls-of-a-specific-type)? Plenty of solutions, depending on what your code is actually supposed to do. – CodeCaster Mar 05 '17 at 13:10
  • Here is a very simple solution for the problem (Written abbreviated to save space): Y c s s t p o t p y w t c b g t t b a. T a s t p t a, i y w t s t. O t o h, i m n b s a g i! – NineBerry Mar 05 '17 at 13:22
  • @NineBerry Good point, but its nothing like that. I would have a key telling me what letter equals what word. It's like: T i m w o d th. T=This; i=is; m=my; w=way; o=of; d=doing; th=things; –  Mar 05 '17 at 13:26
  • Edited and voting to reopen, because OP supplied enough clarifications to make the question entirely unambiguous. – Sergey Kalinichenko Mar 05 '17 at 15:27

1 Answers1

0

If you would like to make a shortcut for setting a property on a specific type, you can do it with an extension method inside a static class:

public static class CheckBoxExtensions {
    public static void E(this CheckBox cb) {
        cb.Enabled = true;
    }
    public static void C(this CheckBox cb) {
        cb.Checked = true;
    }
}

Now you can write checkbox.E() instead of checkbox.Enabled = true.

You should be very careful with extensions like that, because they have a great potential of transforming a perfectly readable code into something only its author would have a chance of understanding.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523