I don't understand what this is asking for at all (sry for bad title) [Answered]

Create a Sieve class with a public bool IsGood(int number) method. This class needs a constructor with a delegate parameter that can be invoked later within the IsGood method. Hint: You can make your own delegate type or use Func<int, bool>
Create a Sieve class with a public bool IsGood(int number) method. This class needs a constructor with a delegate parameter that can be invoked later within the IsGood method. Hint: You can make your own delegate type or use Func<int, bool>
138 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay I will
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay brb
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yeah that part confused me a lot
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i get putting delegate in the param
MODiX
MODiX3y ago
tebeco#0205
what does your code look like
React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol okay i am :]
class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}
class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}
so I think I get it more now
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
Console.WriteLine("Which filter: Even, positive, or x10?");
string filterChoice = Console.ReadLine();
//if (filterChoice == "even")

bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}

public delegate int SieveDelegate(int number);
Console.WriteLine("Which filter: Even, positive, or x10?");
string filterChoice = Console.ReadLine();
//if (filterChoice == "even")

bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}

public delegate int SieveDelegate(int number);
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
oh yeah
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
so just a Func<int> func
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
no and I'm reading sorry. so the int is the paramater and the bool is what it returns?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
it will only be for the better Func<int, bool>
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
but that's so much more confusing to me lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}
class Sieve
{
public bool IsGood(int number)
{
return true;
}
public Sieve(int number, SieveDelegate operation)
{

}
}
React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol okay i didn't know ctor came first i think I need to read the delegate information again brb I'm just so confused man I'm sorry idk why but none of this makes sense to me. the chapter isntruction used a user made delegate, I have no idea what to do with this func thing and brb again, i'm sick 😭 Objectives: • Create a Sieve class with a public bool IsGood(int number) method. This class needs a constructor with a delegate parameter that can be invoked later within the IsGood method. Hint: You can make your own delegate type or use Func<int, bool>. • Define methods with an int parameter and a bool return type for the following: (1) returns true for even numbers, (2) returns true for positive numbers, and (3) returns true for multiples of 10. • Create a program that asks the user to pick one of those three filters, constructs a new Sieve instance by passing in one of those methods as a parameter, and then ask the user to enter numbers repeatedly, displaying whether the number is good or bad depending on the filter in use. • Answer this question: Describe how you could have also solved this problem with inheritance and polymorphism. Which solution seems more straightforward to you, and why? here's the whole of the exercise just so we have the same frame of reference. i feel like I'm wasting your time my man. Sorry.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public class Sieve
{
public Sieve(int number, Func<int, bool> func)
{

}
public bool IsGood(int number)
{
return true;
}
public class Sieve
{
public Sieve(int number, Func<int, bool> func)
{

}
public bool IsGood(int number)
{
return true;
}
I did what you asked I thought
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
passing a method like a variable pretty much?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
it's just calling it right? delegate.Invoke()
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public Sieve(int number, Func<int, bool> func)
{
func.Invoke(number);
}
public Sieve(int number, Func<int, bool> func)
{
func.Invoke(number);
}
like that? I only used ctor for assigning properties before, so I didn't know any of this made sense at all
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
Func<......> foo;
foo()
Func<......> foo;
foo()
React with ❌ to remove this embed.
BigggMoustache
BigggMoustacheOP3y ago
lol okay
BigggMoustache
BigggMoustacheOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

Sieve sieve = new Sieve(4, Even);

public class Sieve
{
public Sieve(int number, Func<int, bool> func)
{
func(number);
}
public bool IsGood(int number)
{
return true;
}
}
bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

Sieve sieve = new Sieve(4, Even);

public class Sieve
{
public Sieve(int number, Func<int, bool> func)
{
func(number);
}
public bool IsGood(int number)
{
return true;
}
}
so this is using the delegate now right?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
but how to access? do i set property to = func?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I am sorry I don't know variable? lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
LOL
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i like it tyvm sorry for being dumb I am genuinely trying to learn, coding is hard.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
xD
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i want a job doing this, i have to learn xD
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yes
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i honestly don't know bud
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
idk what a field is lol i'll search real quick !field haha was hoping holy shit okay tyvm field is nice I'll have to remember that
public class Sieve
{
private Func<int, bool> _function;
public Sieve(int number, Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}
public class Sieve
{
private Func<int, bool> _function;
public Sieve(int number, Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}
What I am understanding is like.. adding 2+ 2 It's not the same as stacking 2 block + 2 block, or cutting 2" wood and 2" wood
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
so like I learn what "delegate" is, but not how it is used. Just like I can learn what 2 + 2 is, but not how it's used I get very frustrated feeling like I'm not learning anything because of this lol okay so I understand now, I think, tyvm
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
ye it's a bout separation right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
right right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i don't understand that lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
so you don't even need to write out your delegate functions?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
if i have func<int, bool> in ctor I can just slap things in the param?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
wow nice
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
tebeco#0205
REPL Result: Success
public class Sieve
{
private Func<int, bool> _function;
public Sieve(int number, Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}

var one = new Sieve(1, i => i > 100);
one.IsGood(34252345)
public class Sieve
{
private Func<int, bool> _function;
public Sieve(int number, Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}

var one = new Sieve(1, i => i > 100);
one.IsGood(34252345)
Result: bool
True
True
Compile: 613.693ms | Execution: 60.531ms | React with ❌ to remove this embed.
BigggMoustache
BigggMoustacheOP3y ago
lol
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
from before when I was making delegate i didn't understand Func<in, out> so was taking number in there
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
you are correct lol I did xD
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public class Sieve
{
private Func<int, bool> _function;
public Sieve(Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}
public class Sieve
{
private Func<int, bool> _function;
public Sieve(Func<int, bool> func)
{
_function = func;
}
public bool IsGood(int number)
{
return _function(number);
}
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lint = fuzz in pocket
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
lol member?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public class Sieve
{
private Func<int, bool> _function;
public Sieve(Func<int, bool> func)
{
_function = func;
}

public bool IsGood(int number)
{
return _function(number);
}
}
public class Sieve
{
private Func<int, bool> _function;
public Sieve(Func<int, bool> func)
{
_function = func;
}

public bool IsGood(int number)
{
return _function(number);
}
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public class Sieve
{
private Func<int, bool> _isGood;

public Sieve(Func<int, bool> isGood)
{
_isGood = isGood;
}

public bool IsGood(int number)
{
return _isGood(number);
}
}
public class Sieve
{
private Func<int, bool> _isGood;

public Sieve(Func<int, bool> isGood)
{
_isGood = isGood;
}

public bool IsGood(int number)
{
return _isGood(number);
}
}
ya that was bad naming. ty for reminding.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yeah duh ty
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
oh okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i did it lol honestlyu lambda still confuse me so i don't use a lot
MODiX
MODiX3y ago
tebeco#0205
(send code as you edit)
React with ❌ to remove this embed.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
public class Sieve
{
private Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
public class Sieve
{
private Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
yo dude people like you make this place worth being in
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
thank yo uso much. oh it's not?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
oh okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I'll have to read about it xD
bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

Sieve sieve = AssignSieve();

Sieve AssignSieve()
{
while(true)
{
Console.WriteLine("Which filter: Even, positive, or Mult10?");
string filterChoice = Console.ReadLine();

if (filterChoice == "even") return new Sieve(Even);
if (filterChoice == "positive") return new Sieve(Positive);
if (filterChoice == "mult10") return new Sieve(Mult10);
else
{
Console.WriteLine("Not a valid input");
}
}
}


while(true)
{
Console.Write("Enter a number: ");
string ?input = Console.ReadLine();
Console.WriteLine($"Can input go through sieve {sieve}? {sieve.IsGood(Int32.Parse(input))}");

}

public class Sieve
{
private Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
bool Even(int number) => number % 2 == 0;
bool Positive(int number) => number > -1;
bool Mult10(int number) => number % 10 == 0;

Sieve sieve = AssignSieve();

Sieve AssignSieve()
{
while(true)
{
Console.WriteLine("Which filter: Even, positive, or Mult10?");
string filterChoice = Console.ReadLine();

if (filterChoice == "even") return new Sieve(Even);
if (filterChoice == "positive") return new Sieve(Positive);
if (filterChoice == "mult10") return new Sieve(Mult10);
else
{
Console.WriteLine("Not a valid input");
}
}
}


while(true)
{
Console.Write("Enter a number: ");
string ?input = Console.ReadLine();
Console.WriteLine($"Can input go through sieve {sieve}? {sieve.IsGood(Int32.Parse(input))}");

}

public class Sieve
{
private Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
blobthumbsup you point out other bad things?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
I thought about switch expression, I'l lrewrite like that thanks. If I take new out it throws error "cannot be used like a method" Oh I get what you're saying. so you would make a sieveEven, sievePositive etc and just reassign them to sieve in the switch
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
i don't know what state specific means
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
okay
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
right
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
Thanks. I get confused a lot because I remember "everything public = code smell" but things need to be public so much because I'm bad at writing code xD so sometimes I don't think about it because idk, I feel like I'm doing things wrong otherwise.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
attributes get ignored because i'm bad xD ty for pointing out though, I will try and remember that
Sieve sieve;
Sieve sieveEven = new Sieve(number => number % 2 == 0);
Sieve sievePositive = new Sieve(number => number > -1);
Sieve sieveMult10 = new Sieve(number => number % 10 == 0);

AssignSieve();
Sieve AssignSieve()
{
while (true)
{
Console.WriteLine("Which filter: Even, positive, or Mult10?");
string filterChoice = Console.ReadLine();
sieve = filterChoice switch
{
"even" => sieveEven,
"positive" => sievePositive,
"mult10" => sieveMult10
_ => AssignSieve()
};
return sieve;
}
}


while(true)
{
Console.Write("Enter a number: ");
string ?input = Console.ReadLine();
Console.WriteLine($"Can input go through sieve? {sieve.IsGood(Int32.Parse(input))}");

}

public class Sieve
{
public readonly Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
Sieve sieve;
Sieve sieveEven = new Sieve(number => number % 2 == 0);
Sieve sievePositive = new Sieve(number => number > -1);
Sieve sieveMult10 = new Sieve(number => number % 10 == 0);

AssignSieve();
Sieve AssignSieve()
{
while (true)
{
Console.WriteLine("Which filter: Even, positive, or Mult10?");
string filterChoice = Console.ReadLine();
sieve = filterChoice switch
{
"even" => sieveEven,
"positive" => sievePositive,
"mult10" => sieveMult10
_ => AssignSieve()
};
return sieve;
}
}


while(true)
{
Console.Write("Enter a number: ");
string ?input = Console.ReadLine();
Console.WriteLine($"Can input go through sieve? {sieve.IsGood(Int32.Parse(input))}");

}

public class Sieve
{
public readonly Func<int, bool> _numChecker;

public Sieve(Func<int, bool> numChecker) => _numChecker = numChecker;

public bool IsGood(int number) => _numChecker(number);
}
this is what I have now why did you say return null or something? I have to get a Sieve assigned to continue, so what's wrong with just calling itself again to repeat until a valid return?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
BigggMoustache
BigggMoustacheOP3y ago
That was what I was thinking. Thanks. "cannot implicitly convert bool to type Sieve" when making that change. Gonna look at it for a minute. Oh duh I fixed it. Thanks a ton for the feedback. :]
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server