Webservice with input class parameter converts it to an array - vb.net

I have a WebService defined as this:
<WebMethod(Description:="Retrieve a list of account profiles.")> _
<System.Web.Services.Protocols.SoapHeader("MyHeader")> _
Public Function RetrieveAccountProfile(<XmlElement(IsNullable:=True, Type:=GetType(WSRetrieveAccountProfile))> ByVal myAccount As WSRetrieveAccountProfile) As <XmlElement(IsNullable:=True)> WSResponseRetrieveAccountProfile
...
The class I am passing as an input parameter WSRetrieveAccountProfile is defined like this:
<Serializable()> _
<XmlType(Namespace:="http://mynamespace/", TypeName:="WSRetrieveAccountProfile")> _
Public Class WSRetrieveAccountProfile
<XmlElement(IsNullable:=False)> Public accountNumber As List(Of Integer)
Public Sub New()
End Sub
Public Function Validate() As Boolean
'Validations here
Return True
End Function
End Class
The issue: I consume web-service and I am working on the client side where I am trying to declare an object of type WSRetrieveAccountProfile so I can pass it as an input parameter when calling the web-service, but I get compilation error - it cannot recognize the type.
After checking Reference.vb I notice that an input parameter for the function is a plain array of Integer ByVal myAccount() As Integer. Now, if I add another variable to the WSRetrieveAccountProfile class definition the ByVal myAccount() As Integer suddenly changes to ByVal myAccount As WSRetrieveAccountProfile and problem is solved.
How do I solve this WITHOUT adding an unnecessary variable? I tried with XmlType attribute with no luck.
* UPDATE *
This definition works:
<Serializable()> _
<XmlType(Namespace:="http://mynamespace/", TypeName:="WSRetrieveAccountProfile")> _
Public Class WSRetrieveAccountProfile
<XmlElement(IsNullable:=False)> Public accountNumber As List(Of Integer)
<XmlElement(IsNullable:=True)> Public TEST As String
Public Sub New()
End Sub
Public Function Validate() As Boolean
'Validations here
Return True
End Function
End Class
* UPDATE - SOLUTION *
If I change
<XmlElement(IsNullable:=False)> Public accountNumber As List(Of Integer)
to
<XmlArray(IsNullable:=True)> Public accountNumber As List(Of Integer)
then the proxy is generated properly and I no longer have an issue.

Change
<XmlElement(IsNullable:=False)> Public accountNumber As List(Of Integer)
to
<XmlArray(IsNullable:=True)> Public accountNumber As List(Of Integer)
to fix proxy generation.

Related

How do I construct an "Add" routine for a custom object list?

I feel really stupid asking this question, but here goes...
I'm trying to create a custom object in VB, that is itself a list (or collection, or "tuple" - I'm not sure what the difference between these is) of custom objects, and I need to create routines to add and remove these secondary objects to/from the larger custom object. So far, my code goes something like this:
Public Class parameterSet
Friend _xParameter As String
Public Property xParameter() As String
Get
Return _xParameter
End Get
Set(value As String)
_xParameter = value
End Set
End Property
Friend _yParameter As String
Public Property yParameter() As String
Get
Return _yParameter
End Get
Set(value As String)
_yParameter = value
End Set
End Property
Friend _zParameter As String
Public Property zParameter() As String
Get
Return _zParameter
End Get
Set(value As String)
_zParameter = value
End Set
End Property
Public Sub New(ByVal xParameter As String, ByVal yParameter As String, ByVal zParameter As String)
_xParameter = xParameter
_yParameter = yParameter
_zParameter = zParameter
End Sub
End Class
Public Class parameterCollection
Friend _parameterCollection As New List(Of parameterSet)
Friend Sub Add(xParameter As String, yParameter As String, zParameter As String)
Throw New NotImplementedException()
End Sub
End Class
What do I have to put in the Add routine to make this work?
Your first class ought to look like this:
Public Class ParameterSet
Public Property X As String
Public Property Y As String
Public Property Z As String
Public Sub New(x As String, y As String, z As String)
Me.X = x
Me.Y = y
Me.Z = z
End Sub
End Class
Your second class ought to look like this:
Imports System.Collections.ObjectModel
Public Class ParameterSetCollection
Inherits Collection(Of ParameterSet)
Public Overloads Sub Add(x As String, y As String, z As String)
Add(New ParameterSet(x, y, z))
End Sub
End Class
You might even want to do this:
Imports System.Collections.ObjectModel
Public Class ParameterSetCollection
Inherits Collection(Of ParameterSet)
Public Overloads Function Add(x As String, y As String, z As String) As ParameterSet
Dim item = New ParameterSet(x, y, z)
Add(item)
Return item
End Function
End Class
The Collection(Of T) class already provides all the standard collection functionality and you can extend it as required.

Problems with extending s.ds.am.UserPrincipal class

