I need to consume a web service in VS 2010 using VB.NET that gives a result of a class type.

I've already added the reference and called it with:

Dim mag As New Magento.ecommerce

I can create a variable as the class:

Dim Stockcodes As New List(Of Magento.clsStockcode)

I just don't understand now how to make my Stockcodes variable get the data from the Web Service.

I've tried variations of:

Stockcodes = mag.ReturnNewStockodes("admin", "password")


Stockcodes.Add(New Stockcodes(mag.ReturnNewStockodes("admin", "password"))

but really I'm not sure what I'm doing.

I'm guessing what I need is the result of the Web Service as a type of dataset but I'm not sure.

The class is:


Recommended Answers

All 6 Replies

Do you have access to the source code of the class? Is the function ReturnNewStockodes public in the class? What should the ReturnNewStockodes return i.e. a single clsStockcode or a list of them?

Stockcodes = mag.ReturnNewStockodes("admin", "password")

Would imply a list of clsStockcodes where if you only expected a single one you would do something like this:

Stockcodes.Add(mag.ReturnNewStockodes("admin", "password"))

Also while I was typing I noticed you had used ReturnNewStockodes not ReturnNewStockCodes - just in case there is a typo...


Yes, the class returns a list of stockcodes.

I made the typo when I was formatting the code for the question :)

I've taken out the main parts of the function here:

<WebMethod()> _
    Public Function ReturnNewStockodesByVal UserName As String, _
                                       ByVal Password As String) As List(Of clsStockcode)

            Dim Stockcodes As New List(Of clsStockcode)

            For Each dr As DataRow In Ds.Tables(0).Rows

                RecordsReturned += 1
                Stockcodes.Add(New clsStockcode(dr("generated_code") & "", dr("description") & "", dr("cost_price") & "", dr("selling_price_1") & "", _
                                                dr("sales_tax_group") & "", dr("qty_on_hand") & "", dr("is_blocked") & ""))

            Return Stockcodes

End Function

And the class for stockcodes:

Imports Microsoft.VisualBasic

Public Class clsStockcode

Public Property _Stockcode As String
Public Property _Description As String
Public Property _CostPrice As String
Public Property _PriceExclusive As String
Public Property _PriceInclusive As String
Public Property _TaxClass As String
Public Property _QtyOnHand As String
Public Property _IsBlocked As String

Public Sub New(ByVal Stockcode As String, ByVal Description As String, ByVal CostPrice As String, ByVal PriceExclusive As String,
               ByVal TaxClass As String, ByVal QtyOnHand As String, ByVal IsBlocked As String)

    _Stockcode = Stockcode
    _Description = Description
    _CostPrice = CostPrice
    _PriceExclusive = PriceExclusive
    _TaxClass = TaxClass
    _QtyOnHand = QtyOnHand
    _IsBlocked = IsBlocked

End Sub

Public Sub New()

End Sub


If it returns a list, you should be able to navigate through it:

Dim Stockcodes As New List(Of magento.clsStockcode)
dim Stockcode as magento.clsStockcode
dim code as string

'Get your stockcodes
If Stockcodes.items.count > 0 then 'I think it's items.count, could just be .count
    For each Stockcode in StockCodes
        'work with the stockcode e.g.
        code = Stockcode._StockCode
end if


Thanks for the help so far!

The major thing that I'm not understanding is how to retrieve the list from the web service into a variable.


Does this not work?

Dim mag As New Magento.ecommerce
Dim Stockcodes As New List(Of magento.clsStockcode) 
Stockcodes = mag.ReturnNewStockodes("admin", "password")

If not, try replacing magento.clsStockcode with mag.clsStockcode

Thanks for you help again!

I iterated through the returned list and all works perfectly now.

Dim mag As New Magento.ecommerce

        If TypeOf mag.ReturnNewStockodes("admin", "password") Is IEnumerable Then

            For Each o As Object In TryCast(mag.ReturnNewStockodes("admin", "password"), IEnumerable)
                Console.WriteLine(DirectCast(o, WindowsApplication1.Magento.clsStockcode)._Stockcode)
            For Each p As System.Reflection.PropertyInfo In mag.ReturnNewStockodes("admin", "password").GetType().GetProperties()
                If p.CanRead Then
                    Console.WriteLine("{0}: {1}", p.Name, p.GetValue(mag.ReturnNewStockodes("admin", "password"), Nothing))  'possible function
                End If
        End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.