0

In C#, I need to write a constructor of a class as below which extends base class from android Library i.e. "Parcelable". I am getting an Error "Parcelable" does not contain a constructor that takes 0 arguments.

I saw C# Error: Parent does not contain a constructor that takes 0 arguments and Parent does not contain a constructor that takes 0 arguments but it did not work. My Class is as follows :

...
using Parcel = Android.OS.Parcel;
using Parcelable = Android.OS.Parcelable;

/// <summary>
/// Created by hashmi on 07/07/2015.
/// </summary>
public class Classname: Parcelable
{
    private int id;
    private string name;
    private string address;
    private string latitude;
    private string longitude;

    public Classname(int id, string name, string address, string latitude, string longitude) 
    {
        this.id = id;
        this.name = name;
        this.address = address;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    ...
}

Code for the base class Parcelable is as follows:

using System;
using Android.Runtime;
using Java.Lang;

namespace Android.OS
{
    [Register("android/os/Parcelable", ApiSince = 1, DoNotGenerateAcw = true)]
    public abstract class Parcelable : Java.Lang.Object
    {
        [Register("CONTENTS_FILE_DESCRIPTOR")]
        public const int ContentsFileDescriptor = 1;

        [Obsolete("This constant will be removed in the future version. Use Android.OS.ParcelableWriteFlags enum directly instead of this field.")]
        [Register("PARCELABLE_WRITE_RETURN_VALUE")]
        public const ParcelableWriteFlags ParcelableWriteReturnValue = ParcelableWriteFlags.ReturnValue;
    }
}

The base class is an Android Library abstract Class. I want to write a Constructor like "Classname" above without modifying the abstract base class that is "Parcelable".

I have edited the original code with the following code.

using Parcel = Android.OS.Parcel;
using Parcelable = Android.OS.Parcelable;

/// <summary>
/// Created by hashmi on 07/07/2015.
/// </summary>
public class LocationDetail : Parcelable
{

    private int id;
    private string name;
    private string address;
    private string latitude;
    private string longitude;


    public LocationDetail(int id, string name, string address, string latitude, string longitude) 
        : base(id, name, address, latitude, longitude) 
    {
        this.id = id;
        this.name = name;
        this.address = address;
        this.latitude = latitude;
        this.longitude = longitude;
    }
...
}

The new error is "Parcelable" does not contain a constructor that takes 5 arguments

humbledog
  • 35
  • 5
  • "It did not work": *What* did not work? Exactly what did you try? Please edit your question and add the code you wrote based on that answer, and the *exact*, full, complete text of the error message you got. That answer correctly illustrates how to call a base class constructor. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 14:29
  • [This](https://stackoverflow.com/questions/7230544/c-sharp-error-parent-does-not-contain-a-constructor-that-takes-0-arguments) (which is what *you* linked) almost certainly answers your question, what about it didn't work? – DavidG Jun 03 '19 at 14:29
  • Hint: `public Classname(int id, string name, string address, string latitude, string longitude) : base(/* see what options vs offers you */) {/* implementation here */}` – Zohar Peled Jun 03 '19 at 14:31
  • @DavidG, Even after implementing that link's answer, as you have suggested, it is still giving me the same error. – humbledog Jun 03 '19 at 14:53
  • Then tell us what the new error is, and show us that code. – DavidG Jun 03 '19 at 14:53
  • @Zohar Peled, VS offers me nothing. – humbledog Jun 03 '19 at 15:09
  • Take a look at how I do something similar in my ViewPagerIndicator port: https://github.com/Cheesebaron/ViewPagerIndicator/blob/master/Library/CirclePageIndicator.cs#L515 Take a look at the `IParcelableCreator` and `IParcelable` interfaces. If they want you to implement the Handle property, make sure to inherit from `Java.Lang.Object`. – Cheesebaron Jun 03 '19 at 15:20
  • @DavidG I have edited my Question to show the new error. – humbledog Jun 03 '19 at 15:52
  • 1
    You can't just pass an arbitrary set of parameters to the base class, please take a look at what `base(...)` is expecting. – DavidG Jun 03 '19 at 15:53
  • @EdPlunkett, I have edited my Question with your suggested changes, the new error is --- "Parcelable" does not contain a constructor that takes 5 arguments – humbledog Jun 03 '19 at 15:54
  • @DavidG, I know that. The base class is an android package class i.e. "Parcelable", and I do not want to modify the system class. How to achieve that? – humbledog Jun 03 '19 at 15:56
  • @humbledog Find out what constructors the base class has. Use one. Does Intellisense not help? – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 15:57
  • @EdPlunket, I have shown the base class in my question. Please see that above. It is an android package class i.e. "Parcelable". Hence I do not want to modify that class. – humbledog Jun 03 '19 at 16:00
  • @humbledog Nobody told you to modify the base class. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 16:01
  • @EdPlunket, as you can see, the base class has no constructor. Also, as It is suggested in different answers, "no constructor" has always been treated as a constructor with 0 arguments. Still, if I pass a base class constructor with 0 arguments, it is giving the error that "Parcelable" does not contain a constructor that takes 0 arguments. – humbledog Jun 03 '19 at 16:07

1 Answers1

1

From the looks of the Android documentation for Parcelable, it doesn't appear to implement any constructors, it implements a creator interface to build the Parcelable from a Parcel.

This is a typical implementation of Parcelable according to the android docs (In Java):

 public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }

As you can see Parcelable itself contains no constructors. This SO thread talks a little about the concept of Parcelables in Android.

I've come across a couple of guides that outline how to use this in Xamarin, one suggests that you can implement it like this: (This guide can be found here)

public class UserParcelable : Java.Lang.Object, IParcelable
{
    public UserParcelable()
    {
    }

    public int DescribeContents()
    {
        throw new NotImplementedException();
    }

    public void WriteToParcel(Parcel dest, [GeneratedEnum] ParcelableWriteFlags flags)
    {
        throw new NotImplementedException();
    }
}
JoeTomks
  • 3,243
  • 1
  • 18
  • 42