Home > other >  ERROR : Location of the symbol related to previous error in C# code
ERROR : Location of the symbol related to previous error in C# code

Time:09-17

using System;

public class Student 
{
  string name;         
  int age;   
  
  public string inputName()    
  {
    Console.WriteLine("Enter name :"); 
    string name= Console.Readline();
    return name;
  }
  
  public void showName(string name){
      Console.WriteLine(name);
  }
  
  static void Main(string[] args)
  {
    Student myObj = new Student();
    string name=myObj.inputName();
    myObj.showName(name);
  }
}

Unable to understand what is incorrect in the above code. I want to take name and age of student as input through the method in C# class and then print the same using another method. I was thinking I will create an object in main() and call the getters and setters function from main. Initially, I guessed that the Console.Readline() was causing the issue and therefore tried to look if I was forgetting any library to import for Readline() but could not successfully get the reason behind this reason. I am totally new to c#, kindly help. NOTE: I am using an online compiler for my above C# code so if this error is specific to that case kindly let me know that as well.

CodePudding user response:

The main method must not be inside the Student class

public static void Main(string[] args)
{
    
}
public class Student 
{

}

Following errors:

Student myObj = new Student(); // is correct, creates an instance
string name=myObj.inputName(); // semantic error
myObj.showName(name);

Here you declare a new string and assign it the input name function. The inputName() in it of itself is correct, but you probably meant to have the name assigned to the object field string name;

So the following

public string inputName()
    {
        Console.WriteLine("Enter name :");
        string name = Console.Readline();
        return name;
    }

need to be

public void inputName()
    {
        Console.WriteLine("Enter name :");
        string name = Console.ReadLine();
        this.name = name;
    }

The return type is changed from string to void, as it does not need to return a value. this.name = name assigns the input to the name field on the object.

The function

public void showName(string name)
    {
        Console.WriteLine(name);
    }

is also a semantic error. There are no functional errors, but you do not need a parameter. The (string name) is the input of the function, if you want to show the current name of the object this is where you use a return type without any parameters.

public string showName()
    {
        return name;
    }

So the final revision would be

public class Program
{

public static void Main(string[] args)
{
    Student myObj = new Student();
    myObj.inputName();
    Console.WriteLine(myObj.showName());
}

public class Student
{
    string name;
    int age;
    public void inputName()
    {
        Console.WriteLine("Enter name :");
        string name = Console.ReadLine();
        this.name = name;
    }

    public string showName()
    {
        return name;
    }
}
}
  • Related