Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Hybrid View

  1. #1
    Join Date
    Dec 2010
    Location
    Sunshine Coast, QLD, Australia
    Posts
    6

    C# Extract Method as Variable

    Hi,

    I'm quite new to using VS - and even C#.

    I've been following along with the R&D testing Raven DB tutorials but I got stuck when Nelson extracted the method below to a variable:

    session.Store(new Account
    {
    Id = string.Format("accounts/{0}", Guid.NewGuid()),
    Name = new Name
    {
    FirstName = AddFirstNameBox.Text,
    LastName = AddLastNameBox.Text
    }
    });

    Does anyone know how to do this without resharp?

  2. #2
    Join Date
    Nov 2009
    Location
    Dallas, Texas
    Posts
    170
    It's not possible to use visual studio refactor tools to extract a method to a variable. You can only do simple refactors like rename, reorder parameters, etc. If you don't have resharper just watch the video carefully and see how the code ends up after the refactor takes place.

  3. #3
    Join Date
    Oct 2009
    Posts
    123
    That is something I am having trouble with myself.

    Nelson: "Oh, hang on, I need to change something..."
    *sound of a fifteen-keystroke shortcut*
    The code has totally rearranged itself on screen and then he flips to another .cs file two frames later.

    It requires a LOT of pausing and scrutiny. It isn't an enormous problem, but it is sometimes difficult to follow someone using a customized interface that looks different to what I have on screen.

    I find myself doing a lot of typing by hand when Nelson uses shortcuts like that.
    ( ! ) Assets/_Scripts/TP_Camera.cs(50,35): BCE0000: WTF?

  4. #4
    Join Date
    Mar 2004
    Location
    Anacortes, WA
    Posts
    4,168
    Quote Originally Posted by D.M.J. View Post
    That is something I am having trouble with myself.

    Nelson: "Oh, hang on, I need to change something..."
    *sound of a fifteen-keystroke shortcut*
    The code has totally rearranged itself on screen and then he flips to another .cs file two frames later.

    It requires a LOT of pausing and scrutiny. It isn't an enormous problem, but it is sometimes difficult to follow someone using a customized interface that looks different to what I have on screen.

    I find myself doing a lot of typing by hand when Nelson uses shortcuts like that.
    Most of the refactorings are very straightforward. For example, all that happened here is that this:

    Code:
    session.Store(new Account
    {
    	Id = string.Format("accounts/{0}", Guid.NewGuid()),
    	Name = new Name
    	{
    		FirstName = AddFirstNameBox.Text,
    		LastName = AddLastNameBox.Text
    	}
    });
    Turned into this:
    Code:
    var account = new Account
    {
    	Id = string.Format("accounts/{0}", Guid.NewGuid()),
    	Name = new Name
    	{
    		FirstName = AddFirstNameBox.Text,
    		LastName = AddLastNameBox.Text
    	}
    };
    
    session.Store(account);
    The videos I tend to do for 3dbuzz don't have too much to do with the code - and these R&D videos, even less. It's more about the concepts. I tend to shuffle code around very quickly to make sure everything looks good as I type, and that's a habit that's hard to break for the videos.

    Would it help if I just said what was doing, like: "OK, now the argument passed into this method call is going to be aliased into a variable"?
    Need any help? Feel free to PM me - or send an email directly to nelson@3dbuzz.com!

  5. #5
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Quote Originally Posted by ;1534522
    Would it help if I just said what was doing, like: "OK, now the argument passed into this method call is going to be aliased into a variable"?
    Gee... wish I'd thought of that...

    /me runs and hides from Nelson!

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  6. #6
    Join Date
    Oct 2009
    Posts
    123
    Saying what you're doing would be perfect. I know the need for code to be aesthetically pleasing to the coder. I've done enough of it to understand the satisfaction from a well laid-out class and the pain that comes from knowing your spacing is inconsistent and your statements are haphazardly grouped.

    Your use of keyboard shortcuts means that there is less feedback on the screen than a mouse-centric user would give and I gain a lot of familiarity from following along and scratching my head for a bit whenever it doesn't work the way I expect it to.
    ( ! ) Assets/_Scripts/TP_Camera.cs(50,35): BCE0000: WTF?

  7. #7
    Join Date
    Mar 2002
    Location
    Sydney, Australia
    Posts
    157
    Unfortunately for those without it.. Resharper adds so much to productivity that it is certainly worth investing in as a consumer.

    The reality is, if Nelson had to manually retype all the refactoring, the videos would be way longer. And then I would probably imagine he wouldn't bother refactoring much of the code because it would take too long.

    To my mind it is better to end up with production level code and have the hassle of pausing the video more often.

  8. #8
    Join Date
    Oct 2009
    Posts
    123
    Oh I certainly didn't mean that I want things to be done in a less efficient way or to produce worse code! I find it instructive. But a little more "mechanical" narration would help.
    ( ! ) Assets/_Scripts/TP_Camera.cs(50,35): BCE0000: WTF?

  9. #9
    Join Date
    Mar 2002
    Location
    Sydney, Australia
    Posts
    157
    It's all good....

    Believe me, I spend more time in pause mode watching Nelson videos than actually playing them... and I have Resharper!

  10. #10
    Join Date
    Feb 2011
    Location
    Alberta, Canada
    Posts
    48
    Resharper adds so much to productivity that it is certainly worth investing in as a consumer.
    I'm not sure I'd go that far. Especially for anyone who is new to programming in general or even just C#. I don't care how much more convinient it is to use a tool like this, if you don't know why it's doing what it's doing it will do more harm than good. Refactoring is NOT always a good thing.

    As a programmer of 30 years I don't have to pause the videos, because I could care less how Nelson codes it. I have my own style. All I care about is the overall big picture of what he's doing. If you're finding it hard to follow the code I would suggest practice, practice, practice. Play with some of the concepts of object oriented programming, I also like the "Head First" series of books, although their Object Oriented Analysis and Design book is Java based, the concepts are all the same and it's those concepts that are far more important than how to use Raven DB or any other library. I would even go so far as to suggest learning to do some UML **shutters**.

    At this point it's all just R&D anyways. I can't speak as authoratatively as Nelson could, but I think it's probably only really important for you to get the idea of what Raven DB is at this point and understand how it differs from a traditional RDBMS.
    I often "Quote" myself. I find it adds spice to my conversations.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •