我有一個代碼塊,它基本上創建了一個物件串列,這段代碼稍后將成為更大程式的一部分,我需要一個將串列回傳給程式的函式。我試過這樣的事情:
//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);
}
}
}
}
But I don't think that it's correct, even visual studio highlights the mistake, saying that listadb doesn't exist in this context. I don't really know where to go from here. Ps. Sorry if the code isn't 100% in english but I'm just a trainee at a company and I'm naming variables and functions how they told me to, when doing projects in the future I will surely remember to use English :D
uj5u.com熱心網友回復:
你似乎把事情搞混了:
public意味著宣告為 的方法public可以被其他物件使用。
您的函式被嵌入到一個方法中,因此,它不是一個public方法:它只是一個函式,被嵌入到一個方法中,只能從該方法中訪問它。
uj5u.com熱心網友回復:
我只是把所有東西都扔進了一個函式中,它起作用了。如果將來有人遇到這個問題,它的外觀如下:
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();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/340910.html
