I have a block of code that basically creates a list of objects, and this code will be a part of a bigger program later on, and I need a function that would return the list to the program. I tried something like this:
//RETURN LIST FUNCTION
public List<WynikPorownania> Zwrlista()
{
return listatf;
}
But it doesn't do the job since it's only available inside this block of code and I can't use the public specifier. Here is my code :
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);
}
}
}
}
EDIT: I read some of your suggestions and I think I understand what the problem is. I rearranged the code this way:
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
CodePudding user response:
You seem to be mixing up things:
public
means that a method, declared as public
, can be used by other objects.
Your function is embedded inside a method, and as a result, it is not a public
method: it's just a function, being embedded inside a method as and such, it is only accessible from within that method.
CodePudding user response:
I just threw everything into a function and It works. If enybody has this problem in the future, Here is how it looks:
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();
}
}
}