Solved

Using non empty parameter Class constructor in Webservice

Posted on 2010-11-14
20
649 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Showdialog 8 29
Help with Query not working in client's PC 1 35
vb.net checkbox 7 41
WCF Service Application cannot connect from TCP terminal 1 14
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now