|
TwojePC.pl © 2001 - 2025
|
 |
A R C H I W A L N A W I A D O M O Ś Ć |
 |
| |
|
C#-przedszkole- osadzenie zmiennej , carlosA 29/04/18 22:32 Nie ma nic gorszego niż stary dziad, który po latach niedotykania programowania wyobraził sobie, że jednak może sobie pisać programy w c#, co gorsza irytuje się bo właściwie nie wiem, jak zadać google trywialne pytanie. Do rzeczy, jest sobie program:
//wyciąłem using i inne niepotrzebne
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c wmic cpu get name",
UseShellExecute = false,
RedirectStandardOutput = true,
};
using (Process process = Process.Start(start))
{
using (var reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
Console.ReadLine();
}
}
no i ok to sobie działa, jednak chciałbym string result poza powyższą strukturą i nie mogę, dlaczego? (a inaczej -czego z podstaw nie zrozumiałem). Bardziej literalnie, chciałbym użyć result w dalszej części programu tj. np.
string result2 = result;
Console.Writeline(result);
kończy się to błędem The name 'result' does exist in the current context.
Jak zdefiniować tego stringa, aby mógł go używać nadal i czego nie rozumiem z podstaw?
#whatever - może , Zajkos 29/04/18 22:41
bo result jest zadeklarowany w bloku { } i poza nim jest out of scope?- hmmm... , carlosA 29/04/18 23:30
rozumiem, że po wyjściu z using obiekt zostaje zwolniony (nie tyle rozumiem, co doczytałem), jednak jak w takim razie przekazać zmienną dalej?#whatever - no to zadeklaruj ja przed blokiem using , RaPToRR 29/04/18 23:45
np
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c wmic cpu get name",
UseShellExecute = false,
RedirectStandardOutput = true,
};
using (Process process = Process.Start(start))
{
string result = null;
using (var reader = process.StandardOutput)
{
result = reader.ReadToEnd();
Console.Write(result);
Console.ReadLine();
}
//tutaj mozesz z zmienna result robic cos dalej ;)
}http://www.krzysztofwasko.pl - albo jeszcze wyzej , RaPToRR 29/04/18 23:49
jesli chcesz wyjscia poza scope pierwszego using
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c wmic cpu get name",
UseShellExecute = false,
RedirectStandardOutput = true,
};
string result = null;
using (Process process = Process.Start(start))
{
using (var reader = process.StandardOutput)
{
result = reader.ReadToEnd();
Console.Write(result);
Console.ReadLine();
}
}
//tutaj mozesz z zmienna result robic cos dalej ;)http://www.krzysztofwasko.pl - no, niestety , carlosA 30/04/18 00:14
próby deklaracji przed result = reader.ReadToEnd(); gdziekolwiek by one nie były kończą się:
Severity Code Description Project File Line Suppression State
Error CS0136 A local or parameter named 'result' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
#whatever - użyj , myszon 30/04/18 07:39
innej nazwy- a w jaki sposób miałoby to pomóc? , carlosA 30/04/18 14:09
chyba, że źle Cię zrozumiałem. Jeśli użyję np. w obrębie using{} string result2 = result; to nie da żadnego efektu, bo zmienna znika wraz z zakończeniem zadania w obrębie using {...}.#whatever
- oczywiście dotarłem do tego... , carlosA 30/04/18 14:21
https://stackoverflow.com/...e-the-using-statement
dlaczego to nie działa nie wiem, prawdopodobnie dlatego że wywołanie dotyczy zewnętrznego programu, który kompilator musi w using zamknąć (wraz z zmiennymi)#whatever - hmmm , Zajkos 30/04/18 18:39
coś chyba namieszałeś, np. deklarujesz zmienną na zewnątrz i w środku.
Przekleiłem Twój kod, przeniosłem deklarację zmiennej na samą górę i działa, np:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string result;
ProcessStartInfo start = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c wmic cpu get name",
UseShellExecute = false,
RedirectStandardOutput = true,
};
using (Process process = Process.Start(start))
{
using (var reader = process.StandardOutput)
{
result = reader.ReadToEnd();
//
}
}
Console.Write(result);
Console.ReadLine();
}
}
}- czeski błąd -podwójna deklaracja stringa , carlosA 1/05/18 14:48
Masz rację, jeden rzeczy nie zauważyłem: dwukrotnie deklarowałem string result. Dzięki.#whatever
|
|
|
|
 |
All rights reserved ® Copyright and Design 2001-2025, TwojePC.PL |
 |
|
|
|