how to make a boolean default to True in web service

C

cj2

In the code below I want when this web service is first published for
the boolean Running.running to be True. It seems to default to false.
How can I make this default to True? Once the web service is running
and I call it with "start" it works fine as coded. I just don't want
to have to call it with start the first time.

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.Odbc

' To allow this Web Service to be called from script, using ASP.NET
AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
_
<ToolboxItem(False)> _
Public Class Running
Public Shared running As Boolean
End Class

Public Class CashOnly
Inherits System.Web.Services.WebService

Dim result As String

<WebMethod()> _
Public Function CashOnly(ByVal act As String) As String
If act = "stop" Then
Running.running = False
result = "stopped"
ElseIf act = "start" Then
Running.running = True
result = "started"
elseIf IsNumeric(act) And act.Trim.Length = 10 Then
If Running.running Then
Dim myDs As New DataSet
Dim myOdbcConnection As New
OdbcConnection("Driver={Microsoft Visual FoxPro Driver};" + _
"SourceType=DBF;" + _
"SourceDB=c:;" + _
"Exclusive=No;" + _
"Collate=Machine;" + _
"NULL=NO;" + _
"DELETED=NO;" + _
"BACKGROUNDFETCH=NO")
Dim myOdbcCommand As New OdbcCommand("select flag from
\\fileserver\i\probill\act_frau.DBF where act = '" + act.Trim + "'",
myOdbcConnection)
myOdbcConnection.Open()
result = myOdbcCommand.ExecuteScalar.ToString
myOdbcConnection.Close()
Else
result = "Try again later"
End If
Else
result = "Invalid request"
End If


Return result
End Function

End Class
 
S

Steven Cheng [MSFT]

Hi Cj,

From your description, you are wantting to set some initial value for a
shared/static variable of your webservice class , correct?

In .net, there are several ways you can initialize a static/shared class
member:

1. Class can have a static constructor which can help you do some
initialize tasks for the entire class(not specific to an particular
instance).

#9.3.2 Shared Constructors
http://msdn.microsoft.com/en-us/library/aa711965(VS.71).aspx

e.g.
==============
Class YourClass
Private Shared running as Boolean

Shared Sub New()
running = True
End Sub

..................


End Class
===================

2. Or you can directly assignt the initial value at the variable's
declaration place. e.g.

===================
Class YourClass
Private Shared running as Boolean = True

...................
=====================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
C

cj2

Steven,

I had tried your second example but it didn't work--it does now. I must
have had something else wrong at that time too. Thanks for confirming
that that should work in web apps too.
 
S

Steven Cheng [MSFT]

You're welcome Cj,

I'm glad that it works for you.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 

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

Top