What's new

C# C# Possible null reference argument for parameter 'title' in 'void Posting.setTitle(string title)

heloworldcheck

Enthusiast
Guys, question lang gusto ko lang malaman pano mag set ( gamit ng setter/getter) ng value by user input. ( If possible man or may mali akong approach sa pag gamit ng setters/getters)

class Posting
{
private string _title = "";


public void setTitle( string title)
{

this._title = title;
}
public string getTitle()
{
return _title;
}

class Program
{
static void Main (string[] args)
{
var app = new Posting();
Console.WriteLine("Please input your title: ");

app.setTitle(Console.ReadLine()); // Error: Possible null reference argument for parameter 'title' in 'void Posting.setTitle(string title)
}
}

up
 
Last edited:
You could try auto-implement property
Code:
public string title { get; set; }
or this one
Code:
public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
 
You could try auto-implement property
Code:
public string title { get; set; }
or this one
Code:
public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
Hello, I figured out the problem. It was the VS code itself that was not working properly, I tried using online c# compiler and it worked fine. Now I needed to find out what was causing error in my VS code since I'm using the updated .NET framework.
 
Pwede mong isimply yan using record.
Saka use Console.Write instead of WriteLine para nasa same line lang.


C#:
public record Posting(string Title);

public class Program
{
    static void Main (string[] args)
    {
        Console.Write("Please input your title: ");
        var app = new Posting(Title: Console.ReadLine());
        Console.WriteLine(app.Title);
    }
}
 
Last edited:

Similar threads

Back
Top