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

Status
Not open for further replies.
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
 
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);
    }
}
 
Status
Not open for further replies.

About this Thread

  • 3
    Replies
  • 349
    Views
  • 3
    Participants
Last reply from:
zackmark29

Trending Topics

Online now

Members online
1,094
Guests online
1,051
Total visitors
2,145

Forum statistics

Threads
2,274,803
Posts
28,958,462
Members
1,233,434
Latest member
lando028
Back
Top