Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Webservice array initializer 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
Hi I'm successfully making a Webservice request with the inclusion of the statements below.

Code:
        request.RequestedShipment.FreightShipmentDetail.LineItems = New FreightShipmentLineItem(0) {New FreightShipmentLineItem()}
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).FreightClass = FreightClassType.CLASS_050
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).FreightClassSpecified = True

        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Packaging = PhysicalPackagingType.SKID
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).PackagingSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Description = "Freight line item 0NE"
        '
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight = New Weight()
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.Units = WeightUnits.LB
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.UnitsSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.Value = (500)
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.ValueSpecified = True




I initialize the array using request.RequestedShipment.FreightShipmentDetail.LineItems = New FreightShipmentLineItem(0) {New FreightShipmentLineItem()}

Which works when there is only 1 line item/

I Can't seem to figure out how to initialize it to pass more line item (up to 5)
I plan on pulling the line items using a dataset and iterating through it to populate the array with multiple line items. The dataset is easy to do.

I've set the boundary to 4 = New FreightShipmentLineItem(3) [4 items on a zero based index] but then i get an error "array initializer missing 4 elements"



Thanks

-dan
 
I am assuming that FreightShipmentLineItem is an object you have created, correct?
If so, can you post the definition of that object?
 

JB

I downloaded a wsdl and made a proxy class using wsdl.exe

FreightShipmentLineItem is a class from with in. I'm not sure what you mean by its definition. I'm kind of new at this type request. The only thing I'm seeing is:

<xs:element name="LineItems" type="ns:FreightShipmentLineItem" minOccurs="0" maxOccurs="unbounded">

Code:
      <xs:complexType name="FreightShipmentLineItem">
        <xs:annotation>
          <xs:documentation>Description of an individual commodity or class of content in a shipment.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
          <xs:element name="FreightClass" type="ns:FreightClassType" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Freight class for this line item.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="Packaging" type="ns:PhysicalPackagingType" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Specification of handling-unit packaging for this commodity or class line.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="Pieces" type="xs:nonNegativeInteger" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Number of pieces for this commodity or class line.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="Description" type="xs:string" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Customer-provided description for this commodity or class line.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="Weight" type="ns:Weight" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Weight for this commodity or class line.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element name="Dimensions" type="ns:Dimensions" minOccurs="0"/>
          <xs:element name="Volume" type="ns:Volume" minOccurs="0">
            <xs:annotation>
              <xs:documentation>Volume (cubic measure) for this commodity or class line.</xs:documentation>
            </xs:annotation>
          </xs:element>
        </xs:sequence>
      </xs:complexType>

The XSD file is here if it will help. Just search for "Lineitems" Right below the lineitems it the XML for ns:FreightShipmentLineItem


Link
 
Well
FreightShipmentDetail and
FreightShipmentLineItem

must be objects, because you are newing them up.. correct? Each of those has a definition, a class file that defines them.(Properties, methods, events)

I am assuming the FreightShipmentDetail object has a property called LineItems, which is a colletion of FreightShipmentLineItem(s), correct?

 
Correct :)

Got this from the object browser.

Private lineItemsField() As RateWebServiceClient.RateWebReference.FreightShipmentLineItem
Member of RateWebServiceClient.RateWebReference.FreightShipmentDetail


Also: If I set the array boundy to 1 and modify the top line above to read

Code:
request.RequestedShipment.FreightShipmentDetail.LineItems = New FreightShipmentLineItem(1) {New FreightShipmentLineItem(), New FreightShipmentLineItem()}


The array actually picks stores (success!) the second line item. I'll know how many line items there will be based on a returned dataset row count. There has to be a way to initialize the array without having a bunch of elseif rowcaount = statements.


Code:
  request.RequestedShipment.FreightShipmentDetail.LineItems = New FreightShipmentLineItem(1) {New FreightShipmentLineItem(), New FreightShipmentLineItem()}


        request.RequestedShipment.FreightShipmentDetail.LineItems(0).FreightClass = FreightClassType.CLASS_050
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).FreightClassSpecified = True

        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Packaging = PhysicalPackagingType.SKID
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).PackagingSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Description = "Freight line item 0NE"
        '
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight = New Weight()
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.Units = WeightUnits.LB
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.UnitsSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.Value = (500)
        request.RequestedShipment.FreightShipmentDetail.LineItems(0).Weight.ValueSpecified = True



        request.RequestedShipment.FreightShipmentDetail.LineItems(1) = New FreightShipmentLineItem
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).FreightClass = FreightClassType.CLASS_065
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).FreightClassSpecified = True
        '
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Packaging = PhysicalPackagingType.SKID
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).PackagingSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Description = "Freight line item two "
        '
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Weight = New Weight()
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Weight.Units = WeightUnits.LB
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Weight.UnitsSpecified = True
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Weight.Value = (650)
        request.RequestedShipment.FreightShipmentDetail.LineItems(1).Weight.ValueSpecified = True
 
Is there any reason you NEED to use an array? I would use a collection(or List) of the object you need to have. For example, will something like this work for you:
(I am writing this in VB, be we can convert it easily to C# if you need.
Code:
'Let's assume you only have one Detail item, but many LineItems
   Dim  shipmentDetail as New FreightShipmentDetail()
   Dim  lineItems as New List(Of FreightShipmentLineItem)
'Now  your loop.. you may need to change this..
   For Each dr as DataRow in dt.Rows  ''maybe this is a datatable of line items..  you need to adjust to your code
      Dim  lineItem as New FreightShipmentLineItem()
      lineItem.FreightClass = dr("FreightClass")
      lineItem.FreightClassSpecified = dr("FrieghtClassSpecified")
'''  continue for each property on the object.  You can also hard code values if you need
      lineItems.Add(lineItem)
   Next 
'All of your lineItems are now in a List, add that to the FreightShiptmentDetail object
   shipmentDetail.LineItems = lineItems
   shipmentDetail.anotherProperty = "Some Value"
''  ..etc
 
The array was presented in an example soap call from the fedex development center.
I sure give the List a tryout :). Might be a bit before I can test it but I'll post the results here. VB is fine with me as that's my primary & favorite language.

Thank you very much


-dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top