c# - Alternative way of implementation of a code -


The title is not exactly meaningful, but I am not going to share it and share it. I wrote a code some time later. Based on this, I was writing code to check duplicate. The code is as follows:

  curNumber = getTOCReference (selItem.SNo, IsParent); Checkfunduplicate (Colonel, God, actual TOCRF out); CurNumber = realTOCRef;  

and Code for CheckForDuplicates

  ListViewItem curItem = this.tlvItems.FindItemWithText (curNumber); If (curItem! = Null) {curNumber = this.getTOCReference (curNumber,! IsParent); Checkfunduplicate (Colonel, God, actual TOCRF out); } And {the actual TOCRef = curNumber; }  

What this code does, it becomes a TOC and it tries that it already exists and if there is an existing TOC then a new TOC becomes. Once it determines that the generated TOC is not in the list, it stores it in the actual TOCRF and sends it back to the main calling code.

I used "out" to return the previously generated tosi, which was because of what I did not want to do because I did this because non duplicate TOC was generated, return The calling code was not going on, instead it was stopped through previous instances, then returned.

I would appreciate any help on this.

When you have a single out parameter, the value of that value as the return value of that method It can often be removed using. The return value looks like this: By changing the external string:

  String CheckForldPlicats (String Colonel, Bull Espent) {ListViewItem curItem = this.tlvItems.FindItemWithText (curNumber); If (curItem! = Null) {string newNumber = this.getTOCReference (curNumber, IsParent); CurNumber = CheckForDuplicates (new number, god); } // else - curNumber is correct - a duplicate return curNumber; }  

To design, I think this code should be a part of your getTOCReference so that customers do not worry about deeduplication - to understand it It is understandable that the duplication of the original function of getTOCReference is nothing complicated - just a little name and some glue:

  • getTOCReference < Rename / code> to getNextTOCReference , because what it actually does - gets the TOC value which is a duplex Without checking for a ticket, the next will come.
  • Create a new method GetNextAvailableTOCRefernece me GetTOCRefernece means to retrieve some saved status and it will return many calls to the same value . It's not what this does, so the new name makes it clear that it looks:

(essentially the code that you gave at the top of the page, using the return value The method is re-applied for, wrapped in the method.)

  string GetNext is available TOCReference (String Colonel, Bull ISPint) {String Lowumber = Gate-Extract Relief (Col., ISPIRE) ; Return checkfunduallinks (new number, god); }  

With these changes, you call getNextAvailalbeTOCreference when you want a new reference and you can be sure that it is unique (no duplicate No.)

I have left the code style - I believe that you can be a bit more consistent, and provide comments for some arguments - especially getTOCReference Because there are 4 different things depending on that method logic. And of course, unit testing so that you can make sure that you say so!


Comments