Link to home
Start Free TrialLog in
Avatar of tuchfeld
tuchfeld

asked on

Using non empty parameter Class constructor in Webservice

Hi,
I'm using WS successfully,
but I wonder why I cannot have a non empty parameter constructor for a class
declared with the header:
    <AttributeUsage(AttributeTargets.All)> _
in other words: I cannot use:
    <AttributeUsage(AttributeTargets.All)> _
    Public Class UserInfo
        Public Sub New(ByVal id As Integer, ByVal first_name As String, ByVal last_name As String)
            Me.id = id
            Me.first_name = first_name
            Me.last_name = last_name
        End Sub
Unless I have an empty constructor:
        Public Sub New()
        End Sub
But then need at the other side to use like this:
        Dim ti(2) As My_WS. UserInfo
        ti(0) = New My_WS. UserInfo()
        ti(0).id = 1
        ti(0).first_name = "chaim"
        ti(0).last_name = "baony"
Which is inconvenient.
How can I use a non empty parameter constructor for WS Class?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You should be able to do this

ti(0) = New My_WS. UserInfo(1, "chaim", "baony")
Avatar of tuchfeld
tuchfeld

ASKER

for some reason I can't.
I get an error:
Too many arguments to 'Public Sub New()'.
That is very strange. Is it public?
Yes.
Notice that if I do not supply:
        Public Sub New()
        End Sub
it gives error:
cannot be serialized because it does not have a parameterless constructor.
I looked at the links.
Can You explain.. Can it be done anyway in the way I wanted?
The workaround is explained in the link above.
Yes - it says: "... you can try to use another method to initialize all the member fields for workaround."
Obviousely, this is a workaround.
So, Can You state that it cannot be done?
i.e.: Class passed in a WS can have only an Empty constructor?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not much expert in kind of basic knowledge about this issue.
Did you want me to design a compiler for you which would allow parameterised constructors?
Please don't be rude.
1) You wrote: "That appears to be the case"
Not much confident answer..
2) A short explanation for the Reason (why is it not allowed).
Would give A grade.
If you had bothered to read the links I pasted, you would have come across this explanation:

"Web services are intended to be loosely coupled.  Your web services exposes describes its operations and data types in WSDL and XML Schema documents that a client can use to generate a proxy for calling the web service.  The schema for the data types only preserves the structure of the data - it doesn't preserve aspects of your type that are specific to the .NET Framework.  For example, a Java client can call your web service just as easily as a .NET client.  Constructors are one of the aspects of a .NET type that are not relevant to the structure of the data and are not preserved in schema."
OK. Thanks. if You want me, I'll update the grade.
That would be helpful.
BTW. "If I had bothered to read..."
I'd buy WS book..
So I suggest that You should add some addiional info beside the link(s).
Anyway, where should I click to update the grade?
and Thanks.
Use the Request Attention button to ask a moderator to change the grade.
OK.