Bug 32620 - ScreenViewBuilder "chaining" methods returning Java.Lang.Object instead of ScreenViewBuilder
Summary: ScreenViewBuilder "chaining" methods returning Java.Lang.Object instead of Sc...
Status: RESOLVED FIXED
Alias: None
Product: Components
Classification: Xamarin
Component: Xamarin Components ()
Version: N/A
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Jon Dick
URL:
Depends on:
Blocks:
 
Reported: 2015-07-30 12:32 UTC by Adam Patridge
Modified: 2015-09-29 13:50 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Adam Patridge 2015-07-30 12:32:13 UTC
While trying to replicate some Google Analytics code in Xamarin, I noticed the chaining methods for ScreenViewBuilder couldn't actually be chained in Xamarin.Android. Is this a requirement of the binding process or could these `Set*` methods return ScreenViewBuilder to allow for near-verbatim C# porting?

I'm using the Xamarin.GooglePlayService.Analytics NuGet package v25.0.0.0 (and .Base of the same version).

Here's the Google Analytics code in question: https://developers.google.com/analytics/devguides/collection/android/v4/campaigns#general-campaigns

Ideally, it would work just like the Java equivalent (with C# letter case adjustments, as needed).

> using Android.Gms.Analytics;
> ...
> t.Send(new HitBuilders.ScreenViewBuilder()
>     .SetCampaignParamsFromUrl(campaignData)
>     .Build() // Compile error since SetCampaignParamsFromUrl returns Java.Lang.Object (no Build method there).
> );

I was able to work around the issue just fine by simply storing a reference to the original ScreenViewBuilder.

> var screenViewBuilder = new HitBuilders.ScreenViewBuilder();
> screenViewBuilder.SetCampaignParamsFromUrl(campaignData);
> t.Send(screenViewBuilder.Build());
Comment 1 Jon Dick 2015-09-29 13:50:37 UTC
This will be better in v27.0.0.0-rc2 and higher (not yet released), however still not perfect.

Basically i've corrected the return types in the classes inside of HitBuilders...  However, since all the builder classes subclass HitBuilders.HitBuilder and all of its methods return HitBuilders.HitBuilder, you will need to chain the subclass methods together first so you don't lose their type and end up with a HitBuilder type....

So, for instance,

// The call to .AddProduct will return HitBuilder, as will .Build ()
// So this will not work!
var screenViewBuilder = new HitBuilders.ScreenViewBuilder ()
    .AddProduct (product) // Returns HitBuilder
    .SetCampaignParams.FromUrl (campaignData) // Returns ScreenViewBuilder
    .Build (); // Returns HitBuilder

// However in the right order, it will work fine:
var screenViewBuilder = new HitBuilders.ScreenViewBuilder ()
    .SetCampaignParams.FromUrl (campaignData) // Returns ScreenViewBuilder
    .AddProduct (product) // Returns HitBuilder
    .Build (); // Returns HitBuilder