Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ive been writing a code for a project and I was wondering how to create a method (IsEnrolled) to return a Boolean value (need to use if statement.) This method will have to return a true statement is student is enrolled and a false if student isn't enrolled. The problem is my CurrentEnrolledCourse property is a string and when I create it I keep getting an error that says "Cannot Implicity convert type 'string' to 'bool'


Here is the code I have so far (This is just part of the code not the complete code!)
C#
public bool IsEnrolled()
{
if (CurrentEnrolledCourse = null)
{
    return false;
}
    else
    { return true;
    }
}


What I have tried:

I have tried to convert string to bool but I havnt found anything on it yet!
Posted
Updated 1-Mar-16 16:30pm

This is clearly homework since "need to use if statement".
Clearly you need to get more familiar with C# syntax; specifically the assignment and comparison operators.
C#
public bool IsEnrolled()
{
    if (CurrentEnrolledCourse == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Assuming the student is enrolled only if the CurrentEnrolledCourse isn't empty.
A better way (since "need to use if statement"):
C#
public bool IsEnrolled()
{
    if (CurrentEnrolledCourse == null)
        return false;
    return true;
}

The right way:
C#
public bool IsEnrolled()
{
    return !string.IsNullOrEmpty(CurrentEnrolledCourse);
}

(And it should be a "get-only" property.)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Mar-16 23:02pm    
As I can guess, you consider "need to use if statement" as an excuse for pointless "if" and "else".

Well, maybe for a student, but not for the person who gave this assignment, if only it was formulated as you suggest. The thing is: this is way too easy to formulate the assignment where "if" is really needed. It if was the goal, as you assumed (which you could infer from the inquirer's "need to use if statement"), the teacher certainly failed. If so, this would be pretty bad teaching which should not be justified. That's exactly why I wrote that using "if" and "else" is bad in this particular case.

—SA
Put "
==
" while comparing
CurrentEnrolledCourse
with null.

Try this:

C#
public bool IsEnrolled()
        {
            if (string.IsNullOrEmpty(CurrentEnrolledCourse))
            {
                return false;
            }
            else
            {
                return true;
            }
        }


or just modify your code:

C#
public bool IsEnrolled()
{
if (CurrentEnrolledCourse == null)
{
    return false;
}
    else
    { return true;
    }
}


Thanks,
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Mar-16 22:26pm    
I'm really sorry, but answering so extremely simple question, you should not have repeat a pretty silly inquirer's mistake (not really a bug, but...) of writing those "if" and "else".
It should be just
return CurrentEnrolledCourse != null;
For such a long code sample, it just has to be better. :-)
—SA
It should be
C#
if (CurrentEnrolledCourse == null) ...

Moreover, all those "if-else" are just pointless. Did you even think of that? Your expression under "if" is already a Boolean, so why "if"? Do just this:
C#
public bool IsEnrolled() { return CurrentEnrolledCourse != null; }

Isn't that simple? :-)

Now, a little bonus:

Let me tell you one old programmer's trick: instead of if (CurrentEnrolledCourse == null) write if (null == CurrentEnrolledCourse). But why? The meaning and functionality of both fragments is exactly the same. So why? Just think about it. Let's say, you again mistakenly typed "=" (assignment) instead of "==" (comparison for equality). But in the second form, the compiler will give you a clear error message which will help you to spot this problem immediately; and this is because you can really assign null to a variable/member and cannot assign anything to the null, which is the so-called immediate constant.

—SA
 
Share this answer
 
v5
Need double equal

if (CurrentEnrolledCourse == null)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Mar-16 22:27pm    
I'm really sorry, but answering so extremely simple question, you should not have repeat a pretty silly inquirer's mistake (not really a bug, but...) of writing those "if" and "else".
Why did you write this "if"? It should be just
return CurrentEnrolledCourse != null;
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900