By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,892 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,892 IT Pros & Developers. It's quick & easy.

webservices: simple question, accessing webservice members

P: n/a
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice?
I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:
public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}
................................

So when I consume this webservice, I want to be able to call the sayHello()
method, and have access to the hello2 variable after having called sayHello
() and thus expecting the "hello2" variable to contain the "another hello
to you!" value:
remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;

Is this possible, and how do I go about doing it? thanks!
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I don't think you can access a public member of
System.Web.Services.WebService class from a client side. I would either
stored the value in database or use Session in the web service.

Good luck!

============================
Hayato Iriumi (hi*****@hotmail.com)
Blog: http://www.vbaspcoder.com
"hellrazor" <jo***@another-world.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice? I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:
public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}
...............................

So when I consume this webservice, I want to be able to call the sayHello() method, and have access to the hello2 variable after having called sayHello () and thus expecting the "hello2" variable to contain the "another hello
to you!" value:
remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;

Is this possible, and how do I go about doing it? thanks!

Nov 16 '05 #2

P: n/a
you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.

For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick

"hellrazor" <jo***@another-world.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice? I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:
public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}
...............................

So when I consume this webservice, I want to be able to call the sayHello() method, and have access to the hello2 variable after having called sayHello () and thus expecting the "hello2" variable to contain the "another hello
to you!" value:
remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;

Is this possible, and how do I go about doing it? thanks!

Nov 16 '05 #3

P: n/a
I screwed up a bit of that code.

Please replace the client class as follows:

public void MyClient()
{
Service1 mywebservice;
SharedClass myShared;
myShared = mywebservice.SayHello();
Console.WriteLine("{0}", myShared.MyHello1);
Console.WriteLine("{0}", myShared.MyHello2);
}

Sorry about that.

--- Nick

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:GKomd.513423$mD.491984@attbi_s02...
you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.
For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick

"hellrazor" <jo***@another-world.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a

webservice?
I want to access this variable, in addition to the data being returned by the [webmethod].

so let's say I have the following webservice class:
public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}
...............................

So when I consume this webservice, I want to be able to call the

sayHello()
method, and have access to the hello2 variable after having called

sayHello
() and thus expecting the "hello2" variable to contain the "another hello to you!" value:
remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;

Is this possible, and how do I go about doing it? thanks!


Nov 16 '05 #4

P: n/a
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in news:q9pmd.46404
$5K2.39317@attbi_s03:
I screwed up a bit of that code.

Please replace the client class as follows:

public void MyClient()
{
Service1 mywebservice;
SharedClass myShared;
myShared = mywebservice.SayHello();
Console.WriteLine("{0}", myShared.MyHello1);
Console.WriteLine("{0}", myShared.MyHello2);
}

Sorry about that.

--- Nick

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:GKomd.513423$mD.491984@attbi_s02...
you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.

For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick

"hellrazor" <jo***@another-world.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
> Hi, (new to webservices here)
>
> Is it possible to access a class instance variable defined in a

webservice?
> I want to access this variable, in addition to the data being
returned by > the [webmethod].
>
> so let's say I have the following webservice class:
>
>
> public class Service1 : System.Web.Services.WebService
> {
>
> public string hello2;
>
> public ModelServices()
> {
> InitializeComponent();
> }
>
> [WebMethod]
> public string sayHello()
> {
> hello2 = "another hello to you!"
> return "hello there";
> }
> }
>
>
> ...............................
>
> So when I consume this webservice, I want to be able to call the

sayHello()
> method, and have access to the hello2 variable after having called

sayHello
> () and thus expecting the "hello2" variable to contain the "another hello > to you!" value:
>
>
> remoteWS.Service1 ws = new remoteWS.Service1();
> string hello1 = ws.sayHello();
> string hello2 = ws.hello2;
>
>
>
> Is this possible, and how do I go about doing it? thanks!
>
>




Hi there, thanks!

When I try to compile the client application in VS, I get the following
error:

C:\clients\test.cs(1070): Cannot implicitly convert type
'WS.ReturnValuesClass' to 'MyApp.ReturnValuesClass'
I have placed the class in the source code for both the webservice and
the client. Is there anything specific involved that I have missed?

Thanks!

Jorge
Nov 16 '05 #5

P: n/a
<clip ...>
Hi there, thanks!

When I try to compile the client application in VS, I get the following
error:

C:\clients\test.cs(1070): Cannot implicitly convert type
'WS.ReturnValuesClass' to 'MyApp.ReturnValuesClass'
I have placed the class in the source code for both the webservice and
the client. Is there anything specific involved that I have missed?

Thanks!

Jorge


Hi Jorge,

That's what I get for not compiling the code first. My apologies.

The shared class should NOT be declared again in the client code. The
definition of the shared class is passed via the WSDL and is coded in the
web service generated code. You can simply declare a variable of type
WS.ReturnValuesClass and refer to the members that way.

If you want to see the code the Visual Studio generates for you when you
make a web reference, then look carefully at your project explorer window.
There are a couple of small icons in the bar just above the treeview of your
solution and project(s). One of the icons, if you hover over it, says
something like "show all files". Click it. Then look into the client
project. You will see a new code file under the web reference to the web
service. In there, you will see the definition of the shared class.

Good Luck,
--- Nick
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.