C
C#2y ago
Ryan-T1412

❔ ✅ method array arg error

.
13 Replies
Ryan-T1412
Ryan-T1412OP2y ago
problem in line 4 but idk why
Angius
Angius2y ago
What problem?
Ryan-T1412
Ryan-T1412OP2y ago
Angius
Angius2y ago
You need to declare the array properly new[]{true, true, false} Or new bool[]{true, true, false} The {a, b, c} syntax works only when the type is apparent on the left side of assignment
Ryan-T1412
Ryan-T1412OP2y ago
like what?
Angius
Angius2y ago
int[] foo = {1, 2, 3}
Ryan-T1412
Ryan-T1412OP2y ago
identifying array first is much better 🙂 and it looks cleaner
//Creating Objects
Problem test1 = new Problem();
bool[] myArray = {true, false, false, true, false};
int result = test1.countTrue(myArray);
// Printing Variables
Console.WriteLine(result);
// Class
class Problem
{
//Method
public int countTrue(bool[] myArray)
{
int counter = 0;
foreach (bool item in myArray)
{
if( item == true ){
counter++;
}
}
return counter;
}
}
//Creating Objects
Problem test1 = new Problem();
bool[] myArray = {true, false, false, true, false};
int result = test1.countTrue(myArray);
// Printing Variables
Console.WriteLine(result);
// Class
class Problem
{
//Method
public int countTrue(bool[] myArray)
{
int counter = 0;
foreach (bool item in myArray)
{
if( item == true ){
counter++;
}
}
return counter;
}
}
Output: 2 !close
Accord
Accord2y ago
Closed!
cap5lut
cap5lut2y ago
heya ryan, one little tip: if( item == true ) can be written simply as if (item) because item is already a boolean value 😉
Ryan-T1412
Ryan-T1412OP2y ago
nice to know that thanks 😄 you are helping everytime 😂
cap5lut
cap5lut2y ago
in if (condition) the condition must be an expression that results in a boolean value, so someBool == true is the same someBool someBool == false (or someBool != true) is the same as !someBool (! is an unary operator to negate the boolean value, so true becomes false and false becomes true)
Ryan-T1412
Ryan-T1412OP2y ago
bool[] myArray = {true, false, false, true, false};
int result = test1.countTrue(myArray);
// Printing Variables
Console.WriteLine(result);
// Class
class Problem
{
//Method
public int countTrue(bool[] myArray)
{
return myArray.Count(val => val);
}
}
bool[] myArray = {true, false, false, true, false};
int result = test1.countTrue(myArray);
// Printing Variables
Console.WriteLine(result);
// Class
class Problem
{
//Method
public int countTrue(bool[] myArray)
{
return myArray.Count(val => val);
}
}
others code and mine 😄 solving it with just 1 line
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server