Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 只有一个方法不能从另一个项目调用_C#_Methods_Project_Call - Fatal编程技术网

C# 只有一个方法不能从另一个项目调用

C# 只有一个方法不能从另一个项目调用,c#,methods,project,call,C#,Methods,Project,Call,虽然我在这里发现了一些类似的问题,但我没有找到一个能帮助我解决问题的答案。 我有一个解决方案(C#)和两个项目:P1(类库)和P2(控制台应用程序)。我确实在P2中添加了一个引用,并且在P2中使用了语句,并且所有的类都是公共的(在两个项目中)。但是,当我在P2中编写一个方法时,我需要从P1中的类调用一个方法,但实际上无法这样做。P1中任何一个类的所有其他方法我都可以调用,除了这个。有什么问题吗 以下是P1中的课程: using System; using System.Collections.G

虽然我在这里发现了一些类似的问题,但我没有找到一个能帮助我解决问题的答案。 我有一个解决方案(C#)和两个项目:P1(类库)和P2(控制台应用程序)。我确实在P2中添加了一个引用,并且在P2中使用了语句,并且所有的类都是公共的(在两个项目中)。但是,当我在P2中编写一个方法时,我需要从P1中的类调用一个方法,但实际上无法这样做。P1中任何一个类的所有其他方法我都可以调用,除了这个。有什么问题吗

以下是P1中的课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P1
{
    public class Song
    {
        private string name;
        private double length;

        public double Length { get { return length; } set { length = value; } }

        public Song(string name, double length)
        {
            this.name = name;
            this.length = length;
        }

        public  Song ReadSong()
        {
            Console.WriteLine("Song name: ");
            string songName = Console.ReadLine();

            bool wrongEntry = true;
            double songLength = 0;
            while (wrongEntry)
            {
                Console.WriteLine("Song length: ");
                string songLengthStr = Console.ReadLine();
                try
                {
                    songLength = Double.Parse(songLengthStr);
                    wrongEntry = false;
                }
                catch (FormatException)
                {
                    Console.WriteLine("Error!");
                }
            }
            return new Song(songName, songLength);
        }
    }
}
这是P2中的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using P1;

namespace P2
{
    public class Festival
    {

        private string name;
        private List<Artist> listOfArtists;

        public Festival(string name)
        {
            this.name = name;
            listOfArtists = new List<Artist>();
        }

        public Artist ReadArtst()
        {
            Console.WriteLine("Artist name: ");
            string artName = Console.ReadLine();
            Console.WriteLine("Date of birth: ");
            DateTime dat = DateTime.Parse(Console.ReadLine());

            Artist art = new Artist(artName, dat);

            Console.WriteLine("How many songs do you want to enter: ");
            int number = Int32.Parse(Console.ReadLine());
            for (int i = 0; i < number; i++)
            {

            }
            return art;
        }

        static void Main(string[] args)
        {
        }     

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用P1;
命名空间P2
{
公课节
{
私有字符串名称;
艺术家私人名单;
公共节日(字符串名称)
{
this.name=名称;
listOfArtists=新列表();
}
公共艺术家ReadArtst()
{
控制台。WriteLine(“艺术家名称:”);
字符串artName=Console.ReadLine();
Console.WriteLine(“出生日期:”);
DateTime dat=DateTime.Parse(Console.ReadLine());
艺术家艺术=新艺术家(艺术名称,dat);
Console.WriteLine(“您想输入多少首歌曲:”);
int number=Int32.Parse(Console.ReadLine());
for(int i=0;i

在for循环的类Festival(在第二个项目中)中,我需要调用ReadSong()(来自第一个项目的Song类),但由于某些原因,无法调用它。我还尝试使用我拥有的其他方法(publicvoidaddsong(songenesong),它是在Artister类中实现的,并且可以工作)。ReadSong()是我唯一不能为循环调用的方法

从您的评论中,您说您正试图这样调用
ReadSong

art.AddSong(ReadSong());

但这不起作用,因为
ReadSong
Song
类的一种方法

您需要首先实例化该类以调用其上的方法,例如:

Song song = new Song(name, length);
song.ReadSong();

但是您的代码中有更深层次的问题-您的
ReadSong
方法在调用
Song
类时会创建一个新实例。由于我不确定您到底想要实现什么,我只能建议您花一些时间重新思考您的设计,或者阅读C#以更好地理解它。

如果没有看到任何代码,很难说。尝试清理和重建。我的最佳猜测是在某个地方涉及到
接口
。有时这是由于依赖项目未引用所需的程序集造成的。P2项目是针对“客户端框架”吗?清理和重建没有帮助。我没有在这里使用接口。不,它不是针对客户端框架。显示一些代码。没有代码,我们几乎只能猜测。据我们所知,您试图用错误的方法签名调用它。如果没有代码,我们无法知道这是c#的一些模糊的“特性”,还是您的一个简单的打字错误。是的,我试过这样做,但这是不可能的。在ReadArtist()方法中,我既并没有歌曲的名称也并没有歌曲的长度(因为在ReadArtist()中,我试图从键盘而不是歌曲中读取艺术家),如何在其中实例化它?这就是为什么我想知道是否可以将ReadSong()设为静态,这样就不需要实例了。谢谢你的帮助,我知道当你只能看到代码的一部分时,这并不容易。想象一下,是有一首通用的歌,还是有很多不同的歌?一个艺术家能收藏许多歌曲吗?问题在于你的逻辑——回到设计阶段,因为现在你不知道在哪里或如何创作歌曲。艺术家总是有相同的歌曲列表吗?当你创作一个艺术家时,你怎么知道他们有什么歌曲?艺术家的歌曲列表是来自数据库、用户输入还是硬编码?花些时间考虑应用程序中对象之间的关系。
Song song = new Song(name, length);
song.ReadSong();