Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Any tips to improve this code?

TheUninvited

Active Coder
So basically i've been trying to convert sublime snippets to visual studio. So far i created this to grab all the snippets inside the sublime text and i am wondering if there's any way to improve the way of finding the elements in the for loop? My code works you know but to me i am trying to make this converter for training and learning purposes i know there is a snippet designer out there i can probably just do everything with my hands and i can still be fine it would take less time than trying to copy and paste my snippets from sublime. But still i just thought why not do this :p Here is the code and video :

The code:
C#:
 public void regexReplaceSublimeParameters(int num, List<string> mylist)
    {    
         
      Regex maxReg = new Regex(@"(\${)" + num + @"(.*?)(?=})");
      Match maxMatch = maxReg.Match(subInText);

for (int i = 0; i < ReturnCounter(); i++)
      {
        if (maxMatch.Success)
        {
          maxReg = new Regex(@"(\${)" + num + @"(.*?)(?=})");
          maxMatch = maxReg.Match(subInText);
          mylist.Add(maxMatch.Value);
          num++;
        }          
      }

foreach (string list in mylist)
      {
        Console.WriteLine(list);
      }
  }


    public int ReturnCounter()
    {
     
      Regex maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
      Match maxMatch = maxReg.Match(subInText);

      while (maxMatch.Success)
      {
     
        if (!maxMatch.Success)
        {
          break;
        }
        else
        {
          counter++;
          maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
          maxMatch = maxReg.Match(subInText);
        }
       
      }

     
      return counter;

    }

The video:
To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 
Last edited by a moderator:
Sorry if I wasn't clear in my previous suggestion, but it's possible to create a regular expression that will capture all the elements from the input text and give you back a collection. The Regex Class documentation on the Microsoft website (https://docs.microsoft.com/en-us/do...egularexpressions.regex?view=netframework-4.8) gives an example of how to do this, using the Regex.Matches() function. The regex I suggested in your other post will give you that collection. The advantage of this approach is that you won't need to create a specific regex with unique integer to match every integer value in the input text.
 
I think you could start with comments e.g. how does the code work, and what does it do.

Documentation is key, it helps you and others remember what does what.
 
I strongly agree with, @Malcolm, on this one.

Comments and Documenting your Code is essential and it helps you to remember what you're doing and it let's others know what a part of your Code does and what it's for.
 
Chuck your code inside of a class then declare the regex at the top so you don't have to keep doing Regex regex and in your while loop you don't need the else statement, you can get away with just:

C#:
public int ReturnCounter()
{
      Regex maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
      Match maxMatch = maxReg.Match(subInText);

      while (maxMatch.Success)
      {
        if (!maxMatch.Success)
        {
          break;
        }
        // Do not need else, break stops the execution there...
        counter++;
        maxReg = new Regex(@"(\${)" + counter + @"(.*?)(?=})");
        maxMatch = maxReg.Match(subInText);
        
      }

    
      return counter;

}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom