I'm currently writing a basic VB ASP.NET web application that connects to a geolocation web service. The web service allows a user to enter as much information they know about an address, and then replies back with a suggestion list of possible locations
of the address. The user then sends the information back across, this time with a suggestion selected and it goes back and forth until a specific address is found. Below is a screenshot from soapUI showing an incomplete address: 510 Marketplace Blvd, Lansing
MI, 48917, and the suggestion list that the web service responds with. There are 10 suggestions in the list. (The correct address should be 510 NORTH Marketplace Blvd, Lansing MI, 48917 which is the address of a business called Tractor Supply.)
I've added the web service's WSDL as a service reference already. As far as I can tell, the suggestion list comes in as an array of classes. How do I take in this array of classes and make it usable? What I'm doing at the moment is dumping it out to a multiline
text box.
Here is a snippet of the code that I have written:
Dim mspGeocodeResponse As New MSPGeocode.GeocodeResponse
mspGeocodeResponse.Address = New MSPGeocode.ExtendedAddressType
mspGeocodeResponse.Address.Coordinates = New MSPGeocode.CoordinatesType
mspGeocodeResponse.Address.suggestionList(100) = New MSPGeocode.FLSuggestionType
mspGeocodeResponse = mspGeocode.Geocode(mspGeocodeRequest)
For i As Integer = mspGeocodeResponse.Address.suggestionList.GetLowerBound(0) _
To mspGeocodeResponse.Address.suggestionList.GetUpperBound(0)
resultsBuilder.Append("Suggestion ").Append(mspGeocodeResponse.Address.suggestionList(i).SuggestionNumber).AppendLine()
resultsBuilder.Append(vbTab).Append("Street Number Low: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetNumberLow).AppendLine()
resultsBuilder.Append(vbTab).Append("Street Number High: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetNumberHigh).AppendLine()
resultsBuilder.Append(vbTab).Append("Street Prefix: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetPrefix).AppendLine()
resultsBuilder.Append(vbTab).Append("Street Name: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetName).AppendLine()
resultsBuilder.Append(vbTab).Append("Street Suffix: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetSuffix).AppendLine()
Next
Here is the declaration of the suggestion list class within Reference.vb:
Partial Public Class FLSuggestionType
Inherits Object
Implements System.ComponentModel.INotifyPropertyChanged
Private suggestionNumberField As Integer
Private streetNumberLowField As String
Private streetNumberHighField As String
Private streetPrefixField As String
Private streetNameField As String
Private streetSuffixField As String
Private quadrantField As String
Private firmField As String
Private unitTypeField As String
Private unitNumberLowField As String
Private unitNumberHighField As String
And here is that class nested inside of the address class:
Partial Public Class ExtendedAddressType
Inherits Object
Implements System.ComponentModel.INotifyPropertyChanged
Private suggestionListField() As FLSuggestionType
Private streetPrefixField As String
When I run the application and type in the address "510 Marketplace Blvd, Lansing MI, 48917" to get the suggestion list back, I get an "Object reference not set to an instance of an object." error. Image of that below.
I assume my syntax is wrong somewhere, but after tons of research of trying different cominations of things, I can't seem to get it pull in the suggestion list. Does anyone have any suggestions?
P.S. Sorry for using Google Drive for the images. I would have used an imagehosting service and embedded them, but my workplace blocks all of those sites.
usually one does not have to initialize sub classes/objects, variables in a main class/object. Ofcourse it is hard to say for sure since you didn't mention what service you are using neither showes the source of the response object. I'm also sure that they
have some kind of documentation available that will help you.
Dim mspGeocodeResponse As New MSPGeocode.GeocodeResponse
mspGeocodeResponse = mspGeocode.Geocode(mspGeocodeRequest)
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
Marked as answer by Allen Li - MSFT on Aug 01, 2012 01:13 AM
Thanks for the information. Uninitializing the MSPgeocodeResponse subclasses fixed the issue. I am baffled, though, because on the request side, I actually have to initialize a subclass for it to work, so therefore I carried through and did the same with
the response. Do you know why it works for one and not the other? The request delaration and initialization looks like this:
Dim mspGeocodeRequest As New MSPGeocode.GeocodeRequest
mspGeocodeRequest.Address = New MSPGeocode.AddressType
request and response object have their own purpose.
Request object usually holds some data that you send to the service (e.g. address). I'm sure you do realize that one must initialize and populate that object with some data even if it is just a simple string (yes string is also an object).
Response is usually an object that the service returns. Of course that object holds data, but that object is initialized and populated with data on the service side. On client side you usually just define the type of the object and assign the returned value
from service to it.
Imagine client and service...
/// <summary>
/// Client
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//First you need to initialize and populate your
//request object since you need to send City to
//the service
RequestObject myCity = new RequestObject();
myCity.City = "Ljubljana";
//You don't have to initialize the response object
//you just have to declare the response object type
ResposeObject myCountry = getCountry(myCity);
Console.WriteLine(String.Concat("City ", myCity.City, " is in ", myCountry.Country, "."));
Console.ReadKey();
}
/// <summary>
/// Service
/// </summary>
/// <param name="city"></param>
/// <returns></returns>
private static ResposeObject getCountry(RequestObject city)
{
return new ResposeObject { Country = "Slovenia" };
}
private class RequestObject
{
public string City { get; set; }
}
private class ResposeObject
{
public string Country { get; set; }
}
Of course this is not by any case a real service, it's just an example of how request and response objects are passed between them because one example can usually say more than a 1000 words.
I tried to keep it as simple as I could so that you'd understand the basic principle.
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
So essentially, I must initialize the request since I'm doing the work of populating it with data, but the service will initialize the response since it's doing the work of populating THAT with data. I hadn't thought about it that way. Thank you very much.
Your information was very helpful.
someone31988
0 Points
4 Posts
Taking in an array of classes from a web service
Jul 26, 2012 02:33 PM|LINK
I'm currently writing a basic VB ASP.NET web application that connects to a geolocation web service. The web service allows a user to enter as much information they know about an address, and then replies back with a suggestion list of possible locations of the address. The user then sends the information back across, this time with a suggestion selected and it goes back and forth until a specific address is found. Below is a screenshot from soapUI showing an incomplete address: 510 Marketplace Blvd, Lansing MI, 48917, and the suggestion list that the web service responds with. There are 10 suggestions in the list. (The correct address should be 510 NORTH Marketplace Blvd, Lansing MI, 48917 which is the address of a business called Tractor Supply.)
https://docs.google.com/open?id=0ByzA8bp3rNK6Nmd0MUpYUEllejQ
I've added the web service's WSDL as a service reference already. As far as I can tell, the suggestion list comes in as an array of classes. How do I take in this array of classes and make it usable? What I'm doing at the moment is dumping it out to a multiline text box.
Here is a snippet of the code that I have written:
Dim mspGeocodeResponse As New MSPGeocode.GeocodeResponse mspGeocodeResponse.Address = New MSPGeocode.ExtendedAddressType mspGeocodeResponse.Address.Coordinates = New MSPGeocode.CoordinatesType mspGeocodeResponse.Address.suggestionList(100) = New MSPGeocode.FLSuggestionType mspGeocodeResponse = mspGeocode.Geocode(mspGeocodeRequest) For i As Integer = mspGeocodeResponse.Address.suggestionList.GetLowerBound(0) _ To mspGeocodeResponse.Address.suggestionList.GetUpperBound(0) resultsBuilder.Append("Suggestion ").Append(mspGeocodeResponse.Address.suggestionList(i).SuggestionNumber).AppendLine() resultsBuilder.Append(vbTab).Append("Street Number Low: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetNumberLow).AppendLine() resultsBuilder.Append(vbTab).Append("Street Number High: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetNumberHigh).AppendLine() resultsBuilder.Append(vbTab).Append("Street Prefix: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetPrefix).AppendLine() resultsBuilder.Append(vbTab).Append("Street Name: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetName).AppendLine() resultsBuilder.Append(vbTab).Append("Street Suffix: ").Append(mspGeocodeResponse.Address.suggestionList(i).StreetSuffix).AppendLine() NextHere is the declaration of the suggestion list class within Reference.vb:
Partial Public Class FLSuggestionType Inherits Object Implements System.ComponentModel.INotifyPropertyChanged Private suggestionNumberField As Integer Private streetNumberLowField As String Private streetNumberHighField As String Private streetPrefixField As String Private streetNameField As String Private streetSuffixField As String Private quadrantField As String Private firmField As String Private unitTypeField As String Private unitNumberLowField As String Private unitNumberHighField As StringAnd here is that class nested inside of the address class:
Partial Public Class ExtendedAddressType Inherits Object Implements System.ComponentModel.INotifyPropertyChanged Private suggestionListField() As FLSuggestionType Private streetPrefixField As StringWhen I run the application and type in the address "510 Marketplace Blvd, Lansing MI, 48917" to get the suggestion list back, I get an "Object reference not set to an instance of an object." error. Image of that below.
https://docs.google.com/open?id=0ByzA8bp3rNK6MDFPaFJDbng2cWc
I assume my syntax is wrong somewhere, but after tons of research of trying different cominations of things, I can't seem to get it pull in the suggestion list. Does anyone have any suggestions?
P.S. Sorry for using Google Drive for the images. I would have used an imagehosting service and embedded them, but my workplace blocks all of those sites.
mitja.GTI
Star
11157 Points
2094 Posts
Re: Taking in an array of classes from a web service
Jul 27, 2012 06:55 PM|LINK
Hi someone31988
usually one does not have to initialize sub classes/objects, variables in a main class/object. Ofcourse it is hard to say for sure since you didn't mention what service you are using neither showes the source of the response object. I'm also sure that they have some kind of documentation available that will help you.
mitja.gti | www.mitjagti.com
someone31988
0 Points
4 Posts
Re: Taking in an array of classes from a web service
Aug 01, 2012 01:32 PM|LINK
Thanks for the information. Uninitializing the MSPgeocodeResponse subclasses fixed the issue. I am baffled, though, because on the request side, I actually have to initialize a subclass for it to work, so therefore I carried through and did the same with the response. Do you know why it works for one and not the other? The request delaration and initialization looks like this:
mitja.GTI
Star
11157 Points
2094 Posts
Re: Taking in an array of classes from a web service
Aug 02, 2012 11:01 AM|LINK
Hi someone31988
request and response object have their own purpose.
Request object usually holds some data that you send to the service (e.g. address). I'm sure you do realize that one must initialize and populate that object with some data even if it is just a simple string (yes string is also an object).
Response is usually an object that the service returns. Of course that object holds data, but that object is initialized and populated with data on the service side. On client side you usually just define the type of the object and assign the returned value from service to it.
Imagine client and service...
/// <summary> /// Client /// </summary> /// <param name="args"></param> static void Main(string[] args) { //First you need to initialize and populate your //request object since you need to send City to //the service RequestObject myCity = new RequestObject(); myCity.City = "Ljubljana"; //You don't have to initialize the response object //you just have to declare the response object type ResposeObject myCountry = getCountry(myCity); Console.WriteLine(String.Concat("City ", myCity.City, " is in ", myCountry.Country, ".")); Console.ReadKey(); } /// <summary> /// Service /// </summary> /// <param name="city"></param> /// <returns></returns> private static ResposeObject getCountry(RequestObject city) { return new ResposeObject { Country = "Slovenia" }; } private class RequestObject { public string City { get; set; } } private class ResposeObject { public string Country { get; set; } }Of course this is not by any case a real service, it's just an example of how request and response objects are passed between them because one example can usually say more than a 1000 words.
I tried to keep it as simple as I could so that you'd understand the basic principle.
mitja.gti | www.mitjagti.com
someone31988
0 Points
4 Posts
Re: Taking in an array of classes from a web service
Aug 03, 2012 02:10 PM|LINK
So essentially, I must initialize the request since I'm doing the work of populating it with data, but the service will initialize the response since it's doing the work of populating THAT with data. I hadn't thought about it that way. Thank you very much. Your information was very helpful.