저는 C#을 처음 사용하고 있으며 아래 코드가 작동하지 않는 이유에 대해 머리를 쓰려고합니다. 그래서 정적하지 HtmlRequest someVar = new HtmlRequest();
다른 클래스의 메서드에서 변수 반환
반환 SB가 값을 들고 있지만 라인 htmlString = htmlReq.getHtml(uri)
에 hmtmlString
에 반환 아니에요 사용하여 필요한만큼 여러 번 인스턴스화 될 수있는 사용자 정의 클래스 HtmlRequest를 만들기 위해 노력했습니다.
내가 가져 {코드 ... 반환 (SB)를;} 퍼팅 시도했습니다 공용 클래스 HtmlRequest 이후하지만 난 return sb;
에 중단 점을 넣을 경우 변수가 올바른 올바른 구문을
public partial class MainWindow : DXWindow
{
private void GetLinks()
{
HtmlRequest htmlReq = new HtmlRequest();
Uri uri = new Uri("http://stackoverflow.com/");
StringBuilder htmlString = new StringBuilder();
htmlString = htmlReq.getHtml(uri); //nothing returned on htmlString
}
}
public class HtmlRequest
{
public StringBuilder getHtml(Uri uri)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
Do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
return sb;
}
}
를 얻을 수 없다 그러나 그것을 돌려 보내지 않고있다. 아마 뭔가 분명합니다. 누군가가 왜 작동하지 않고 어떻게 고칠 수 있는지 설명 할 수 있습니까?
이 당신에게
메소드를 즉시 종료하는 대신 값을 사용하십시오. 최적화 된 빌드가 사용되지 않으면 반환 값을 저장하지 않습니다. –
방금 시도한이 컴퓨터에서 작동합니다 ..? – Thousand
@AustinSalonen - 고맙습니다. 임시 줄'string pause;에 중단 점을 넣고 확인했습니다. 실제로 변수를 사용하면 값이 있습니다. 그건 좀 짜증나! 이 문제가 발생하는 이유는 무엇입니까? 이 질문을 답으로 추가하면 받아 들일 것입니다. – user3357963