繁体   English   中英

当前上下文中不存在 Print() 名称

[英]Print() name doesn't exist in current context

c# 新手需要帮助解决以下错误。 在 Grybai 班中,当前上下文中不存在打印案例“svoris”中的错误。 第三个参数 'Svoris' 给出了一个错误。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;

 namespace C
{


class Grybai
{
    public string name;
    private int Svoris;
    public void Tipas(string nameType, int Weight) { name = nameType; Svoris = Weight; }

    public string GetName() { return name; }
    public int GetWeight() { return Svoris; }

尝试使用 GetWeight 方法,仍然没有...

}

class Program
{
    const string CFd = "..//..//Duom.txt";
    const string CFr = "..//..//Rez.txt";

    //Duomenu nuskaitymas is failo i masyva
    static void Main(string[] args)
    {
        Grybai[] A = new Grybai[10];    //Sukuriam strukturu masyva
        int n = 0;
        Read(A, ref n);
        Print(A, ref n, Svoris);

Svoris 名称在当前上下文中不存在,如何修复?

    }

    static void Read(Grybai[] tarp, ref int n)
    {
        using (StreamReader reader = new StreamReader(CFd))
        {
            string line;
            string[] parts;
            if (File.Exists(CFr)) File.Delete(CFr);
            while ((line = reader.ReadLine()) != null)
            {
                parts = line.Split(' ');
                tarp[n] = new Grybai();
                tarp[n].Tipas(parts[0], int.Parse(parts[1]));
                n++;
            }
        }
    }

    static void Sort(Grybai[] tarpA, int n)
    {
        Grybai tarpB;

        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i < n - 1; i++)
            {
                if (tarpA[i].GetName()[0] > tarpA[i + 1].GetName()[0])
                {
                    tarpB = tarpA[i];
                    tarpA[i] = tarpA[i + 1];
                    tarpA[i + 1] = tarpB;
                }
            }
        }
    }

    static void Print(Grybai[] tarp, int n, int svoris)
    {
        string top = "|-----------------------------------------------------------|\r\n" +
                     "|                     Surusiuoti duomenys                   |\r\n" +
                     "|-----------------------------------------------------------|\r\n" +
                     "|Pavadinimas         |Tipas            |Svoris        |\r\n" +
                     "|-----------------------------------------------------------|";

为了解决“Svoris”上的参数错误,您可以替换如下一行代码。

  Old Code :  Print(A, ref n, Svoris);
  New Code :  Print(A, ref n, A.GetWeight());

根据您的代码,GetWeight() 返回 Svoris 的值。 它服务于预期的目的。

 private int Svoris;

您“Svoris”是“Grybai”课程中的一个私人课程。 您不能像在“程序”类中那样访问它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM