0

I'm new to C# and having problems with the usage of a non standard constructor. This is a part of the interface of the library I'm using (QRCoder) after the latest update:

public class Contact
  {
    [Obsolete("This constructor is deprecated. Use WithCombinedAddress instead.")]
    public Contact(string name, string country, string addressLine1, string addressLine2);
    [Obsolete("This constructor is deprecated. Use WithStructuredAddress instead.")]
    public Contact(string name, string zipCode, string city, string country, string street = null, string houseNumber = null);

    public static Contact WithCombinedAddress(string name, string country, string addressLine1, string addressLine2);
    public static Contact WithStructuredAddress(string name, string zipCode, string city, string country, string street = null, string houseNumber = null);
    public override string ToString();

Before the update, this code used to work:

  Contact contactCreditor;
  string name, zipCode, city, country, street, housenr, strIban;
  ... 
  contactCreditor = new SwissQrCode.Contact (name, zipCode, city, country, street, housenr);

Now I get the error message

This constructor is deprecated. Use WithStructuredAddress instead.

How can I modify my code to use this new constructor?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Welcome. `WithCombinedAddress` is a [static method](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) that can be called without having an instance of your `Contact` class. Call it like `Contact.WithCombinedAddress` and you'll get your instance - just like before. – devsmn Sep 28 '22 at 08:34
  • Replace `new SwissQrCode.Contact` with `SwissQrCode.Contact.WithStructuredAddress`. – Johnathan Barclay Sep 28 '22 at 08:34
  • `var newContact = SwissQrCode.Contact.WithStructuredAddress(...)` – DavidG Sep 28 '22 at 08:34

1 Answers1

3

The class no longer has a non deprecated constructor, instead call the static initializer.

Contact contactCreditor;
  string name, zipCode, city, country, street, housenr, strIban;
  ... 
  contactCreditor = SwissQrCode.Contact.WithStructuredAddress(
                        name,
                        zipCode,
                        city,
                        country,
                        street,
                        housenr);
Jodrell
  • 34,946
  • 5
  • 87
  • 124