Solved

Using non empty parameter Class constructor in Webservice

Posted on 2010-11-14
20
652 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:tuchfeld
  • 9
  • 9
20 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34131165
You should be able to do this

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

Author Comment

by:tuchfeld
ID: 34131189
for some reason I can't.
I get an error:
Too many arguments to 'Public Sub New()'.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34131395
That is very strange. Is it public?
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:tuchfeld
ID: 34131424
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34131869
0
 

Author Comment

by:tuchfeld
ID: 34133899
I looked at the links.
Can You explain.. Can it be done anyway in the way I wanted?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34134467
The workaround is explained in the link above.
0
 

Author Comment

by:tuchfeld
ID: 34141767
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?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34143222
>Class passed in a WS can have only an Empty constructor?
That appears to be the case.
0
 

Author Closing Comment

by:tuchfeld
ID: 34153064
not much expert in kind of basic knowledge about this issue.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153475
Did you want me to design a compiler for you which would allow parameterised constructors?
0
 

Author Comment

by:tuchfeld
ID: 34153533
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153575
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."
0
 

Author Comment

by:tuchfeld
ID: 34153637
OK. Thanks. if You want me, I'll update the grade.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153652
That would be helpful.
0
 

Author Comment

by:tuchfeld
ID: 34153694
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34153756
Use the Request Attention button to ask a moderator to change the grade.
0
 

Author Comment

by:tuchfeld
ID: 34153773
OK.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question