Property return value error - vb.net

I have a class classed class1.vb, it has a public property named prop1 with a get and a set clause. I add one to the value passed in the set clause and in the get clause return the value variable as determined in the set clause. However, I get an error on the variable in the get clause...
Public Property prop1()
Get
Return value
End Get
Set (value)
value = value +1
End Set
End Property
any ideas?

Try setting the type of the property and try changing your value variable to something else, since the Set is using it's own local variable of the same name:
Private myValue As Integer
Public Property prop1() As Integer
Get
Return MyValue
End Get
Set (ByVal value As Integer)
MyValue = value
End Set
End Property

Related

How does one set a range of possible values a property can be in Visual Basic?

I need a property to never be less than 0. Is there a way to set a property's range so that it can never be a certain value, or in this case, never less than zero?
I've looked into this, but since I'm not sure what I'm looking for, it's hard to do research.
Raise an argument exception if the value is invalid.
Public Property MyProperty As Integer
Get
Return _myProperty
End Get
Set(value As Integer)
If value < 0 Then
Throw New ArgumentException("Value must be greater than or equal to zero.", "MyProperty")
End If
_myProperty = value
End Set
End Property
Alternatively, the property setter might be able to handle the out-of-range value. For example, if we don't what get raise the exception but instead use an alternate value within an acceptable range. Example:
Public Property MyProperty As Integer
Get
Return _myProperty
End Get
Set(value As Integer)
If value < 0 Then
_myProperty = 0
Else
_myProperty = value
End If
End Set
End Property

Object variable as object property

I have a collection of Employee objects in a collection called MyEmployees.
I want to assign one of these objects as the superior of the others.
I have created the property pSuperior in the class cEmployee
Below are the Set and Get methods for the class:
Public Property Set Superior(value As Object)
Set pSuperior = value
End Property
Public Property Get Superior() As Object
Superior = pSuperior
End Property
The Set method gives me no errors at run-time:
Set MyEmployees.Item(1).Superior = MyEmployees.Item(2)
But when I use
Debug.Print MyEmployees.Item(1).Superior.Name
to test I get run-time error 91 object variable or with block not set
Is my Set Method not working?
You always assign objects to variables (including return values of functions and properties) using Set. So the code for your Get method should be:
Public Property Get Superior() As Object
Set Superior = pSuperior
End Property
Looks like you're Get property is missing Set for your object assignment as a return value. I'm not sure what else you may have going on, so here's some sample code of what worked for me (except no collection). It may also be an improvement to define your Get/Set objects as Employees.
Here's example class code:
Private pSuperior As Employee
Private pName As String
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Name(Value As String)
pName = Value
End Property
Public Property Get Superior() As Employee
Set Superior = pSuperior
End Property
Public Property Set Superior(Value As Employee)
Set pSuperior = Value
End Property
And Example using the class as you described:
Sub test()
Set John = New Employee
Set Tim = New Employee
Set Tim.Superior = John
John.Name = "John"
Tim.Name = "Tim"
MsgBox Tim.Name & "'s Superior is " & Tim.Superior.Name
End Sub

What is the equivalent for the "propfull" shortcut in 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

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.

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