skip to Main Content

For example i want to write (rand%(MaxValue-MinValue+1))-MinValue i don’t want to write this line everytime i want to use it but is there a way to make Visual Studio auto write it when i just need to write rand or R.

2

Answers


  1. Why not use #define?

    Here is an example:

    #define rand(r,max,min) ((r%(max-min+1))-min)
    

    And to call it:

    rand(_rand, MaxValue, MinValue);
    
    Login or Signup to reply.
  2. I recommend you to use snippet, it’s very convenient.

    You need to create a new txt file on the desktop, and then enter the following code in the file.

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>propertyName</Title>
          <Shortcut>rand</Shortcut>
        </Header>
        <Snippet>
      <Code Language="cpp">
        <![CDATA[
        (rand%(MaxValue-MinValue+1))-MinValue
        ]]>
      </Code>
      <Declarations>   
      </Declarations>
    </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    Then change the suffix of the txt file to snippet, and finally import the snippet according to the method in the document.

    RESULT:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search