Code snippet

T

tshad

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
 
T

tshad

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

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
 
T

tshad

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 said:
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

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
 
T

tshad

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 said:
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 said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top