Przerobienie kodu C# na VC++

0
        private void pobierzDaneNBP()
        {
            System.Net.WebClient wb = new System.Net.WebClient();
            wb.DownloadFile("http://www.nbp.pl/Kursy/xml/dir.txt", "dir.txt");

            StreamReader objReader = new StreamReader("dir.txt");

            String sLine = "";

            DataTable files = new DataTable();

            files.Columns.Add("name");

            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                {

                    if (sLine.StartsWith("a")) //NBP posiada kilka rodzai standardu zapisu, rozróżnia się je po pierwszej literze pliku
                    {

                        DataRow dr = files.NewRow();

                        dr["name"] = sLine;

                        files.Rows.Add(dr);

                    }

                }

            }
            objReader.Close();
            string actual_file = files.Rows[files.Rows.Count - 1]["name"].ToString();
            DataSet ds = new DataSet();

            ds.ReadXml("http://www.nbp.pl/kursy/xml/" + actual_file + ".xml");


            dataGridView1.DataSource = ds.Tables[1];
        }
 

Czy trudno będzie przerobić ten kod na Visual C++? Czy VC++ są też listy? Czy jest parsowanie stringów do int w taki sam prosty sposób w C# to znaczy string s; int.Parse(s)?

0

1.Listy są-std::list
2.Parsowanie stringa musisz zrobić na piechotę.W C++ std::string niestety nie posiada narzędzia do sparsowania na int.

1
MasterBLB napisał(a)

1.Listy są-std::list
2.Parsowanie stringa musisz zrobić na piechotę.W C++ std::string niestety nie posiada narzędzia do sparsowania na int.

O.o RLY?
Przykłady:
*

atoi(str.c_str());
stringstram(str) >> i
sscanf(str.c_str(), "%d", &i);
0

Próbuje przepisać to na C++/CLI, ale słabo mi coś to idzie. Za bardzo C++ nie znam;/. Nie wiem jak to zastąpić DataTable files = new DataTable();. Jeszcze jedno pytanie co znaczy ten znaczek ^?

		void pobierzDane()
		{
			String^ remoteUri = "http://www.nbp.pl/Kursy/xml/";
			String^ fileName = "dir.txt", ^myStringWebResource = nullptr;
			// Create a new WebClient instance.
			WebClient^ myWebClient = gcnew WebClient;
			// Concatenate the domain with the Web resource filename.
			myStringWebResource = String::Concat( remoteUri, fileName );
			// Download the Web resource and save it into the current filesystem folder.
			myWebClient->DownloadFile( myStringWebResource, fileName );
		}

		void wczytajDaneDoTabeli()
		{
			String^ fileName = "dir.txt";
			StreamReader^ din = File::OpenText(fileName);
			String^ str;
			//DataTable^ files = new DataTable(); //jak stworzyć DataTable w C++/CLI
			int count = 0;
			while ((str = din->ReadLine()) != nullptr) 
			{
				count++;
					
			}
		}
 
0
 
			DataTable^ files = gcnew DataTable(); 
			files.Columns.Add("name"); //tutaj nie wiem jak poprawnie zamienić tą linikę kodu

Podpowie ktoś, jak to zastąpić?

0
		void wyswietlWalutyWcomboBox(ComboBox^ cBNazwaWaluty) //funkcja wyświetlająca dane w comboBoxach
		{
			try
			{
				for (int i = 0; i < dataGridView1->Rows->Count; i++)
				{
					if(dataGridView1->Rows[i]->Cells[0]->Value!=nullptr)
						cBNazwaWaluty->Items->Add(dataGridView1->Rows[i]->Cells[0]->Value->ToString());
				}
			}
			catch(Exception^)
			{
				MessageBox::Show("Nie udało się wyświetlić listy walut.");
			}
		}
 
 
		void odczytajInformacjeOwalucie(int^ index, Label^ lPrzelicznik, Label^ lkodWaluty, Label^ lKursSredni)
        {
            if (dataGridView1->Rows[index]->Cells[0]->Value != nullptr)
            {
				lPrzelicznik->Text = dataGridView1->Rows[index]->Cells[1]->Value->ToString();
			}
        }

Dlaczego w wyswietlWalutyWcomboBox if(dataGridView1->Rows[i]->Cells[0]->Value!=nullptr) nie powoduje błędów, a w odczytajInformacjeOwalucie pokazuje błąd? Jeśli usunę funkcje odczytajInformacjeOwalucie, nie pokazują się żadne błędy i program działa, a przecież kod obu w funkcjach jest podobny, a nawet taki sam w niektórych miejscach.
Treść błędu:

1>------ Build started: Project: PrzelicznikWalutInC, Configuration: Debug Win32 ------
1>  PrzelicznikWalutInC.cpp
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(99): error C2660: 'System::Windows::Forms::DataGridView::Rows::get' : function does not take 1 arguments
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(99): error C2664: 'System::Windows::Forms::DataGridViewRowCollection::default::get' : cannot convert parameter 1 from 'System::Int32 ^' to 'int'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(99): error C2660: 'System::Windows::Forms::DataGridViewRowCollection::default::get' : function does not take 0 arguments
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(99): error C2065: 'Cells' : undeclared identifier
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(99): error C2227: left of '->Value' must point to class/struct/union/generic type
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2660: 'System::Windows::Forms::DataGridView::Rows::get' : function does not take 1 arguments
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2664: 'System::Windows::Forms::DataGridViewRowCollection::default::get' : cannot convert parameter 1 from 'System::Int32 ^' to 'int'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2660: 'System::Windows::Forms::DataGridViewRowCollection::default::get' : function does not take 0 arguments
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2065: 'Cells' : undeclared identifier
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2227: left of '->Value' must point to class/struct/union/generic type
1>c:\users\marcin\documents\visual studio 2010\projects\przelicznikwalutinc\przelicznikwalutinc\Form1.h(101): error C2227: left of '->ToString' must point to class/struct/union/generic type
 
2

cannot convert parameter 1 from 'System::Int32 ^' to 'int'

w parametrze funkcji powinno być int index a nie int^ index.

int to int, a int^ to „uchwyt na inta” – brak bezpośredniego odpowiednika takiej konstrukcji w C#.

1 użytkowników online, w tym zalogowanych: 0, gości: 1