I have been trying to extend the s.ds.am.UserPrincipal class in VS 2010 VB app as I require access to AD User properties that are not by default a part of the UserPrincipal. This is a new concept to me and I have found some useful code on here as well but I have run into 2 problems with my extensions that I cannot seem to figure out.
Problem 1: When I attempt to use my UserPrincipalEx to retrieve a user I receive the following error.
ERROR:
Principal objects of type MyApp.UserPrincipalEx can not be used in a query against this store
CODE TO PRODUCE ERROR:
Dim ctx As New PrincipalContext(ContextType.Domain, DomainController)
Dim user As UserPrincipalEx = UserPrincipalEx.FindByIdentity(ctx, samAccountName)
Problem 2: When I attempt to create a new user with my UserPrincipalEx I receive the following error. When I trace the PrincipalContext (ctx) into the class it appears to disconnect from the DomainController.
ERROR:
Unknown error (0x80005000)
CODE TO PRODUCE ERROR:
Dim ctx As New PrincipalContext(ContextType.Domain, DomainController, DirectoryOU)
Dim NewADUser As New UserPrincipalEx(ctx, newsamAccountName, newPassword, True)
CLASS:
Imports System.DirectoryServices.AccountManagement
Public Class UserPrincipalEx
Inherits UserPrincipal
Public Sub New(ByVal context As PrincipalContext)
MyBase.New(context)
End Sub
Public Sub New(ByVal context As PrincipalContext, ByVal samAccountName As String, ByVal password As String, ByVal enabled As Boolean)
MyBase.New(context, samAccountName, password, enabled)
End Sub
<DirectoryProperty("Title")> _
Public Property Title() As String
Get
If ExtensionGet("title").Length <> 1 Then
Return Nothing
End If
Return DirectCast(ExtensionGet("title")(0), String)
End Get
Set(ByVal value As String)
Me.ExtensionSet("title", value)
End Set
End Property
<DirectoryProperty("physicalDeliveryOfficeName")> _
Public Property physicalDeliveryOfficeName() As String
Get
If ExtensionGet("physicalDeliveryOfficeName").Length <> 1 Then
Return Nothing
End If
Return DirectCast(ExtensionGet("physicalDeliveryOfficeName")(0), String)
End Get
Set(ByVal value As String)
Me.ExtensionSet("physicalDeliveryOfficeName", value)
End Set
End Property
<DirectoryProperty("Company")> _
Public Property Company() As String
Get
If ExtensionGet("company").Length <> 1 Then
Return Nothing
End If
Return DirectCast(ExtensionGet("company")(0), String)
End Get
Set(ByVal value As String)
Me.ExtensionSet("company", value)
End Set
End Property
' Implement the overloaded search method FindByIdentity.
Public Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityValue As String) As UserPrincipalEx
Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx)
End Function
' Implement the overloaded search method FindByIdentity.
Public Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal identityType As IdentityType, ByVal identityValue As String) As UserPrincipalEx
Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx)
End Function
End Class
PROBLEM 1
I was able to solve Problem 1 by adding the following to the top of the class after the Imports
<DirectoryRdnPrefix("CN")> _
<DirectoryObjectClass("user")> _
PROBLEM 2
I have also been banging my head against this for to long and was finally able to resolve Problem 2. The DirectoryOU variable did not contain a correct path the intended AD OU. I was missing a comma and after staring at everything for 30 minutes I finally noticed the issue.

VB.NET CType: How do I use CType to change an object variable "obj" to my custom class that I reference using a string variable like obj.GetType.Name?

The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.
Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType.
I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.
When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i)
I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"
Thanks for your help.
Rick
'+++++++++++++++++++++++++++++++
Imports DataLaasXC.Business
Imports DataLaasXC.Utilities
Public Class ucCustomerList
'Here is the calling method:
Public Sub CallingSub()
Dim customerList As New XCCustomers()
Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")
'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
Dim employeeList As New XCEmployees()
Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")
'doing stuff here...
End Sub
'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)
Dim fullyQualifiedClassName As String = obj.GetType.FullName
Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))
If (count > 0) Then
For i As Integer = 0 To count - 1
'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))
selectedItems.Add(Item)
Next
End If
Return selectedItems
End Function
End Class
'+++++++++++++++++++++++++++++++
' Below are the supporting classes if you need to see what else is happening:
Namespace DataLaasXC.Utilities
Public Class IntStringPair
Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
Value = _Value
Key = _Key
End Sub
Public Property Value As String
Public Property Key As Integer
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomer
Public Property CustomerId As Integer
Public Property CustomerName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomers
Inherits List(Of XCCustomer)
Public Sub New()
PopulateCustomersFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployee
Public Property EmployeeId As Integer
Public Property EmployeeName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployees
Inherits List(Of XCEmployee)
Public Sub New()
PopulateEmployeesFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
From MSDN
CType(expression, typename)
. . .
typename : Any expression that is legal
within an As clause in a Dim
statement, that is, the name of any
data type, object, structure, class,
or interface.
This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.
You can't change this at runtime.
Hope this helps.
Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:
Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
And then your troublesome line would become (with also using the property name parameters):
Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))
Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.
ctype is used to convert in object type.

Custom ASP.NET MVC2 HtmlHelper: How to get the instance of the object passed to it?

