By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,137 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,137 IT Pros & Developers. It's quick & easy.

Code snippet

P: n/a
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
Oct 22 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Tue, 21 Oct 2008 21:29:38 -0700, tshad <tf*@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
Oct 22 '08 #2

P: n/a
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 <tf*@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

Oct 22 '08 #3

P: n/a
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" <tf*@dslextreme.comwrote in message
news:%2***************@TK2MSFTNGP06.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.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 <tf*@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


Oct 22 '08 #4

P: n/a
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" <tf*@dslextreme.comwrote in message
news:%2****************@TK2MSFTNGP03.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.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" <tf*@dslextreme.comwrote in message
news:%2***************@TK2MSFTNGP06.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.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 <tf*@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



Oct 22 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.