473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Oct 22 '08 #1
4 2178
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rob Conner | last post by:
No you don't need to know Zope to help me. The whole reason I'd even want to do this is because of Zope though. I made a Zope product, and now want to perfect it. some simple example code... ...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
15
by: iwdu15 | last post by:
hi, i have these 2 code snippets: Public Sub ImRec(ByVal IM As IAccIm, ByVal Sender As IAccUser) Dim str As String = IM.GetConvertedText(DECODE) Dim temp As String = str temp =...
2
by: daokfella | last post by:
Hi all, I've created a code snippet in VS 2005 and saved it in the proper snippet folder. When I right-click and select Insert Snippet, I navigation to My Code Snippets and it shows my snippet....
3
by: Hardy | last post by:
I created a code snippet which is for displaying file name,Line number and function name in C#.NET project. // <File> sf.GetFileName() // </File> // <Line> sf.GetFileLineNumber().ToString()...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
2
by: steve.falzon | last post by:
Hi I've been searching high and low for this info but have been unsuccessful so far. I wonder if anybody knows where I can get a specification for the directives contained within the code...
2
Frinavale
by: Frinavale | last post by:
I'm attempting to supplement the help for my code by providing other developers with code snippets that demonstrate how to use my classes/method. I've created a .snippet file and have placed it in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.