Can't provide constructor parameters

J

John Granade

It seems like this should be easy but I must be missing something...I'm
trying to create a web service class that accepts is instantiated with
parameters but I always get just the new() option (parameterless). Here's a
very simple example of what I've tried.

------Web Service----------
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
Private _text As String

Protected Sub New()
End Sub

Public Sub New(ByVal Text As String)
_text = Text
End Sub

<WebMethod()> _
Public Function GetText() As String
Return _text
End Function
End Class
---------------------------

I added the Protected New sub because I read that you must have one
parameterless constuctor to create a serializable class and it certainly
gives an error if you don't. The above code looks simple enough to me but
when I try to call it in my client it doesn't show a class that can be
created with parameters.

If I add a reference to the web service called 'ws', then in my code when I
try to do somethign like "dim s as New ws.Service1("text")" is says "too
many arguments to 'Public Sub New()' (which I knew because Intellisense
doesn't show any parameters when I typed ws.Service1). But why does it even
see 'Public Sub New()' when I set it to Protected and why doesn't it see
'public sub new (by val text as string)'?

I've been Googling it to death and I can't find anything on this. I see
examples that seem to instantial the web methods with parameters but not
working code. The code above is from a brand new web service project and a
windows client calling it so I don't know how to simplify it any more.

Thanks for any help.

John
John<nospam>@GTSolutions.us
 
D

Dave Bush

The Web Service isn't REALLY a class. .NET just uses the class mechanism
to make it easy for us to create them. But the web service standard has
no knowledge of what we are doing and is really just letting us call the
individual methods in them. You'll notice there isn't any ability to call
a property either.

What we create as an object on the client side is just a proxy to call the
web service. All the web service or the client knows about really are the
methods in our class that we've marked with the "WebMethod" attribute and
are public.

Dave Bush
http://blog.dmbcllc.com
 
J

John Granade

Thanks for the insight. I still feel like I'm missing something simple.
What I really have is a web service that receives an arraylist and I'm
wanting to load that arraylist with my class object. For instance, if my
class just defined two properties (PropertyName and PropertyValue) I was
hoping I could do something like this on the client end...

arraylist.add( new webservice.myClassInfo("text", "value"))
arraylist.add(new webserive.myClassInfo("text2", "value2"))

But since my custom class that's defined from the web service can't take
parameters, I have to do...

dim x as new webservice.myClassInfo
x.PropertyName = "text"
x.PropertyValue = "value"
arraylist.add(x)

dim y as new webservice.myClassInfo
y.PropertyName = "text2"
y.PropertyValue = "value2"
arraylist.add(y)

It still works but it's a lot more lines of code but I'll just do this for
now and refactor later. It's going to bug me though... :)

Thanks,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top