which way to return struc from webservice?

C

cj2

Which function shows the proper way to return this structure
Validate, Validate2, or Validate3? And a short why would be nice.

Public Class Validate
Inherits System.Web.Services.WebService

Public Structure ValResult
Dim mResponse As String
Dim mReason As String
End Structure

<WebMethod()> _
Public Function Validate(ByVal ibtn As String, ByVal irequestor As
String)
Dim thisResult As New ValResult
thisResult.mResponse = "ALLOW"
thisResult.mReason = "Good"
Return thisResult
End Function

<WebMethod()> _
Public Function Validate2(ByVal ibtn As String, ByVal
irequestor As String) As ValResult
Validate2.mResponse = "ALLOW"
Validate2.mReason = "Good"
Return Validate2
End Function

<WebMethod()> _
Public Function Validate3(ByVal ibtn As String, ByVal irequestor As
String) As ValResult
Dim thisResult As New ValResult
thisResult.mResponse = "ALLOW"
thisResult.mReason = "Good"
Return thisResult
End Function
End Class
 
F

Family Tree Mike

cj2 said:
Which function shows the proper way to return this structure
Validate, Validate2, or Validate3? And a short why would be nice.

Public Class Validate
Inherits System.Web.Services.WebService

Public Structure ValResult
Dim mResponse As String
Dim mReason As String
End Structure

<WebMethod()> _
Public Function Validate(ByVal ibtn As String, ByVal irequestor As
String)
Dim thisResult As New ValResult
thisResult.mResponse = "ALLOW"
thisResult.mReason = "Good"
Return thisResult
End Function

<WebMethod()> _
Public Function Validate2(ByVal ibtn As String, ByVal irequestor
As String) As ValResult
Validate2.mResponse = "ALLOW"
Validate2.mReason = "Good"
Return Validate2
End Function

<WebMethod()> _
Public Function Validate3(ByVal ibtn As String, ByVal irequestor As
String) As ValResult
Dim thisResult As New ValResult
thisResult.mResponse = "ALLOW"
thisResult.mReason = "Good"
Return thisResult
End Function
End Class


Validate3 is my choice, because I hate the other two.

Validate does not convey a return type. Validate2 does the work in a very
vb6 way in that the function name is treated as the return variable.
 
S

Steven Cheng

Hi Cj,

IMO, I would prefer the 3rd one as it use the most standard signature and
it will cause less confusion when things get complicated.

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).


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

cj2

Ok, if that is the standard way of doing things I'll go with it. But
I'm kinda wondering why #2 isn't more correct. I've run all 3 and
examined the values as they run and I can see why #1 isn't good. But
since I define the function Validate2 as type valresult then it would
seem that validate2 is what I should set to the values. In #3 I see
that valresult3 gets set to the values of thisresult when the return
statement is executed.
 
J

J.B. Moreno

cj2 said:
Which function shows the proper way to return this structure
Validate, Validate2, or Validate3? And a short why would be nice.


<WebMethod()> _
Public Function Validate(ByVal ibtn As String, ByVal irequestor As
String)
-snip
No return type -- does this even compile?
<WebMethod()> _
Public Function Validate2(ByVal ibtn As String, ByVal
irequestor As String) As ValResult
Validate2.mResponse = "ALLOW"


Using the function return value directly without initializing it, if
you ever refactor the program and turn ValResult into a class, you've
just created an exception.
<WebMethod()> _
Public Function Validate3(ByVal ibtn As String, ByVal irequestor As
String) As ValResult
Dim thisResult As New ValResult
thisResult.mResponse = "ALLOW"
thisResult.mReason = "Good"
Return thisResult
End Function
End Class

Of the available options, this one is best. But there are other issues,
I know that they aren't what you were asking about, but....

You should be spelling out whatever "Val" stands for (abbreviations of
any sort should not be your first choice), you should not be using the
prefix m for an element in a structure, and although it's probably just
because it's an example, you shouldn't be hard-coding "ALLOW" and
"Good" (they should be either public constants or enums depending upon
your actual usage).
 
J

J.B. Moreno

cj2 said:
Ok, if that is the standard way of doing things I'll go with it. But
I'm kinda wondering why #2 isn't more correct. I've run all 3 and
examined the values as they run and I can see why #1 isn't good. But
since I define the function Validate2 as type valresult then it would
seem that validate2 is what I should set to the values. -snip-

Recursion and bugs. Recursion isn't really applicable given the
signature, but as I said in another post, not declaring a return
variable and initializing it, will result in an exception when
returning a reference type.
 
C

cj2

J.B. Moreno said:
-snip
No return type -- does this even compile?

yup, and it works, although the xml it returns looks a bit funny.
Using the function return value directly without initializing it, if
you ever refactor the program and turn ValResult into a class, you've
just created an exception.
Ok.


Of the available options, this one is best. But there are other issues,
I know that they aren't what you were asking about, but....

You should be spelling out whatever "Val" stands for (abbreviations of
any sort should not be your first choice), you should not be using the
prefix m for an element in a structure, and although it's probably just
because it's an example, you shouldn't be hard-coding "ALLOW" and
"Good" (they should be either public constants or enums depending upon
your actual usage).
as you said it's just an example.
 
C

cj2

Thanks

J.B. Moreno said:
Recursion and bugs. Recursion isn't really applicable given the
signature, but as I said in another post, not declaring a return
variable and initializing it, will result in an exception when
returning a reference type.
 

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