StealthRT
StealthRT

Reputation: 10552

web service sharing global variable

I am new at creating web services in ASP.net/VB.net. I am setting up a public variable like so inside my Service.vb in App_Code folder:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Diagnostics
Imports System.Web.Script.Services

<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService

    Public avIP As String = "0.0.0.0"
etc etc....

And now i have made another Class and i want to get the value of avIP. However, when i do this:

Client.Connect(Service.avIP, 60128)

It does not give me that value only an error. I do not seem to get any value if i do Service.. Nothing comes up in the list of suggestions.

How can i grab the value from Service.vb into my other class?

UPDATE

I have the following in the Service.vb file:

<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService
    Public svc As Service = New Service
    Dim avIp As String = "0.0.0.0"

And in the avReceiver.vb i have:

Client.Connect(svc.avIP, 60128)

Upvotes: 0

Views: 3318

Answers (1)

Steve
Steve

Reputation: 216358

To refer to a public variable declared in a class like yours, you need to create an instance of that class.
This means that every created instance of the class has its copy of the variable.
It is a very basic functionality on every object oriented language.

' Create a new instance of the Service class
Dim svc As Service = new Service()
' Set the value of a Public Property 
svc.avIP = "192.168.1.0"
' Use the instance value of that property
Client.Connect(svc.avIP, 60129)

' Create another instance of the Service class
Dim svc1 As Service = new Service()
' Set the value of a Public Property 
svc1.avIP = "192.168.1.1"
' Use the instance value of that property
Client.Connect(svc1.avIP, 60129)

If you want to use a property member of a class without declaring an instance of that class you need to declare that member as Shared (static in C#).
This means that every instance of that class will share the same variable (and its value of course).

Public Class Service   
     Inherits System.Web.Services.WebService   

     Public Shared avIP As String = "0.0.0.0"   
     ....
End Class

' Set the one and only avIP for every instance
Service.avIP = "192.168.1.0"
' Use directly the value
Client.Connect(Service.avIP, 60129)

' Create an instance of the Service class
Dim svc As Service = new Service()
' Pass the same value used directly with the class name (192.168.1.0)
Client.Connect(svc.avIP, 60129)

Upvotes: 1

Related Questions