Error xs1061 in Compare method

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error xs1061 in Compare method

Post by wriedmann »

Hello,

please see this code:

Code: Select all

using System.Collections.Generic

class xCompare

public property MyValue as string auto

end class

class xComparer<xCompare> implements IComparer<xCompare>

method Compare( o1 as xCompare, o2 as xCompare ) as int
local nReturn as int
local c1 as string
local c2 as string

c1 := o1:MyValue // error XS1061: 'xCompare' does not contain a definition for 'MyValue' and no accessible extension method 'MyValue' accepting a first argument of type 'xCompare' could be found 
c2  := o2:MyValue // error XS1061: 'xCompare' does not contain a definition for 'MyValue' and no accessible extension method 'MyValue' accepting a first argument of type 'xCompare' could be found 

nReturn := String.Compare( c1, c2 )

return nReturn

end class
The error seems to have to do with the fact that the Compare method is using generics.
When I remove the generics part from the method declaration, the code compiles without error.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Error xs1061 in Compare method

Post by Chris »

Hi Wolfgang,

This is because there's a name conflict between the name of the Generic class param and the type having the same named. Normally the naming convention you would use for that is

class xComparer<T> implements IComparer<xCompare>

if what you intended to do was to force the generic param being of type xCompare, then you would need to use

class xComparer<T> implements IComparer<xCompare> where T is xCompare
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error xs1061 in Compare method

Post by wriedmann »

Hi Chris,

thank you very much! That solved my problem.

Sometimes it seems I have problems with the syntax - and could not find any samples, therefore I've asked here.

Thank you again!

Wolfgang
P.S. I like the XPorter enhancements very much!
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Error xs1061 in Compare method

Post by Chris »

You're welcome Wolfgang, that's why we have the forums! :)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply