c#中返回列表的公共函数



我有一段代码,基本上创建了一个对象列表,这段代码将成为稍后更大程序的一部分,我需要一个函数将列表返回给程序。我尝试这样做:

//RETURN LIST FUNCTION
public List<WynikPorownania> Zwrlista()
{
return listatf;
}            

但是它不能完成这项工作,因为它只在这段代码中可用,而且我不能使用公共说明符。下面是我的代码:

using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks + " : " + Ilosc;
}
}
class Program
{
public static void Main()
{
try
{
///////CONNECTION
string conn = "CONNECTION STRING IS HERE";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka INNER JOIN stanmag using(ID_KARTOTEKA) WHERE stanmag.ID_MAGAZYN=10002;";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
if(standysp<0)
{
standysp = 0;
}
listadb.Add(new indexstan { index=index, standysp=standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
Console.WriteLine(listadb.Count);

//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? -1;
}
//zwr("EMPIS_DESKA_FASOLKA");
//READ FROM TXT AND RETURN HIGHER
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";

List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{   
var linia = line.Split("=");
string index = linia[0];
int value = int.Parse(linia[1]);
if(value<0)
{
value = 0;
}
if(zwr(index)==-1)
{
return new WynikPorownania { Indeks = index, Ilosc = (int)zwr(index) };
}
else
{
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}
}).ToList();
//DISPLAY ALL LISTATF CLASSES
foreach (WynikPorownania WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
//RETURN LIST FUNCTION
public List<WynikPorownania> Zwrlista()
{
return listatf;
}
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Nie znaleziono pliku z podanej sciezki. Blad zwrocil: "+ex);
}
catch (FormatException ex)
{
Console.WriteLine("Podaj indeksy i wartosci w formacie 'indeks=wartosc'. Blad zwrocil: "+ex);
}
catch (NullReferenceException ex)
{
Console.WriteLine("Nie podales prawidlowego indeksu. Blad zwrocil: "+ex);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Zly format. Blad zwrocil: "+ ex);
}
}
}
}

编辑:我读了你的一些建议,我想我明白问题是什么了。我这样重新排列代码:

using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
public class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks + " : " + Ilosc;
}
}
class Program
{
public List<indexstan> listadatabase()
{
string conn = "CONNECTION STRING";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka INNER JOIN stanmag using(ID_KARTOTEKA) WHERE stanmag.ID_MAGAZYN=10002;";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
if (standysp < 0)
{
standysp = 0;
}
listadb.Add(new indexstan { index = index, standysp = standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
Console.WriteLine(listadb.Count);
return listadb;
}
public double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? -1;
}
public List<WynikPorownania> lista()
{
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";
List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
int value = int.Parse(linia[1]);
if (value < 0)
{
value = 0;
}
if (zwr(index) == -1)
{
return new WynikPorownania { Indeks = index, Ilosc = (int)zwr(index) };
}
else
{
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}
}).ToList();
return listatf;
}
public static void Main()
{
try
{


}
catch (FileNotFoundException ex)
{
Console.WriteLine("Nie znaleziono pliku z podanej sciezki. Blad zwrocil: "+ex);
}
catch (FormatException ex)
{
Console.WriteLine("Podaj indeksy i wartosci w formacie 'indeks=wartosc'. Blad zwrocil: "+ex);
}
catch (NullReferenceException ex)
{
Console.WriteLine("Nie podales prawidlowego indeksu. Blad zwrocil: "+ex);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Zly format. Blad zwrocil: "+ ex);
}
}
}
}

但是我不认为这是正确的,甚至visual studio也强调了这个错误,说listadb在这个上下文中不存在。我真不知道接下来该怎么办。Ps.抱歉,如果代码不是100%用英语,但我只是一个公司的实习生,我正在按照他们告诉我的方式命名变量和函数,将来做项目时,我一定会记得使用英语:D

你好像把事情搞混了:

public表示声明为public的方法可以被其他对象使用。

你的函数被嵌入到一个方法中,因此,它不是一个public方法:它只是一个函数,被嵌入到一个方法中,因此,它只能从该方法中访问。

我只是把所有东西都扔进一个函数,它工作。如果将来每个人都有这个问题,它看起来是这样的:

using System;
using FirebirdSql.Data.FirebirdClient;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace dokselect
{
class indexstan
{
public string index;
public double standysp;
}
public class WynikPorownania
{
public string Indeks;
public int Ilosc;
public override string ToString()
{
return Indeks + " : " + Ilosc;
}
}
class Program
{
public static List<WynikPorownania> funkcjalista()
{
///////CONNECTION
string conn = "CONNECTION STRING";
FbConnection myConnection = new FbConnection(conn);
FbDataReader myReader = null;
string sql = "select KARTOTEKA.indeks,STANMAG.standysp FROM kartoteka INNER JOIN stanmag using(ID_KARTOTEKA) WHERE stanmag.ID_MAGAZYN=10002;";
FbCommand myCommand = new FbCommand(sql, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
///////LIST lista1
List<indexstan> listadb = new List<indexstan>();
double standysp;
string index;
while (myReader.Read())
{
index = myReader[0].ToString();
standysp = Convert.ToDouble(myReader[1]);
if (standysp < 0)
{
standysp = 0;
}
listadb.Add(new indexstan { index = index, standysp = standysp });
//Console.WriteLine(myReader[0].ToString());
}
myConnection.Close();
Console.WriteLine(listadb.Count);

//RETURN STANDYSP FUNCTION
double zwr(string myIndex)
{
var result = listadb.FirstOrDefault(listadb => listadb.index == myIndex)?.standysp;
return result ?? -1;
}
//zwr("EMPIS_DESKA_FASOLKA");
//READ FROM TXT AND RETURN HIGHER
string path = "C:/Users/Praktykant/Documents/textdocs/dok1.txt";


List<WynikPorownania> listatf = File.ReadAllLines(path).Select(line =>
{
var linia = line.Split("=");
string index = linia[0];
int value = -1;
try
{
value = int.Parse(linia[1]);
}
catch (IndexOutOfRangeException)
{
index += " - ZLE ZAPISANA LINIA";
}
catch (FormatException)
{
index += " - PODANO ZLA WARTOSC";
}

if (zwr(index) == -1)
{
return new WynikPorownania { Indeks = index, Ilosc = (int)zwr(index) };
}
else
{
if (value < 0)
{
value = 0;
}
return new WynikPorownania { Indeks = index, Ilosc = (int)Math.Max(value, zwr(index)) };
}
}).ToList();

//DISPLAY ALL LISTATF CLASSES
foreach (WynikPorownania WynikPorownania in listatf)
{
Console.WriteLine(WynikPorownania);
}
return listatf;

}
public static void Main()
{
funkcjalista();
}
}
}

相关内容

  • 没有找到相关文章

最新更新