0

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)
StealthRT
  • 10,108
  • 40
  • 183
  • 342

1 Answers1

1

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)
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Your first example does not seem to work. on the code **svc.avIP = "0.0.0.0"** it tells me **'avIP is not a member of 'Service'** – StealthRT Oct 06 '12 at 22:17
  • The first example is based on the unmodified version of the Service class defined in your question. That class has a *Public avIP*, so it should be something in the Service class declaration. – Steve Oct 06 '12 at 22:29
  • Your update makes things worse. You don't need to create inside the Service class an instance of the Service class. You need to create an instance of the Service class outside the Service class and call the avIP property using that instance, or change the lifetime of avIP to Shared. Looks at this [question and relative answers](http://stackoverflow.com/questions/613998/what-is-the-use-of-shared-variable-in-vb-net) – Steve Oct 07 '12 at 08:20