I am working on a ASP.NET MVC2 website. For learning. I am working with the Visual Basic language.
In a view I would like to do this:
<%: Html.EditorForEnumeracion(Function(v) v.combustible%>
So, I created an HtmlHelper extension method:
Imports System.Runtime.CompilerServices
Imports System.Linq.Expressions
Imports System.Reflection
Public Module HtmlHelpers
<Extension()> _
Public Function EditorForEnumeracion(Of TModel, TValue)(ByVal html As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of TModel, TValue))) As MvcHtmlString
'My stuff goes here
End Function
End Module
The problem is that I do not know how to get the instance of the v.combustible object that I pass to the helper. I do not care about the v object. I need to work with the combustible attribute of the v object.
Here and here seems to be how to do this, but I do not understand. Also, I work with Visual Basic, not C#.
I think that I can get the instance of the Enumeracion object through the expression parameter, but I do not understand how.
More info now.
This is my "Vehiculo" class:
Namespace Models.Automovil
Public Class Vehiculo
Public Property tipo As New Models.Enumeracion("TipoDeVehiculo")
Public Property marca As String
Public Property modelo As String
Public Property numeroDePuertas As Integer
Public Property combustible As New Models.Enumeracion("TipoDeCombustible")
Public Property potencia As Integer
Public Property fechaPrimeraMatriculacion As DateTime
Public Property version As String
Public Property precio As Decimal
Public Property descripcion As String
End Class
End Namespace
And this is my "Enumeracion" class:
Namespace Models
Public Class Enumeracion
Private bd As New tarificadorasegasaEntities
Private diccionario As New Dictionary(Of String, Integer)
Private _nombre As String
Private _clave As String
Private _valor As Integer
Public ReadOnly Property nombre As String
Get
Return _nombre
End Get
End Property
Public ReadOnly Property clave As String
Get
Return _clave
End Get
End Property
Public ReadOnly Property valor As Integer
Get
Return _valor
End Get
End Property
'More stuff here. Methods.
End Class
End Namespace
The model is the Vehiculo class.
Still not resolving this.
Thanks in advance.
You need to compile the expression into a Func(Of TModel, TValue), then call it on the model:
Dim func = expression.Compile()
Dim value = func(html.ViewData.Model)
Try like this:
<Extension()> _
Public Function EditorForMyCustomClassB(Of Vehiculo, Enumeracion)(ByVal html As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of Vehiculo, Enumeracion))) As MvcHtmlString
Dim res = ModelMetadata.FromLambdaExpression(expression, html.ViewData)
Dim e As Enumeracion = DirectCast(res.Model, Enumeracion)
' use e here
End Function

Calling Subroutines from lambda in vb.net

I find myself calling functions from lambdas frequently as the provided delegate does not match or does not have sufficient parameters. It is irritating that I cannot do lambda on subroutines. Each time I want to do this I have to wrap my subroutine in a function which returns nothing. Not pretty, but it works.
Is there another way of doing this that makes this smoother/prettier?
I have read that this whole lambda inadequacy will probably be fixed in VS2010/VB10 so my question is more out of curiosity.
A simple Example:
Public Class ProcessingClass
Public Delegate Sub ProcessData(ByVal index As Integer)
Public Function ProcessList(ByVal processData As ProcessData)
' for each in some list processData(index) or whatever'
End Function
End Class
Public Class Main
Private Sub ProcessingSub(ByVal index As Integer, _
ByRef result As Integer)
' (...) My custom processing '
End Sub
Private Function ProcessingFunction(ByVal index As Integer, _
ByRef result As Integer) As Object
ProcessingSub(index, result)
Return Nothing
End Function
Public Sub Main()
Dim processingClass As New ProcessingClass
Dim result As Integer
' The following throws a compiler error as '
' ProcessingSub does not produce a value'
processingClass.ProcessList( _
Function(index As Integer) ProcessingSub(index, result))
' The following is the workaround that'
' I find myself using too frequently.'
processingClass.ProcessList( _
Function(index As Integer) ProcessingFunction(index, result))
End Sub
End Class
If you find that you are doing it too often and generally with the same type of data, you can wrap the delegate in a class.
Create a base class that converts to the delegate:
Public MustInherit Class ProcessDataBase
Public Shared Widening Operator CType(operand As ProcessDataBase) as ProcessingClass.ProcessData
Return AddressOf operand.Process
End Sub
Protected MustOverride Sub Process(index As Integer)
End Class
Inherit from the class:
Public Class ProcessResult
Inherits ProcessDataBase
Public Result As Integer
Protected Overrides Sub Process(index as Integer)
' Your processing, result is modified.
End SUb
End Class
Use it:
Public Class Main()
Public Sub Main()
Dim processingClass As New ProcessingClass
Dim processor As New ProcessResult
processingClass.ProcessList(processor)
Dim result as integer=processor.Result
End Sub
End Class
It IS fixed in VB10, the VS10 Beta is available, if it's an option for you to use it. In VB10 you have lambdas without a return value, and inline subs/functions.
For now, maybe you could just forget lambdas and work with delegates instead? Something like:
processingClass.ProcessList(AddressOf ProcessingSub)