C
Join ServerC#
help
❔ ✅ method array arg error
RRyan-T141212/24/2022
.

RRyan-T141212/24/2022
problem in line 4
RRyan-T141212/24/2022
but idk why
AAngius12/24/2022
What problem?
RRyan-T141212/24/2022

AAngius12/24/2022
You need to declare the array properly
AAngius12/24/2022
new[]{true, true, false}
AAngius12/24/2022
Or
new bool[]{true, true, false}
AAngius12/24/2022
The
{a, b, c}
syntax works only when the type is apparent on the left side of assignmentRRyan-T141212/24/2022
like what?
AAngius12/24/2022
int[] foo = {1, 2, 3}
RRyan-T141212/24/2022
identifying array first is much better 🙂
RRyan-T141212/24/2022
and it looks cleaner
RRyan-T141212/24/2022
//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;
}
}
RRyan-T141212/24/2022
Output: 2
RRyan-T141212/24/2022
!close
AAccord12/24/2022
Closed!
Ccap5lut12/24/2022
heya ryan, one little tip:
if( item == true )
can be written simply as if (item)
because item
is already a boolean value 😉RRyan-T141212/24/2022
nice to know that thanks 😄
RRyan-T141212/24/2022
you are helping everytime 😂
Ccap5lut12/24/2022
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)RRyan-T141212/24/2022
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);
}
}
RRyan-T141212/24/2022
others code and mine 😄
RRyan-T141212/24/2022
solving it with just 1 line
AAccord12/25/2022
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.