IComparer does not implement interface member - Error CS0738 [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to sort two objects by one of their properties (.Transaction.topLeftX, an integer) using the following code to create a comparer to use in a Sort method:
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public Kairos.Net.RecognizeImage Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
if (x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX) <= 0) return x;
else return y;
}
}
However, I get the error message Error CS0738 'RecogniseFacesKairos.RespComp' does not implement interface member 'IComparer.Compare(RecognizeImage, RecognizeImage)'. 'RecogniseFacesKairos.RespComp.Compare(RecognizeImage, RecognizeImage)' cannot implement 'IComparer.Compare(RecognizeImage, RecognizeImage)' because it does not have the matching return type of 'int'.
Does the comparer used in the Sort method need to have return type int?

The IComparer<T> interface is supposed to implement a method that returns an int comparison. -1 for less than, 0 for equal and 1 for greater than.
Look at your code, if you're just comparing the top left, you can probably just do the following:
public int Compare(FooImage x, FooImage y) {
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}

The desired outcome of sorting objects by one of their parameters was achieved by the following code:
...
Kairos.Net.KairosClient client = new Kairos.Net.KairosClient();
client.ApplicationID = appId;
client.ApplicationKey = appKey;
Kairos.Net.RecognizeResponse resp = client.Recognize(...);
RespComp SortImages = new RespComp();
resp.Images.Sort(SortImages);
...
public class RespComp : IComparer<Kairos.Net.RecognizeImage>
{
public int Compare(Kairos.Net.RecognizeImage x, Kairos.Net.RecognizeImage y)
{
return x.Transaction.topLeftX.CompareTo(y.Transaction.topLeftX);
}
}

Related

Using ternary operations to make my code cleaner [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been doing some C# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
So basically if Bomb("xxxxxx") contains the string "bomb" it will return "Duck!!!" if not it will return "There is no bomb, relax."
But for some reason, this doesn't work and I can't figure out why.
You just need to add return
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") == true ? "Duck!!!" : "There is no bomb, relax.";
}
Your ternary operator looks OK, but you're missing a return statement from the function. Also note that Contains returns a boolean, so the == true is redundant:
So I have been doing some c# practice problems for a while and I want to start playing with ternary operations to make my code cleaner.
Here is my code:
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}
you missed the return part in the function
public static string Bomb(string txt)
{
return txt.ToLower().Contains("bomb") ? "Duck!!!" : "There is no bomb, relax.";
}

How to use a variable instantiated outside a method in C#? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to know how to use a global variable - which I already instantiated it as a public integer type - in a method later.
Here's my code so far:
public int money = 500000;
//other variables
//...some code in between
public static void UpdateResources (int cost, int airRate, int waterRate, int foodRate, int energyRate, int maintenanceRate, int happinessRate)
{
// \/ Problem here
if (money < cost)
{
//uncheck box
}
else
{
//implement input variables with other external variables
}
}
Remove "static" keyword from your method, static method cannot access instance variables. Static method is something belong to the type itself, while your instance variable is not. another option is to put the "money" as static, but than all your instances going to use the same "money" which is probably not what you aiming for.
public void updateResources (int cost, int airRate, int waterRate, int foodRate, int energyRate, int maintenanceRate, int happinessRate)
{
// v- No more Problem here :)
if (money < cost)
{
//uncheck box
}
else
{
//implement input variables with other external variables
}
}

Accessing a property through reflection returns a different value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Ok, as my original question seemed a bit ambiguous because I was asking for a general question about the C# language, but showing part of a particular example where I was having a problem with it, I'm going to try to rewrite so that it is clearer that my question is about the C# language, not about my particular problem.
I currently have a property (several, in fact) of a class, that return a different value depending on whether you access them directly by code, or using reflection. This is what happens when I access the property using the immediate console of VS:
> SelectedLine.QtyOutstanding
0
> var prop = SelectedLine.GetType().GetProperty("QtyOutstanding")
> prop.GetValue(SelectedLine)
8
Regardless of how the property is defined, what is the difference, in C#, between both ways of accessing the property?
Shouldn't they both run exactly the same code in the setter/getter, if there is one?
(Considering that GetType() returns the same type as the variable is declared as)
I found a way to produce this, maybe your case looks like that?
If your SelectedLine is accessible via interface, and your class has an explicite implementation of that, but also has a public property with the same name, this could lead to different results.
Example
class Program
{
static void Main(string[] args)
{
var SelectedLine = (ILine)new Line(8);
Console.WriteLine(SelectedLine.QtyOutstanding); // 0
var prop = SelectedLine.GetType().GetProperty("QtyOutstanding");
Console.WriteLine(prop.GetValue(SelectedLine)); // 8
Console.ReadLine();
}
}
class Line : ILine
{
public Line(int qtyOutstanding)
{
QtyOutstanding = qtyOutstanding;
}
public int QtyOutstanding { get; }
int ILine.QtyOutstanding
{
get
{
return 0;
}
}
}
interface ILine
{
int QtyOutstanding { get; }
}

Implement a functional processing pipeline with compile-time order constraints [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Let's say we have a data object X and some "processor" objects/methods A, B, C and D. A(X) produces a new X with some additional data (the result of A processing). B(X) produces a new X with some other additional data. C(X) also produces a new X with some additional data but it requires that A has already been ran against X.
So: A(X).B(X).C(X).D(X) should run properly. B(X).D(X).A(X).C(X) should also run properly. B(X).C(X).A(X).D(X) should fail (because C requires the info A produces).
Is this possible to implement in C# so that the order constraints are enforced in compile time? If not, is there a design pattern or some common strategy of how this should be implemented? There can be many processors and many constraints, what I'd like to avoid is having to declare a factorial number of types to keep track of whether a processor has been ran or not.
You can use inheritance, combined with generic constraints:
class Data {
}
class ExtendedData : Data {
}
static class Pipeline {
public static ExtendedData A<T>(this T value) where T : Data {
if (value is ExtendedData extended) {
return extended;
}
else {
return new ExtendedData():
}
}
public static T B<T>(this T value) where T : Data {
return value;
}
public static ExtendedData C(this ExtendedData value) {
return value;
}
}
These variants will work:
new Data().A().B().C();
new Data().B().A().C();
new Data().A().C().B();
This variant will be rejected by the compiler:
new Data().B().C().A();
C() will expect an ExtendedData, while B() will only deliver Data.

Unity error CS0029: Cannot implicitly convert type `bool' to `int' but it's bool [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have error
CS0029: Cannot implicitly convert type bool to int.
My declaration:
public bool isBig = false;
If statement with error:
if (player.GetHP() < 6 && player.isBig == false)
I don't understand this. I have also change this bool to return and checked few solutions:
player.GetBig() == false/0 / (player.GetBig()) == false/0 / !(player.GetBig())
but nothing works...
// Edit
public int GetBig()
{ // isBig is bool
return this.isBig;
}
public int GetHP()
{ // HP is int
return this.HP;
}
Simple typo.
public int GetBig()
should be
public bool GetBig()

Categories