Coding without declering variables RRS feed

  • General discussion

  • //I have gotten  used to coding UI without declaring elements variables

    //like this

    private void OnMainBar_Tapped(object sender, TappedRoutedEventArgs e)


                if (sender.GetType() == typeof(Button))


                    if ((sender as Button).Tag != null)


                       if((sender as Button ).Tag.GetType ()==typeof (View.ParcelPage ))






            } // I could have done this with   var btn=((sender)Button);...

               //so is there advantages or disadvantage in my approach

    Monday, October 20, 2014 5:03 PM

All replies

  • I thought I saw a doc outlining that the c# compiler would optimize that sort of thing into one cast behind the scenes and use it repeatedly, but I can't seem to find that document at the moment.

    I use variables just for readability and ease of updating code.  For example, if you fired the method from another UI object instead of a button, you'd have a lot of "Buttons" to change to the new thing, where I might have only the one line.

    var senderObject = sender as HyperlinkButton

    Darin R.

    Monday, October 20, 2014 6:07 PM
  • Yes but not having a variable mince you don't have to

    worry about changing to a new control in  the code need only to be extended

    cause  if (sender.GetType() == typeof(Button)) is not type button

    the thread will continue to the next statement ...

    Wednesday, October 22, 2014 11:30 AM
  • The code is ugly and hard to follow. But it's your code, so you should do what you prefer.

    I like my code to be as concise as possible, while also being logical and easy to read.

    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Thursday, October 23, 2014 4:09 AM
  • Is not that the code is hard  to read  cause WinRT support Navigation by typeof  word

    so most programmers have gotten used to working with reflection ...

    Thursday, October 23, 2014 6:36 AM
  • Or to shorten even that:

    if (sender is Button)

    Darin R.

    Thursday, October 23, 2014 9:27 PM
  • It requires more work to read your code than to simply cast the button to a variable and then use the variable. It would produce simple, less verbose code, which is easier to read.

    You should post this question to Stack Overflow, where you will get some really interesting and detailed answers, probably with a detailed discussion of the pros and cons of your method.

    I'm a self-taught noob amateur. Please take this into account when responding to my posts or when taking advice from me.

    Friday, October 24, 2014 12:04 AM
  • is key word does  not necessary  substitute typeof key word ..  
    Friday, October 24, 2014 10:42 AM
  • Yes but  HyperlinkButton is Button but it is not  typeof(Button) so we can not substitute (is) key

    word for (typeof) key word ....

    Friday, October 24, 2014 11:32 AM
  • Hi pumpkinswan...

    Don’t say you’re an amateur programmer

    cause programming  deals with 

    Information and  no one can

    Ever muster information .

    Friday, October 24, 2014 4:05 PM
  • >Don't say you're an amateur programmer

    An amateur is not the same thing as a 'novice' or 'beginner'.  It just means that you do something because you like it, not because it's your job.  You can be both an amateur and an expert.


    David http://blogs.msdn.com/b/dbrowne/

    Friday, October 24, 2014 5:12 PM
  • Cool... but it's important to realize that we all love IT especially windows development ...
    Friday, October 24, 2014 8:02 PM