Code snippet

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    #1

    Code snippet

    I was watching a video that used a code snippet to create a property and
    when you type "prop" tab tab, it would create the private variable as well
    as the property definitions with the private variable in it. When you
    changed the private variable it would also change the variables in the
    Property definition.

    But when I do it, I only get the property definition but no variables in it:

    public int MyProperty { get; set; }

    Why is that?

    Where is the rest of the code?

    In the example it would do:

    private int myVar;

    public int MyProperty
    {
    get { return myVar; }
    set {myVar = value; }
    }

    Thanks,

    Tom


  • Peter Duniho

    #2
    Re: Code snippet

    On Tue, 21 Oct 2008 21:29:38 -0700, tshad <tfs@dslextreme .comwrote:
    I was watching a video that used a code snippet to create a property and
    when you type "prop" tab tab, it would create the private variable as
    well
    as the property definitions with the private variable in it. When you
    changed the private variable it would also change the variables in the
    Property definition.
    >
    But when I do it, I only get the property definition but no variables in
    it:
    >
    public int MyProperty { get; set; }
    >
    Why is that?
    When you do what? You haven't described what the video demonstrates, nor
    provided a link to the video, nor described what exactly you're doing.

    It _sounds_ like the video demonstrates two different things: using a code
    snippet to insert a property with private field; and using refactoring to
    rename the private field.
    Where is the rest of the code? [...]
    As for what you "get", that looks like a perfectly reasonable alternative
    to having the property and private field separate. In C# 3.0, there are
    now automatic properties, meaning that you can leave out the getter and
    setter implementations , and the compiler will generate the private field
    automatically for you. Since if you've defined a property it's generally
    a bad idea to write code that accesses the private field without going
    through the property, this is just as good, if not better, than writing
    the private field, getter, and setter explicitly.

    Of course, it only works for the simplest implementations . If you need
    your getter or setter to do something interesting, you'll have to
    implement the whole property explicitly. But lots of properties get by
    just fine without anything special.

    Pete

    Comment

    • tshad

      #3
      Re: Code snippet

      I hadn't realized that the snippet was a custom snippet.

      I found this one which is exactly what I want to do but when I try to import
      it into the snippet manage I get an error saying it is invalid.

      What am I missing here?

      *************** *************** ********
      <?xml version="1.0" encoding="utf-8" ?>
      - <CodeSnippets
      xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
      - <CodeSnippet Format="1.0.0">
      - <Header>
      <Title>propOL D</Title>
      <Shortcut>propO LD</Shortcut>
      <Description>Co de snippet for a longhand property</Description>
      <Author>Danie l Moth</Author>
      - <SnippetTypes >
      <SnippetType>Ex pansion</SnippetType>
      </SnippetTypes>
      </Header>
      - <Snippet>
      - <Declarations >
      - <Literal>
      <ID>field</ID>
      <ToolTip>backin g store</ToolTip>
      <Default>mPro p</Default>
      </Literal>
      - <Literal>
      <ID>type</ID>
      <ToolTip>Proper ty type</ToolTip>
      <Default>int</Default>
      </Literal>
      - <Literal>
      <ID>property</ID>
      <ToolTip>Proper ty name</ToolTip>
      <Default>MyProp erty</Default>
      </Literal>
      </Declarations>
      - <Code Language="cshar p">
      - <![CDATA[
      private $type$ $field$;
      public $type$ $property$
      {
      get {return this.$field$;}
      set {this.$field$ = value;}
      }

      $end$ ]]>
      </Code>
      </Snippet>
      </CodeSnippet>
      </CodeSnippets>
      *************** *************** ********

      Thanks,

      Tom

      On Tue, 21 Oct 2008 21:29:38 -0700, tshad <tfs@dslextreme .comwrote:
      >
      >I was watching a video that used a code snippet to create a property and
      >when you type "prop" tab tab, it would create the private variable as
      >well
      >as the property definitions with the private variable in it. When you
      >changed the private variable it would also change the variables in the
      >Property definition.
      >>
      >But when I do it, I only get the property definition but no variables in
      >it:
      >>
      > public int MyProperty { get; set; }
      >>
      >Why is that?
      >
      When you do what? You haven't described what the video demonstrates, nor
      provided a link to the video, nor described what exactly you're doing.
      >
      It _sounds_ like the video demonstrates two different things: using a code
      snippet to insert a property with private field; and using refactoring to
      rename the private field.
      >
      >Where is the rest of the code? [...]
      >
      As for what you "get", that looks like a perfectly reasonable alternative
      to having the property and private field separate. In C# 3.0, there are
      now automatic properties, meaning that you can leave out the getter and
      setter implementations , and the compiler will generate the private field
      automatically for you. Since if you've defined a property it's generally
      a bad idea to write code that accesses the private field without going
      through the property, this is just as good, if not better, than writing
      the private field, getter, and setter explicitly.
      >
      Of course, it only works for the simplest implementations . If you need
      your getter or setter to do something interesting, you'll have to
      implement the whole property explicitly. But lots of properties get by
      just fine without anything special.
      >
      Pete

      Comment

      • tshad

        #4
        Re: Code snippet

        I figured out part of the problem.

        The code snippet wasn't valid xml. When opening it up in IE it gave me an
        error. I had to delete the "-" from the beginning of the line and then IE
        showed it as valid.

        But I am still getting the error saying it is invalid:

        *************** *************** *************** **
        <?xml version="1.0" encoding="utf-8" ?>
        <CodeSnippets
        xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
        <Header>
        <Title>propOL D</Title>
        <Shortcut>propO LD</Shortcut>
        <Description>Co de snippet for a longhand property</Description>
        <Author>Danie l Moth</Author>
        <SnippetTypes >
        <SnippetType>Ex pansion</SnippetType>
        </SnippetTypes>
        </Header>
        <Snippet>
        <Declarations >
        <Literal>
        <ID>field</ID>
        <ToolTip>backin g store</ToolTip>
        <Default>mPro p</Default>
        </Literal>
        <Literal>
        <ID>type</ID>
        <ToolTip>Proper ty type</ToolTip>
        <Default>int</Default>
        </Literal>
        <Literal>
        <ID>property</ID>
        <ToolTip>Proper ty name</ToolTip>
        <Default>MyProp erty</Default>
        </Literal>
        </Declarations>
        <Code Language="cshar p">
        <![CDATA[
        private $type$ $field$;
        public $type$ $property$
        {
        get {return this.$field$;}
        set {this.$field$ = value;}
        }

        $end$

        ]]>
        </Code>
        </Snippet>
        </CodeSnippet>
        </CodeSnippets>
        *************** *************** *************** ***

        Tom

        "tshad" <tfs@dslextreme .comwrote in message
        news:%23oO6YMFN JHA.456@TK2MSFT NGP06.phx.gbl.. .
        >I hadn't realized that the snippet was a custom snippet.
        >
        I found this one which is exactly what I want to do but when I try to
        import it into the snippet manage I get an error saying it is invalid.
        >
        What am I missing here?
        >
        *************** *************** ********
        <?xml version="1.0" encoding="utf-8" ?>
        - <CodeSnippets
        xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
        - <CodeSnippet Format="1.0.0">
        - <Header>
        <Title>propOL D</Title>
        <Shortcut>propO LD</Shortcut>
        <Description>Co de snippet for a longhand property</Description>
        <Author>Danie l Moth</Author>
        - <SnippetTypes >
        <SnippetType>Ex pansion</SnippetType>
        </SnippetTypes>
        </Header>
        - <Snippet>
        - <Declarations >
        - <Literal>
        <ID>field</ID>
        <ToolTip>backin g store</ToolTip>
        <Default>mPro p</Default>
        </Literal>
        - <Literal>
        <ID>type</ID>
        <ToolTip>Proper ty type</ToolTip>
        <Default>int</Default>
        </Literal>
        - <Literal>
        <ID>property</ID>
        <ToolTip>Proper ty name</ToolTip>
        <Default>MyProp erty</Default>
        </Literal>
        </Declarations>
        - <Code Language="cshar p">
        - <![CDATA[
        private $type$ $field$;
        public $type$ $property$
        {
        get {return this.$field$;}
        set {this.$field$ = value;}
        }
        >
        $end$ ]]>
        </Code>
        </Snippet>
        </CodeSnippet>
        </CodeSnippets>
        *************** *************** ********
        >
        Thanks,
        >
        Tom
        >
        >
        >On Tue, 21 Oct 2008 21:29:38 -0700, tshad <tfs@dslextreme .comwrote:
        >>
        >>I was watching a video that used a code snippet to create a property and
        >>when you type "prop" tab tab, it would create the private variable as
        >>well
        >>as the property definitions with the private variable in it. When you
        >>changed the private variable it would also change the variables in the
        >>Property definition.
        >>>
        >>But when I do it, I only get the property definition but no variables in
        >>it:
        >>>
        >> public int MyProperty { get; set; }
        >>>
        >>Why is that?
        >>
        >When you do what? You haven't described what the video demonstrates, nor
        >provided a link to the video, nor described what exactly you're doing.
        >>
        >It _sounds_ like the video demonstrates two different things: using a
        >code snippet to insert a property with private field; and using
        >refactoring to rename the private field.
        >>
        >>Where is the rest of the code? [...]
        >>
        >As for what you "get", that looks like a perfectly reasonable alternative
        >to having the property and private field separate. In C# 3.0, there are
        >now automatic properties, meaning that you can leave out the getter and
        >setter implementations , and the compiler will generate the private field
        >automaticall y for you. Since if you've defined a property it's generally
        >a bad idea to write code that accesses the private field without going
        >through the property, this is just as good, if not better, than writing
        >the private field, getter, and setter explicitly.
        >>
        >Of course, it only works for the simplest implementations . If you need
        >your getter or setter to do something interesting, you'll have to
        >implement the whole property explicitly. But lots of properties get by
        >just fine without anything special.
        >>
        >Pete
        >
        >

        Comment

        • tshad

          #5
          Re: Code snippet

          I don't know why mine is getting an error.

          But I found out why my snippet looks different that the one in the video. He
          must have been using the one from 2005 and in 2008, the have a different one
          that isn't nearly as good. Not sure why they changed it. I just copied
          the text from the 2005 version and pasted it into the prop.snippet file for
          2008 and saved it and it works fine now.

          The 2005 version
          *************** *************** *************** ***
          <?xml version="1.0" encoding="utf-8" ?>
          <CodeSnippets
          xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
          <CodeSnippet Format="1.0.0">
          <Header>
          <Title>prop</Title>
          <Shortcut>pro p</Shortcut>
          <Description>Co de snippet for an automatically implemented
          property</Description>
          <Author>Microso ft Corporation</Author>
          <SnippetTypes >
          <SnippetType>Ex pansion</SnippetType>
          </SnippetTypes>
          </Header>
          <Snippet>
          <Declarations >
          <Literal>
          <ID>type</ID>
          <ToolTip>Proper ty type</ToolTip>
          <Default>int</Default>
          </Literal>
          <Literal>
          <ID>property</ID>
          <ToolTip>Proper ty name</ToolTip>
          <Default>MyProp erty</Default>
          </Literal>
          </Declarations>
          <Code Language="cshar p"><![CDATA[public $type$ $property$ { get;
          set; }$end$]]>
          </Code>
          </Snippet>
          </CodeSnippet>
          </CodeSnippets>
          *************** *************** *************** ****

          The 2008 version
          *************** *************** *************** *******
          <?xml version="1.0" encoding="utf-8" ?>
          <CodeSnippets
          xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
          <CodeSnippet Format="1.0.0">
          <Header>
          <Title>prop</Title>
          <Shortcut>pro p</Shortcut>
          <Description>Co de snippet for property and backing field</Description>
          <Author>Microso ft Corporation</Author>
          <SnippetTypes >
          <SnippetType>Ex pansion</SnippetType>
          </SnippetTypes>
          </Header>
          <Snippet>
          <Declarations >
          <Literal>
          <ID>type</ID>
          <ToolTip>Proper ty type</ToolTip>
          <Default>int</Default>
          </Literal>
          <Literal>
          <ID>property</ID>
          <ToolTip>Proper ty name</ToolTip>
          <Default>MyProp erty</Default>
          </Literal>
          <Literal>
          <ID>field</ID>
          <ToolTip>The variable backing this property</ToolTip>
          <Default>myVa r</Default>
          </Literal>
          </Declarations>
          <Code Language="cshar p"><![CDATA[private $type$ $field$;

          public $type$ $property$
          {
          get { return $field$;}
          set { $field$ = value;}
          }
          $end$]]>
          </Code>
          </Snippet>
          </CodeSnippet>
          </CodeSnippets>
          *************** *************** *************** *******

          Tom

          "tshad" <tfs@dslextreme .comwrote in message
          news:%23f2NQSFN JHA.4248@TK2MSF TNGP03.phx.gbl. ..
          >I figured out part of the problem.
          >
          The code snippet wasn't valid xml. When opening it up in IE it gave me an
          error. I had to delete the "-" from the beginning of the line and then IE
          showed it as valid.
          >
          But I am still getting the error saying it is invalid:
          >
          *************** *************** *************** **
          <?xml version="1.0" encoding="utf-8" ?>
          <CodeSnippets
          xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
          <CodeSnippet Format="1.0.0">
          <Header>
          <Title>propOL D</Title>
          <Shortcut>propO LD</Shortcut>
          <Description>Co de snippet for a longhand property</Description>
          <Author>Danie l Moth</Author>
          <SnippetTypes >
          <SnippetType>Ex pansion</SnippetType>
          </SnippetTypes>
          </Header>
          <Snippet>
          <Declarations >
          <Literal>
          <ID>field</ID>
          <ToolTip>backin g store</ToolTip>
          <Default>mPro p</Default>
          </Literal>
          <Literal>
          <ID>type</ID>
          <ToolTip>Proper ty type</ToolTip>
          <Default>int</Default>
          </Literal>
          <Literal>
          <ID>property</ID>
          <ToolTip>Proper ty name</ToolTip>
          <Default>MyProp erty</Default>
          </Literal>
          </Declarations>
          <Code Language="cshar p">
          <![CDATA[
          private $type$ $field$;
          public $type$ $property$
          {
          get {return this.$field$;}
          set {this.$field$ = value;}
          }
          >
          $end$
          >
          ]]>
          </Code>
          </Snippet>
          </CodeSnippet>
          </CodeSnippets>
          *************** *************** *************** ***
          >
          Tom
          >
          "tshad" <tfs@dslextreme .comwrote in message
          news:%23oO6YMFN JHA.456@TK2MSFT NGP06.phx.gbl.. .
          >>I hadn't realized that the snippet was a custom snippet.
          >>
          >I found this one which is exactly what I want to do but when I try to
          >import it into the snippet manage I get an error saying it is invalid.
          >>
          >What am I missing here?
          >>
          >************** *************** *********
          > <?xml version="1.0" encoding="utf-8" ?>
          >- <CodeSnippets
          >xmlns="http://schemas.microso ft.com/VisualStudio/2005/CodeSnippet">
          >- <CodeSnippet Format="1.0.0">
          >- <Header>
          > <Title>propOL D</Title>
          > <Shortcut>propO LD</Shortcut>
          > <Description>Co de snippet for a longhand property</Description>
          > <Author>Danie l Moth</Author>
          >- <SnippetTypes >
          > <SnippetType>Ex pansion</SnippetType>
          > </SnippetTypes>
          > </Header>
          >- <Snippet>
          >- <Declarations >
          >- <Literal>
          > <ID>field</ID>
          > <ToolTip>backin g store</ToolTip>
          > <Default>mPro p</Default>
          > </Literal>
          >- <Literal>
          > <ID>type</ID>
          > <ToolTip>Proper ty type</ToolTip>
          > <Default>int</Default>
          > </Literal>
          >- <Literal>
          > <ID>property</ID>
          > <ToolTip>Proper ty name</ToolTip>
          > <Default>MyProp erty</Default>
          > </Literal>
          > </Declarations>
          >- <Code Language="cshar p">
          >- <![CDATA[
          >private $type$ $field$;
          >public $type$ $property$
          >{
          >get {return this.$field$;}
          >set {this.$field$ = value;}
          >}
          >>
          >$end$ ]]>
          > </Code>
          > </Snippet>
          > </CodeSnippet>
          > </CodeSnippets>
          >************** *************** *********
          >>
          >Thanks,
          >>
          >Tom
          >>
          >>
          >>On Tue, 21 Oct 2008 21:29:38 -0700, tshad <tfs@dslextreme .comwrote:
          >>>
          >>>I was watching a video that used a code snippet to create a property
          >>>and
          >>>when you type "prop" tab tab, it would create the private variable as
          >>>well
          >>>as the property definitions with the private variable in it. When you
          >>>changed the private variable it would also change the variables in the
          >>>Property definition.
          >>>>
          >>>But when I do it, I only get the property definition but no variables
          >>>in it:
          >>>>
          >>> public int MyProperty { get; set; }
          >>>>
          >>>Why is that?
          >>>
          >>When you do what? You haven't described what the video demonstrates,
          >>nor provided a link to the video, nor described what exactly you're
          >>doing.
          >>>
          >>It _sounds_ like the video demonstrates two different things: using a
          >>code snippet to insert a property with private field; and using
          >>refactoring to rename the private field.
          >>>
          >>>Where is the rest of the code? [...]
          >>>
          >>As for what you "get", that looks like a perfectly reasonable
          >>alternative to having the property and private field separate. In C#
          >>3.0, there are now automatic properties, meaning that you can leave out
          >>the getter and setter implementations , and the compiler will generate
          >>the private field automatically for you. Since if you've defined a
          >>property it's generally a bad idea to write code that accesses the
          >>private field without going through the property, this is just as good,
          >>if not better, than writing the private field, getter, and setter
          >>explicitly.
          >>>
          >>Of course, it only works for the simplest implementations . If you need
          >>your getter or setter to do something interesting, you'll have to
          >>implement the whole property explicitly. But lots of properties get by
          >>just fine without anything special.
          >>>
          >>Pete
          >>
          >>
          >
          >

          Comment

          Working...