Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

Is it possible to only use the "?" part of Terenary operators instead of using both ? and :

This error keeps on coming

What I have tried:

using System;

class Students
{
    private int ID;
    private string Name;
    private int PassMark = 35;

    public void SetId(int id)
    {
        if (id < 0)
        {
            throw new Exception("Your id cannot be less than '0'");
        }
        this.ID = id;
    }
    public int GetId()
    {
        return this.ID;
    }
    public void SetName(string name)
    {
        if (string.IsNullOrEmpty(Name) ? throw new Exception
            ("Your Name cannot be 'Null'")) //There is this error here that i cant solve

        this.Name = name;
    }
    public string GetName()
    {
        return string.IsNullOrEmpty(this.Name)? "No Name" : this.Name;
    }
    public int GetPassMark()
    {
        return this.PassMark;
    }
}

class Program
{
    public static void Main()
    {
        Students C1 = new Students();
        C1.SetId(100);
        C1.SetName("John");

        Console.WriteLine("Name = {0} && Id = {1} && PassMark = {2}", C1.GetName(), C1.GetId(), C1.GetPassMark());
    }
}
Posted
Updated 29-Jun-18 2:22am

No, it's not possible. The "?" must have an associated ":".

BTW, it's not required, but it's a good idea to scope the comparison:

C#
return (string.IsNullOrEmpty(this.Name))? "No Name" : this.Name;


and purists will suggest that you scope the entire statement:

C#
return ((string.IsNullOrEmpty(this.Name))? "No Name" : this.Name);
 
Share this answer
 
v2
you are mixing an "if" and a "?" what usually is an alternative way to do an "if". (and forgot a semicolon)

Have you tried
C#
string.IsNullOrEmpty(Name) ? throw new Exception ("Your Name cannot be 'Null'"))  : this.Name = name;

or
C#
if (string.IsNullOrEmpty(Name))
{
   throw new Exception ("Your Name cannot be 'Null'"));
}
else
{
   this.Name = name;
}

?

And using only the first term with "?"... I would not do it. Not sure if even possible, but for clarity reasons I would than use the "if" without "else"
 
Share this answer
 
v2
Comments
NotAComputerScienceStudent 29-Jun-18 10:12am    
The ? thingy looked cooler so i just wanted to see if it was possible
Nelek 29-Jun-18 15:50pm    
As it is said... the best teacher are the own errors, aren't they?
As John Simmons mentions, this is simply not valid C#. You have to follow the syntactical rules of the language. You don't simply get to make them up as you go along :)

That said, if you're using a recent version of C#, I am guessing you maybe want to do the following:

C#
Name = string.IsNullOrEmpty(name) ? throw new Exception("Your Name cannot be 'Null'") : name;


Recent C# allows you to include throws with a ternary operator.

As an aside, it seems you are new to C#. The GetXXX/SetXXX pattern is more of a Java thing. You should probably look at how C# properties work. They formalize this pattern in the C# language.

C#
private string name = "No Name";

public string Name
{
   get => name;
   set => name = value ?? throw new ArgumentNullException("Name");
}


Then, you can simply access the property Name as if its a variable within your code. Note, the code above only excludes null values. If you need to exclude empty values as well, you have a bit more coding to do.

While above remains correct for null values, for empty values you should consider throwing ArgumentException. It is generally not appropriate to simply throw Exception. There is almost always a more meaningful/specific exception. In the rare case there is not one, you should create your own by deriving from Exception.
 
Share this answer
 
v2

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