Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I am trying to convert this VS 2010 WPF VB.Net project to a VS 2008 Win Forms version.
Encrypting a dataset using AES, including compression[^]

My problem is that the Friend properties are written in the shorthand version using VS 2010 and that way of writing them is not supported in VS 2008.

I know understanding “Get” and “Set” should be simple but I just don’t get them.
With all of the examples I’ve looked at over that last several years I still don’t understand how to properly create them.
Here is what I’m trying to convert in the DataHandler Module.
VB
''' <summary>
''' The dataset for the application
''' </summary>
Friend Property TheDataSet As EncryptDataSetVB.MyDataSet

''' <summary>
''' Name of the file where the dataset is saved
''' </summary>
Friend Property FileName As String

''' <summary>
''' Username for encryption
''' </summary>
Friend Property UserName As String

''' <summary>
''' Password for encryption
''' </summary>
Friend Property Password As String

''' <summary>
''' Will the data be compressed before encrypting
''' </summary>
Friend Property Compress As Boolean


Would someone please show me the correct conversion and help me understand why they are that way.
Posted

Essentially VS2010 generates a backing field for you.

so:
Public Property Fred As String
in VS2010

in VS2008 would be:
VB
Private _Fred As String
Public Property Fred() As String
   Get
      Return _Fred
   End Get
   Set(ByVal value As String)
      _Fred = value
   End Set
End Property 'Fred
The "Get" part can be thought of a function with the same name and type as the property. The "Set" part is like a subroutine, but instead of calling it, you use the property name like a variable. The "backing field" is just a variable that stores what the property Gets or Sets.

I use a snippet to insert properties as it makes life much easier. Here is the listing for my "Insert Property" snippet.
<codesnippets>
	xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <codesnippet format="1.0.0">
    <Header>
      <Title>PropertyInsert</Title>
      <author>author</author>
      <shortcut>ips</shortcut>
      <description>Create Region</description>
      <snippettypes>
        <snippettype>Expansion</snippettype>
        <snippettype>SurroundsWith</snippettype>
      </snippettypes>
    </Header>
    <snippet>
      <declarations>
        <literal>
          <id>propname</id>
          <default>VarName</default>
        </literal>
        <literal>
          <id>Type</id>
          <default>Type</default>
        </literal>	
      </declarations>
      <code language="VB">
        <![CDATA[
	Private _$propname$ as  $Type$
	Public Property $propName$ As $Type$
		Get
			Return _$propname$
		End Get
		Set (ByVal value as $Type$)
			_$propname$ = value
		End Set
	End Property '$propname$
      ]]>
      </code>
    </snippet></codesnippet></codesnippets>
Just copy this to a text file with ".snippet" extension; i.e Property.snippet.

Then under VS, go to Tools->Code Snippette Manager and click on the "Import" button and select the file you created. To use it. just type "ips" (without the quotes) and hit the "Tab" key. A template will be inserted into your code. The first field is "VarName"; just type in what you what and hit the "Tab" key to move to "Type" and the type in the variable type (i.e. Sting, Boolean, etc.) and hit "Tab" again. It will automatically fill in the rest for you.

"ips" is the shortcut I defined for it. If you want to use some other shortcut, just change
<shortcut>ips</shortcut>
in the file.
 
Share this answer
 
Comments
ledtech3 2-Nov-13 0:27am    
So any property you would need can be done that way ?
No other stuff needs to be added ?
So many examples do things a little different it was difficult to understand what they were wanting in the get and set.
Some times the simplest things are the hardest to grasp.
TnTinMn 2-Nov-13 0:37am    
That is it pretty much for the simplest usage. You could add other code in the "Set" for validation or some other purpose, but it is not necessary. .Net uses properties as hook point in the code for things like databinding.
ledtech3 2-Nov-13 0:40am    
Thanks,
That pretty much answers my question then.
I will have to build a sample application for studying more advanced ways of using it.
TnTinMn 2-Nov-13 0:46am    
A good place to start is with documentation,
http://msdn.microsoft.com/en-us/library/zzh9ha57%28v=vs.90%29.aspx
Have fun!
ledtech3 2-Nov-13 0:55am    
Yea, thats why I had to break down and ask the question here.
MSDN documentation does not always make sense.
Or have sample code that does even close to what you want to do.
Or expain it the way you did.
Thanks again.
When the shorthand property is created in the editor enter down to the next line and type Get and press enter and it should create the Get/Set accessors for you. Then of course you will need to create your own backing fields, where as the short hand version creates the backing fields for you (in the background).

MSDN Reference http://msdn.microsoft.com/en-us/library/vstudio/dd293589.aspx[^]

VB
''' <summary>
   ''' The dataset for the application
   ''' </summary>
   Friend Property TheDataSet As EncryptDataSetVB.MyDataSet
       Get

       End Get
       Set(value As EncryptDataSetVB.MyDataSet)

       End Set
   End Property

   ''' <summary>
   ''' Name of the file where the dataset is saved
   ''' </summary>
   Friend Property FileName As String
       Get

       End Get
       Set(value As String)

       End Set
   End Property

   ''' <summary>
   ''' Username for encryption
   ''' </summary>
   Friend Property UserName As String

   ''' <summary>
   ''' Password for encryption
   ''' </summary>
   Friend Property Password As String

   ''' <summary>
   ''' Will the data be compressed before encrypting
   ''' </summary>
   Friend Property Compress As Boolean
 
Share this answer
 
v2
Comments
ledtech3 1-Nov-13 23:01pm    
I found that but what I'm trying to figure out is if anything else is supposed to go in the get or set like most of the tutorials I have read on it.
Like
Get
Return TheDataSet
End Get

Plus I'm using VS 2008 and that does not work like that there.(not exactly)
It does in VS 2010 but I don't use it unless I have to it on a different os boot.
Trak4Net 2-Nov-13 14:53pm    
I guess I misunderstood what you were looking for. It looks like you have it figured out from the other solution. If not let me know I will try and help out.
ledtech3 2-Nov-13 15:10pm    
Thanks I had already figured out the part you showed but did not undersatand what else was supposed to go in there.
Thank You for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900