![](/img/trans.png)
[英]The name 'InitializeComponent' doesn't exist in the current context
[英]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.