{"id":5167,"date":"2023-05-19T15:02:13","date_gmt":"2023-05-19T13:02:13","guid":{"rendered":"https:\/\/ekiwi.de\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/"},"modified":"2023-05-19T15:09:44","modified_gmt":"2023-05-19T13:09:44","slug":"visual-studio-create-method-from-selected-code-lines-refactoringredesign","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/en\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/","title":{"rendered":"Visual Studio: Create method from selected code lines (refactoring\/redesign)"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/#Refactoring_with_right-click_does_not_work\" >Refactoring with right-click does not work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/#Refactor_via_menu\" >Refactor via menu<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi.de\/en\/index.php\/5167\/visual-studio-create-method-from-selected-code-lines-refactoringredesign\/#Key_combination_to_extract_a_method\" >Key combination to extract a method<\/a><\/li><\/ul><\/nav><\/div>\n<p>Those who program with Visual Studio (<abbr title=\"Visual Studio\">VS<\/abbr>) sometimes find themselves in the situation where they want to outsource some lines of code within a method to a separate function\/method. This may be because you want to make the code clearer or because the code in question is needed several times, and it therefore makes sense to create a separate method for the code.<\/p>\n<p>The procedure for transferring existing code into a new method is called <strong>Refactoring <\/strong>(<em>Restructuring\/Redesigning Code<\/em>). So you have to look for a corresponding entry in <a href=\"https:\/\/ekiwi.de\/en\/index.php\/4779\/visual-studio-found-conflicts-between-different-versions-of-assembly\/\" title=\"Visual Studio: Found conflicts between different versions of assembly\">Visual Studio<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Refactoring_with_right-click_does_not_work\"><\/span>Refactoring with right-click does not work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The intuitive approach would be to select the lines of code in question and then right-click to find a corresponding entry for refactoring in the context menu. Unfortunately, one does <a href=\"https:\/\/ekiwi.de\/en\/index.php\/4793\/visual-studio-referenced-component-could-not-be-found\/\" title=\"Visual Studio: The referenced component could not be found\">not find the<\/a> corresponding feature here.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Refactor_via_menu\"><\/span>Refactor via menu<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Microsoft has placed refactoring in VS in the &#8220;<em>Edit<\/em>&#8221; menu or in the &#8220;<em>Edit<\/em>&#8221; menu in the German language setting. First select the desired code to be extracted and then use the menu to select &#8220;<em>Refactor<\/em>&#8221; \u27a5\u00a0 &#8220;<em>Extract Method&#8230;<\/em>&#8220;.<\/p>\n<figure id=\"attachment_5115\" aria-describedby=\"caption-attachment-5115\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5115\" src=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-1.jpg\" alt=\"Convert Visual Studio code selection to method\" width=\"700\" height=\"440\" srcset=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-1.jpg 700w, \/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-1-300x189.jpg 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-5115\" class=\"wp-caption-text\">Select lines of code and choose from &#8220;<em>Edit<\/em>&#8220;, &#8220;<em>Refactor<\/em>&#8220;, &#8220;<em>Extract method&#8230;<\/em>&#8220;<\/figcaption><\/figure>\n<p>It will then replace the highlighted lines of code already with &#8220;<em>NewMethod<\/em>&#8220;, which will be inserted directly below as the new implementation of the method. At the same time, a small window opens in which you can adjust the name of the new method. In the example here, the name was changed to &#8220;<em>UpdateProperties<\/em>&#8220;. If you then press <em>Enter,<\/em> both the implementation and the inserted reference are automatically renamed. If you are unsure whether everything is working correctly, you can display a preview with the key combination &#8220;<em>Shift+Enter<\/em>&#8220;.<\/p>\n<figure id=\"attachment_5117\" aria-describedby=\"caption-attachment-5117\" style=\"width: 608px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5117\" src=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-2.jpg\" alt=\"Screenshot Visual Studio assign new name for extracted method\" width=\"608\" height=\"302\" srcset=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-2.jpg 608w, \/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-2-300x149.jpg 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><figcaption id=\"caption-attachment-5117\" class=\"wp-caption-text\">enter new name for extracted method<\/figcaption><\/figure>\n<p>In the following screenshot, you can see that a new method has been successfully created from the selected code. You can now reuse this method as often as you like.<\/p>\n<figure id=\"attachment_5119\" aria-describedby=\"caption-attachment-5119\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5119\" src=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-3.jpg\" alt=\"Screenshot of the implementation of the new method and replacement of the selected code\" width=\"577\" height=\"304\" srcset=\"\/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-3.jpg 577w, \/wp-content\/uploads\/2023\/05\/visual-studio-create-method-from-selection-3-300x158.jpg 300w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><figcaption id=\"caption-attachment-5119\" class=\"wp-caption-text\">Implementation of new method is inserted below and selected code lines are replaced with method<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Key_combination_to_extract_a_method\"><\/span>Key combination to extract a method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If the way via the Edit menu is too cumbersome for you, you can also reach your goal faster by using the appropriate key combination (shortcut). To do this simply:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>Select the code in question<\/li>\n<li><em>Press Ctrl+R<\/em> and <em>Ctrl+M<\/em> one after the other (Ctrl key remains pressed)<\/li>\n<li>Assign method name and press <em>Enter<\/em> again.<\/li>\n<\/ol>\n<\/li>\n<\/ol>","protected":false},"excerpt":{"rendered":"<p>Those who program with Visual Studio (VS) sometimes find themselves in the situation where they want to outsource some lines<\/p>\n","protected":false},"author":2,"featured_media":4244,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[870],"tags":[872,989,886,876,990,893,875],"class_list":["post-5167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-c-en","tag-code-en","tag-programmierung-en","tag-programming-en","tag-visual-basic-en","tag-visual-stu","tag-visual-studio-en"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/comments?post=5167"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/posts\/5167\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media\/4244"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/media?parent=5167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/categories?post=5167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/en\/index.php\/wp-json\/wp\/v2\/tags?post=5167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}