Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Workshop

The Workshop is designed to help you review what you've learned in this hour and to point you ahead to the material that will be covered in future hours.

Quiz

  1. You need to return a value between 0 and 20. What data type should return?

    1. Byte

  2. You need to alter the variables being passed to your method. How do you declare them?

    1. ByRef in Visual Basic. Ref in C#

  3. What return type would you use when declaring a method that lets a client application know if a given pH is Acid, Base, or Neutral?

    1. You would use an Enumeration (possibly called pH) and give it Acid, Base, and Neutral as possible values.

  4. What do you place after the return statement if you are trying to return the following array of integers: myArray(3)?

    1. You would return myArray.

  5. You have a class called myClient of type Client and it contains two public attributes. What do you declare as your return type?

    1. Client

  6. In Question 5, what do you place after the return statement?

    1. myClient

Exercises

Try experimenting with the different data types that you learned about in this hour. Write methods that accept different methods as parameters as well as those that return them. Do as many as it takes to make you feel comfortable with passing data back and forth between your XML Web service and client applications.

#Region "Simple Data Types"

  'Returning a String
  <WebMethod()> Public Function StringReturn() As String
    Return "This is a string"
  End Function

  'Return a Byte (0 to 255)
  <WebMethod()> Public Function ByteReturn() As Byte
    Return 222
  End Function

  'Return Boolean (true or false)
  <WebMethod()> Public Function BoolReturn() As Boolean
    Return True
  End Function

  'Return Short or Int16
  <WebMethod()> Public Function ShortReturn() As Short
    Return 23344
  End Function

  'Return Integer or Int32
  <WebMethod()> Public Function IntReturn() As Integer
    Return -23343444
  End Function

  'Return Long or Int64
  <WebMethod()> Public Function LongReturn() As Long
    Return 23344444444444444
  End Function

  'Return Single
  <WebMethod()> Public Function SingleReturn() As Single
    Return 233400000000000
  End Function

  'Return Single
  <WebMethod()> Public Function DoubleReturn() As Double
    Return 3444344400000000000
  End Function

#End Region

#Region "Returning Enums"

  'Declaring an Enum
  Public Enum Color
    Red = 1
    Blue = 2
    Green = 3
  End Enum

  'Returning an Enum
  <WebMethod()> Public Function EnumReturn() As Color
    Return Color.Red
  End Function

#End Region

#Region "Returning Arrays"

  'Returning an Array
  <WebMethod()> Public Function ArrayReturn() As Integer()
    Dim a(4) As Integer
    Dim i As Integer

    For i = 0 To 4
      a(i) = i + 1
    Next

    Return a
  End Function

#End Region

#Region "Returning Classes"

  'Declaring a Class
  Public Class Person
    Public Name As String
    Public ID As Integer
  End Class

  'Returning a Class
  <WebMethod()> Public Function ClassReturn() As Person

    Dim oPerson As New Person()

    oPerson.Name = "Jim Smith"
    oPerson.ID = 5

    Return oPerson
  End Function

  'Returning an Array of Classes
  <WebMethod()> Public Function ClassesReturn() As Person()

    Dim oPerson(2) As Person

    oPerson(0) = New Person()
    oPerson(0).Name = "Jim Smith"
    oPerson(0).ID = 1
    oPerson(1) = New Person()
    oPerson(1).Name = "Edgar Smith"
    oPerson(1).ID = 2
    oPerson(2) = New Person()
    oPerson(2).Name = "Mary Smith"
    oPerson(2).ID = 3

    Return oPerson
  End Function

#End Region

#Region "Params"

  'Accepting a String as a param
  <WebMethod()> Public Function ParamStringReturn( _
          ByVal sWord1 As String, _
          ByVal sWord2 As String) As String

    Return sWord1 & " and " & sWord2
  End Function

  'Accepting a Long as a param
  <WebMethod()> Public Function ParamLongReturn( _
           ByVal iNum1 As Long, _
           ByVal iNum2 As Long) As Long

    Return iNum2 - iNum1
  End Function

  'New Enum
  Public Enum Champion
    Corum = 1
    Renark = 2
    Urlich = 3
    Hawkmoon = 4
  End Enum

  'Accepting a Enum as a Parameter
  <WebMethod()> Public Function ParamEnumReturn( _
           ByVal enumName As Champion) As champion

    Return enumName
  End Function

  'Accepting an Array as a Param
  <WebMethod()> Public Function ParamArrayReturn( _
           ByVal iName() As Integer) As Integer

    Return UBound(iName)
  End Function
#End Region

#Region "ByRefParams"

   'Accepting Integer by Reference
  <WebMethod()> Public Function RefReturnReturn( _
           ByRef iVal As Integer) As Integer

    Dim iOld As Integer

    iOld = iVal
    iVal = iVal - 1
    Return iOld
  End Function

   'Accepting Classes by Reference
  <WebMethod()> Public Sub RefClassReturn( _
           ByRef oPers As Person)

    oPers.ID = oPers.ID - 1
    oPers.Name = "Hi " & oPers.Name
  End Sub
#End Region

  • + Share This
  • 🔖 Save To Your Account