Cannot match parameter list for constructor (C# in Visual Studio)

Am porting an iOS application from Swift on Xcode to C# on Visual Studio 15. I need to do some dynamic arrangement of UITextViews, and need to instantiate a new instance of NSLayoutConstraint to facilitate that. When I try to create it directly via a new instance:

constraint = new NSLayoutConstraint( toField, toAlignment, NSLayoutRelation.Equal, fromField, fromAlignment, 1, 0);

I get the error ‘No matching overload’.

When I look at the popup for the NSLayoutConstraint(), it gives me two parameter list options:

NSLayoutConstaint.init() : instancetype

and

NSLayoutConstraint.$New(view1: rtl.id!) attribute (attr1: NSLayoutAttribute) relatedBy (relation: NSLayoutRelation) toItem (view2: rtl.id) attribute (attr2:NsLayoutAttribute) multiplier (multiplier: CGFloat) constant (c: CGFloat) : Instancetype

Looking at the NSLayoutConstraint metadata, it says that the definition is:

public static this withItem(id! view1) attribute(NSLayoutAttribute attr1) relatedBy(NSLayoutRelation relation) toItem(id view2) attribute(NSLayoutAttribute attr2) multiplier(CGFloat multiplier) constant(CGFloat c);

In both cases, it seems to match the parameters required in Swift on Xcode (the original code works fine there). I’ve tried casting the toField/fromField values, which are the only ones which do not explicitly match the parameter list types, to no avail. I’ve also played around with the last two float parameters just in case the compiler isn’t automatically casting them. Nothing seems to work.

Are there any differences in the calling method from C#? Is there some documentation page containing precise details on how to translate the various UI calls which I haven’t found?

Thanks,
Andrew

What’s the exact error message? Do you get any follow-up hints, such as “parameter 2 is X, should be Y”?

Oh, i just notice this on second read: that’s a mixture between C# and Swift syntax. Swift doesn’t use “new” but it uses the parameters inside a single set of (). C# uses separate sets of parens, i.e.

Swift: constraint = NSLayoutConstraint(item: ..., attribute: ..., relatedBy: ..., ...
C#: constraint = new NSLayoutConstraint withItem(...) attribute(...) relatedBy(...) ...

yours,
marc

That is … puzzling. I’ve been programming C# for several years, and the constructor parameter passing syntax I used is the only one I have ever used. I’ve gone back and looked through both some texts and the online Microsoft documentation, and I cannot find any mention of the syntax you specify. You can used named arguments in C#, but parentheses are still required after the constructor name, and the name/value sets are paired by colons and separated with commas (much like Swift):

new Constructor(parameter1: value, parameter2: value);

I can find no mention of the syntax you specify. Is it only for calls to the UIKit library functions?

I tried it, and it does build, so I can continue with my coding.

Thanks,
Andrew

Welcome to Cocoa :wink:

Check out http://docs.elementscompiler.com/Hydrogene/LanguageExtensions/MultiPartMethodNames/ for mor info on multi-part method names.