What is the equivalent for the "propfull" shortcut in vb.net - vb.net

What is the shortcut for auto-implementing a property in vb.net? I want get/set and associated field, like with "propfull" in C#.
I'm using VS12 with ReSharper 7.1.3

you can declare property just like this without get or set
Property Prop2 As String = "Empty"
this is equivalent to
Private _Prop2 As String = "Empty"
Property Prop2 As String
Get
Return _Prop2
End Get
Set(ByVal value As String)
_Prop2 = value
End Set
End Property
here are the details http://msdn.microsoft.com/en-us/library/dd293589.aspx

Related

In VB.NET, is it possible to break on an AutoProperty's Set?

In VB.NET, if a property is defined thusly:
Private _status As String
Public Property Status() As String
Get
Return _status
End Get
Set(value As String)
_status = value
End Set
End Property
... then it is trivial to add a break to the Set part of the property in Visual Studio.
However, if it is defined as an AutoProperty:
Public Property Status() As String
, can a break still be added to the Set somehow, without converting it temporarily to a property with a backing variable as in the first example?

Can't serialize using DataContractSerializer

I have tried the following code:
PolicyProcessRequest.BranchCode = "HeadOff"
PolicyProcessRequest.Policy.BranchCode = "HeadOff"
PolicyProcessRequest.Policy.Risks.Item(0).BranchCode = "HeadOff"
Dim dcs As DataContractSerializer = New DataContractSerializer(GetType(PureMessagingService.PolicyProcessRequestType))
Dim ms As New MemoryStream()
dcs.WriteObject(ms, PolicyProcessRequest)
Am getting the following exception on the call to WriteObject
System.Runtime.Serialization.SerializationException was caught
HResult=-2146233076 Message=Member BranchCode in type Sirius.SBO.Import.PureMessagingService.BaseRequestType cannot be serialized.
This exception is usually caused by trying to use a null value where a null value is not allowed.
The 'BranchCode' member is set to its default value (usually null or zero). The member's EmitDefault setting is 'false', indicating that the member should not be serialized.
However, the member's IsRequired setting is 'true', indicating that it must be serialized. This conflict cannot be resolved. Consider setting 'BranchCode' to a non-default value. Alternatively, you can change the EmitDefaultValue property on the DataMemberAttribute attribute to true, or changing the IsRequired property to false.
Yet I've set the 'BranchCode' property to the non default value everywhere in the request.
Public Class BaseNBQuoteRequestType
Private agentCodeField As String
Private branchCodeField As String
Private currencyCodeField As CurrencyType
Private currencyCodeFieldSpecified As Boolean
Private itemField As BasePartyType
Private policyField As BaseQuoteRiskMsgType
Private updatePartyField As Boolean
Public Property AgentCode() As String
Get
Return Me.agentCodeField
End Get
Set(ByVal value As String)
Me.agentCodeField = value
End Set
End Property
Public Property BranchCode() As String
Get
Return Me.branchCodeField
End Get
Set(ByVal value As String)
Me.branchCodeField = value
End Set
End Property
Public Property CurrencyCode() As CurrencyType
Get
Return Me.currencyCodeField
End Get
Set(ByVal value As CurrencyType)
Me.currencyCodeField = value
End Set
End Property
Public Property CurrencyCodeSpecified() As Boolean
Get
Return Me.currencyCodeFieldSpecified
End Get
Set(ByVal value As Boolean)
Me.currencyCodeFieldSpecified = value
End Set
End Property
Public Property Party() As BasePartyType
Get
Return Me.itemField
End Get
Set(ByVal value As BasePartyType)
Me.itemField = value
End Set
End Property
Public Property Policy() As BaseQuoteRiskMsgType
Get
Return Me.policyField
End Get
Set(ByVal value As BaseQuoteRiskMsgType)
Me.policyField = value
End Set
End Property
Public Property UpdateParty() As Boolean
Get
Return Me.updatePartyField
End Get
Set(ByVal value As Boolean)
Me.updatePartyField = value
End Set
End Property
End Class
I have the same problem, and it comes from the DataContractSerializer which is used to generate the code. I have other services which use the XmlSerializer with no problem.
Unfortunatly, when using Svcutil.exe or the 'Add Service Reference' feature in Visual Studio to generate client code, an appropriate serializer is automatically selected for you. If the schema is not compatible with the DataContractSerializer, the XmlSerializer is selected (source).
So, you have to manualy fix the IsRequired setting in references.cs each time you generate it.
Replace
IsRequired=true
by
IsRequired=false
in the references.cs file.

