❔ Change Null Values
I'm working on a project in my course, where I need to change all values that come from the API to my list that are NULL to "N/A".
I made a default value but my professor didn't accept it, he said it's not the most professional way, the code below is close to what he asked, but I still couldn't get to the point. And everything I think about is going to turn into a very big or complicated code, which they already said it's not, I need help!
public class Root
{
public Name name { get; set; }
public string cca3 { get; set; }
public Currencies currencies { get; set; }
public List<string> capital { get; set; }
public string region { get; set; }
public string subregion { get; set; }
public Languages languages { get; set; }
public List<double> latlng { get; set; }
public double area { get; set; }
public string flag { get; set; }
public Maps maps { get; set; }
public int population { get; set; }
public List<string> timezones { get; set; }
public List<string> continents { get; set; }
public Flags flags { get; set; }
public Gini gini { get; set; }
public string giniYear { get; set; }
public double giniValue { get; set; }
public static string ReturnNull(string Value)
{
return Value != null ? Value : "n/d";
}
}
21 Replies
"not the most professional way" ?
What did your original code that the professor did not like look like?
Also you should follow the normal C# style conventions. Property names should not be lowercase, they should use Pascal casing.
I did like public string giniValue {get; set;} = "n/d"
yes, I used Json to C# and everything came like this, as I'm still defining the values that I'm going to show in the project, I haven't changed it yet
You probably don't want to hard-code that string for every value, you can define a constant or static property with the default value of "n/d" and then assign that as the default to each property.
But of course that only applies to strings.
he told me to look at this code in FullName that I could get the idea, but it only confused me more public class Aluno
{
public int Id { get; set; }
public string Nome { get; set; }
public string Apelido { get; set; }
public List<Disciplina> Disciplinas { get; set; }
public string NomeCompleto
{
get
{
return $"{Nome} {Apelido} ";
}
}
NomeCompleto = FullName sorry 🙂
I don't see what anything in that code has to do with handling null.
I don't either, but it was with this orientation that I did the returnNull in the other code, it said I'm getting close, but it seems that it is more confused than with the default value
Like I said, maybe they are looking for not hard-coding the same string literal multiple times.
thank you, I will try to do it that way and send it to him
public string giniYear { get; set; } = ReturnNull();
public double giniValue { get; set; }
} public static string ReturnNull() { return "n/d"; } I sent this code to the professor, and that's not it yet, it's not to be done with default
} public static string ReturnNull() { return "n/d"; } I sent this code to the professor, and that's not it yet, it's not to be done with default
I was thinking maybe they want something like this:
...but it they said not to use default values then I don't know what they think is a cleaner way to do it.
because now he was very clear, do not use the default value, I have no idea what to do, he even said that the way he wants it was simpler, but I don't know what could be simpler than that
Ask for a hint.
I did that, and I got that code where I can look up the full name, as we still have time to deliver the project, he won't tell me anything else, or deduct points from the grade
Are they implying you should implement a traditional getter with a backing field and you check for null in the getter? That would be objectively much worse.
I mean, if that's what they want I would be pretty...unimpressed.
I worry they might be teaching based on much older versions of C# where things like default property initializers did not exist yet (like, before C# 6.0)
They might be looking to separate the value from the display, so that you could still assign null to the backing field.
E.g.
it is possible
thank you guys i will try this approach
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.I discover what he wants, a reading property
public string ReadData{
get
{
return ($"{(name.common == null ? "n/d" : name.common)}, {(name.official == null ? "n/d" : name.official)}, {(region == null ?
"n/d" : region)}");
}
}
i really dont understand how this could be better than default value
It's not.
In fact it goes against the idea of eliminating the possibility of null in your code, which is one of the big features in modern C# - nullable reference types.
Ask them why they think this is better.
I will, thank you 👋
I have a feeling this professor hasn't done "real world" work in a while
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.