Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 99601

Trying to compile a class using a custom class causes error CS0246

$
0
0

I am struggling with compiling my C# projects from the command line. I've made 2 simple classes to present this problem. I have a Person class and a Registry class that stores an array of Person. When I try to use csc to compile the Registry file, I get "Registry.cs(4,5): error CS0246: The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)"

A clunky workaround I found after some time was to compile the Person.cs file to a .dll file (with csc /target:library Person.cs) and then referencing the library file while I try to compile Registry.cs(csc /r:"./Person.dll" /out:test.exe Registry.cs). This works but doesn't seem like the right workflow at all. If you've encountered anything similar, please help. Below is the source code for both classes.

Person.cs

using System;

namespace ClassTest {

public class Person {
    private string Name {set; get;} 
    private int Age {set; get;}

    public Person(string name, int age) {
        Name = name;
        Age = age;
    }

    public void GetInfo() {
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Age: " + Age + "\n");
    }
}

}

Registry.cs

using System;

namespace ClassTest {

public class Registry {
    Person[] citizens;

    public Registry() {
        citizens = new Person[2];
        citizens[0] = new Person("John", 30);
        citizens[1] = new Person("Mary", 22);
        foreach (Person p in citizens) {
            p.GetInfo();
        }
    }

    public static void Main() {
        Console.WriteLine("");
        Registry r = new Registry();
    }
}

}

Viewing all articles
Browse latest Browse all 99601

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>