Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello i have this class but iam still having the below warring in visual studio while i am using set accssor to set the name feild

Quote:
Severity Code Description Project File Line Suppression State Warning (active) CS8618 Non-nullable field '_name' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. C#ForBeginners D:\Projects\C#ForBeginners\C#ForBeginners\Program.cs 15 `


public class Student
{
    private int _id;

    private string _name;

    private int _PassMark = 35;


    public int ID
    {
        set
        {
            if (value < 0)
            {
                throw new Exception("id feild can be negative value");
            }

            _id = value;
        }
        get
        {
            return _id;
        }
    }

    public string Name
    {
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                _name = "No Name";

            }

            _name = value;
        }
        get
        {
            return _name;
        }
    }

}


What I have tried:

what i have tried is that i added the `?` beside the field to make the compiler understand that the feild accepts null values;
Posted

It's telling you that _name needs to have a value assigned to it by the time the Student class constructor is finished executing.

All you have to do is change
C#
private string _name;

to assign an Empty string to it or some other appropriate default value.
C#
private string _name = string.Empty;
 
Share this answer
 
To add to what Dave has said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited nearly 2 hours for Dave to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 
Comments
Member 14479161 27-May-24 12:51pm    
i am not having a problem in solving the compiler error, the compiler message is clear and I have read it and i know how to solve it but i was wondering from the behaviour why would that happens,

Note: if you have read the part What I have tried in that post you will see that i understand the error and I solve it but I am just wondering the behaviour

what i have tried is that i added the `?` beside the field to make the compiler understand that the feild accepts null values;

A simple solution to your problem is to assign the default value of _name in your class definition; private string _name="No Name". My preference would be to limit the class definition to assignments only. In your definition you are both testing the input and then assigning it. In strict terms, that runs contrary to the single responsibility principle.

 
Share this answer
 

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