PC Review


Reply
Thread Tools Rate Thread

Code snippet

 
 
tshad
Guest
Posts: n/a
 
      22nd Oct 2008
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


 
Reply With Quote
 
 
 
 
tshad
Guest
Posts: n/a
 
      22nd Oct 2008
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.microsoft.com/VisualStudio/2005/CodeSnippet">
- <CodeSnippet Format="1.0.0">
- <Header>
<Title>propOLD</Title>
<Shortcut>propOLD</Shortcut>
<Description>Code snippet for a longhand property</Description>
<Author>Daniel Moth</Author>
- <SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
- <Snippet>
- <Declarations>
- <Literal>
<ID>field</ID>
<ToolTip>backing store</ToolTip>
<Default>mProp</Default>
</Literal>
- <Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
- <Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
- <Code Language="csharp">
- <![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 <(E-Mail Removed)> wrote:
>
>> 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



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      22nd Oct 2008
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.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propOLD</Title>
<Shortcut>propOLD</Shortcut>
<Description>Code snippet for a longhand property</Description>
<Author>Daniel Moth</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>field</ID>
<ToolTip>backing store</ToolTip>
<Default>mProp</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
private $type$ $field$;
public $type$ $property$
{
get {return this.$field$;}
set {this.$field$ = value;}
}

$end$

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

Tom

"tshad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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.microsoft.com/VisualStudio/2005/CodeSnippet">
> - <CodeSnippet Format="1.0.0">
> - <Header>
> <Title>propOLD</Title>
> <Shortcut>propOLD</Shortcut>
> <Description>Code snippet for a longhand property</Description>
> <Author>Daniel Moth</Author>
> - <SnippetTypes>
> <SnippetType>Expansion</SnippetType>
> </SnippetTypes>
> </Header>
> - <Snippet>
> - <Declarations>
> - <Literal>
> <ID>field</ID>
> <ToolTip>backing store</ToolTip>
> <Default>mProp</Default>
> </Literal>
> - <Literal>
> <ID>type</ID>
> <ToolTip>Property type</ToolTip>
> <Default>int</Default>
> </Literal>
> - <Literal>
> <ID>property</ID>
> <ToolTip>Property name</ToolTip>
> <Default>MyProperty</Default>
> </Literal>
> </Declarations>
> - <Code Language="csharp">
> - <![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 <(E-Mail Removed)> wrote:
>>
>>> 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

>
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      22nd Oct 2008
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.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for an automatically implemented
property</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![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.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for property and backing field</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;

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

Tom

"tshad" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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.microsoft.com/VisualStudio/2005/CodeSnippet">
> <CodeSnippet Format="1.0.0">
> <Header>
> <Title>propOLD</Title>
> <Shortcut>propOLD</Shortcut>
> <Description>Code snippet for a longhand property</Description>
> <Author>Daniel Moth</Author>
> <SnippetTypes>
> <SnippetType>Expansion</SnippetType>
> </SnippetTypes>
> </Header>
> <Snippet>
> <Declarations>
> <Literal>
> <ID>field</ID>
> <ToolTip>backing store</ToolTip>
> <Default>mProp</Default>
> </Literal>
> <Literal>
> <ID>type</ID>
> <ToolTip>Property type</ToolTip>
> <Default>int</Default>
> </Literal>
> <Literal>
> <ID>property</ID>
> <ToolTip>Property name</ToolTip>
> <Default>MyProperty</Default>
> </Literal>
> </Declarations>
> <Code Language="csharp">
> <![CDATA[
> private $type$ $field$;
> public $type$ $property$
> {
> get {return this.$field$;}
> set {this.$field$ = value;}
> }
>
> $end$
>
> ]]>
> </Code>
> </Snippet>
> </CodeSnippet>
> </CodeSnippets>
> ************************************************
>
> Tom
>
> "tshad" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>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.microsoft.com/VisualStudio/2005/CodeSnippet">
>> - <CodeSnippet Format="1.0.0">
>> - <Header>
>> <Title>propOLD</Title>
>> <Shortcut>propOLD</Shortcut>
>> <Description>Code snippet for a longhand property</Description>
>> <Author>Daniel Moth</Author>
>> - <SnippetTypes>
>> <SnippetType>Expansion</SnippetType>
>> </SnippetTypes>
>> </Header>
>> - <Snippet>
>> - <Declarations>
>> - <Literal>
>> <ID>field</ID>
>> <ToolTip>backing store</ToolTip>
>> <Default>mProp</Default>
>> </Literal>
>> - <Literal>
>> <ID>type</ID>
>> <ToolTip>Property type</ToolTip>
>> <Default>int</Default>
>> </Literal>
>> - <Literal>
>> <ID>property</ID>
>> <ToolTip>Property name</ToolTip>
>> <Default>MyProperty</Default>
>> </Literal>
>> </Declarations>
>> - <Code Language="csharp">
>> - <![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 <(E-Mail Removed)> wrote:
>>>
>>>> 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

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help-Intellisense (Code Snippet) Tab Tab does not insert code (VS2008) Jeff Johnson Microsoft C# .NET 0 12th Jul 2010 10:37 PM
insert into code snippet seeker Microsoft Access VBA Modules 2 25th Jun 2009 09:16 PM
Code Snippet Add-In?? Don Microsoft VB .NET 3 24th Sep 2003 09:24 AM
Re: code snippet help please Junior Microsoft Access Form Coding 0 7th Jul 2003 02:13 PM
Re: code snippet help please Allen Browne Microsoft Access Form Coding 0 7th Jul 2003 01:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:25 AM.