C
C#2y ago
Cinthia

❔ 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
mtreit
mtreit2y ago
"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.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
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.
Cinthia
CinthiaOP2y ago
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 🙂
mtreit
mtreit2y ago
I don't see what anything in that code has to do with handling null.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
Like I said, maybe they are looking for not hard-coding the same string literal multiple times.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
I was thinking maybe they want something like this:
class MyAwesomeClass
{
const string NA = "N/A";

public int AwesomeNumber { get; set; }
public string? AwesomeString { get; set; } = NA;
public string? EvenMoreAwesomeString { get; set; } = NA;
}
class MyAwesomeClass
{
const string NA = "N/A";

public int AwesomeNumber { get; set; }
public string? AwesomeString { get; set; } = NA;
public string? EvenMoreAwesomeString { get; set; } = NA;
}
...but it they said not to use default values then I don't know what they think is a cleaner way to do it.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
Ask for a hint.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
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.
class MyAwesomeClass
{
const string NA = "N/A";

string? _awesome;

public int AwesomeNumber { get; set; }
public string? AwesomeString
{
get
{
return _awesome == null ? NA : _awesome;
}
}
}
class MyAwesomeClass
{
const string NA = "N/A";

string? _awesome;

public int AwesomeNumber { get; set; }
public string? AwesomeString
{
get
{
return _awesome == null ? NA : _awesome;
}
}
}
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)
indigo
indigo2y ago
They might be looking to separate the value from the display, so that you could still assign null to the backing field. E.g.
class MyAwesomeClass
{
const string NA = "N/A";
string? _awesome;

public string AwesomeString => _awesome ?? NA;

public MyAwesomeClass(string? awesome) => _awesome = awesome;
}
class MyAwesomeClass
{
const string NA = "N/A";
string? _awesome;

public string AwesomeString => _awesome ?? NA;

public MyAwesomeClass(string? awesome) => _awesome = awesome;
}
Cinthia
CinthiaOP2y ago
it is possible thank you guys i will try this approach
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.
Cinthia
CinthiaOP2y ago
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
mtreit
mtreit2y ago
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.
Cinthia
CinthiaOP2y ago
I will, thank you 👋
jcotton42
jcotton422y ago
I have a feeling this professor hasn't done "real world" work in a while
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