Changing property attributes at runtime in vb.net

I discover propertygrid which may be handy for editing or just showing some custom setup data in my program. But I have need for some attributes of properties to be changable.
Like 'readonly' attribute.
This is what I have so far:
Const myPersonCat As String = "MyPerson"
Const myDesc1 As String = "Firstname is one element"
<CategoryAttribute(myPersonCat), _
DescriptionAttribute(myDesc1), _
[ReadOnly](myBool)> _
Public Property firstname() As String
Get
Return _firstname
End Get
Set(ByVal value As String)
If Not _firstname = value Then save_param("firstname", value, myPersonCat, myDesc1)
_firstname = value
End Set
End Property
Const mydesc2 As String = "but Lastname is second"
<CategoryAttribute(myPersonCat), _
DescriptionAttribute(mydesc2), _
[ReadOnly](myBool)> _
Public Property lastname() As String
Get
Return _lastname
End Get
Set(ByVal value As String)
If Not _lastname = value Then save_param("lastname", value, myPersonCat, myDesc2)
_lastname = value
End Set
End Property
Save_param is call to function which saves property with basic data in database.
All of that work's nice.
But now is a question...
Is here some, not too complicated way to setting 'myBool' for readonly attribute with variable instead of constant with which I can block to change some properties dependable of situation in program.
Maybe for whole category or for a single property?
Or maybe here exists some other way to get similar functionality?
No, there is no way to change the value of the attribute. As an alternative, you can write code in your Property Set to make it throw an exception if the user tries to set the value while it is supposed to be read-only.

How to make several similar properties call one generic one

I'm wondering if it's possible in VB.NET to make similar properties call one generic one?
A sentence doesn't explain it well so here's a code example.
I have a bit field defined like this:
<Flags()> _
Enum E_Operation As Integer
Upload = 1
Download = 2
Overwrite = 4
etc...
End Enum
Now my class has one property per bit in the bit field. Each property just returns the value or sets the corresponding flag. e.g.
Public Property IsUpload() As Boolean
Get
Return ((Operation And E_Operation.Upload) = E_Operation.Upload)
End Get
Set(ByVal value As Boolean)
SetBit(E_Operation.Upload, value)
End Set
End Property
Now I have quite a lot of properties and I would like to simplify them (ideally just one line) by calling a generic property with the bit number to Set or Get.
Public Property IsUpload() As Boolean
GenericProperty(E_Operation.Upload)
End Property
Is there any way to achieve this in VB.NET?
You can make the enumeration a parameter in a private property:
Private Property OperationFlag(Flag As E_Operation) As Boolean
Get
Return ((Operation And Flag) = Flag)
End Get
Set(ByVal value As Boolean)
Operation = (Operation And Not Flag) Or (value And Flag)
End Set
End Property
And make a public property wrapper:
Public Property IsUpload As Boolean
Get
Return OperationFlag(E_Operation.Upload)
End Get
Set(value As Boolean)
OperationFlag(E_Operation.Upload) = value
End Set
End Property

VB.Net Keyboard Shortcut to auto-generate a Property

As the title suggests I am looking for the key sequence to generate the standard Property syntax in a vb.net class. Example below so there is no confusion on what I am asking for. Thanks in advance!
Public Property MyProperty() As String
Get
Return _MyProperty
End Get
Set(ByVal value As String)
Me._MyProperty= value
End Set
End Property
Type "property" and hit tab twice:
Private newPropertyValue As String
Public Property NewProperty() As String
Get
Return newPropertyValue
End Get
Set(ByVal value As String)
newPropertyValue = value
End Set
End Property
To generate property in Microsoft visual Studio just write prop and press tab for twice. Property will be generated automatically by Visual Studio.
Thanks,