C# 数组转换
任何帮助,因为我是 C# 菜鸟。以下代码工作正常并返回 1 个字符串 ViewState2。我希望它返回一个 ViewState2 和 EventValidation2 数组,以便我稍后可以对其进行操作。我如何将下面的代码转换为返回数组?
public string get_status(string local_fname)
{
var dts_doc = new HtmlAgilityPack.HtmlDocument();
dts_doc.Load(local_fname);
//Pull the values
var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");
string ViewState2 = ViewState.Attributes[3].Value;
string EventValidation2 = EventValidation.Attributes[3].Value;
//Display the values
//System.Console.WriteLine(ViewState.Attributes[3].Value);
//System.Console.WriteLine(EventValidation.Attributes[3].Value);
//System.Console.ReadKey();
return ViewState2;
}
Any help here as I'm a C# noob. The following code works fine and returns 1 string ViewState2. I'd like it to return an array of ViewState2 and EventValidation2 so I can manipulate it later on. How would I convert the code below to return an array?
public string get_status(string local_fname)
{
var dts_doc = new HtmlAgilityPack.HtmlDocument();
dts_doc.Load(local_fname);
//Pull the values
var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/@value[1]");
var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/@value[1]");
string ViewState2 = ViewState.Attributes[3].Value;
string EventValidation2 = EventValidation.Attributes[3].Value;
//Display the values
//System.Console.WriteLine(ViewState.Attributes[3].Value);
//System.Console.WriteLine(EventValidation.Attributes[3].Value);
//System.Console.ReadKey();
return ViewState2;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用数组,而使用类。这样做,您不必记住每个元素的含义。
另外,您应该阅读 C# 语言中的编码指南和命名约定,
using
语句也可能很有趣。我纠正了一些“错误”,但可能没有抓住全部。此外,我还重命名了几个变量,以使它们的内容更清晰。您可能还想考虑仅在循环中使用var
关键字,同时使用 LINQ(或一般的匿名类型)或使用非常长的类名。写出类型名称可以大大提高可读性。Don't use an array, but a class. Doing this, you don't have to remember what each element means.
Also, you should read up on coding guidelines and naming conventions in the C# language, also the
using
statement might be interesting. I have corrected some "mistakes", but probably didn't catch all. Also, I have renamed a couple of variables, to make their content clearer. You also might want to look into using thevar
keyword only in a loop, while using LINQ (or anomynous types in general) or with really long class names. Written out type names can increase readability quite a lot.如果您确实想要一个包含
ViewState2
和EventValidation2
的数组,您可以进行以下更改:并且:
也就是说,这确实是“快速而肮脏”的方法,如果您希望此代码可维护,则不太合适(您最后一次阅读有关“返回长度为 2 的数组,其中一个字符串表示 X 作为第一个元素,另一个字符串表示Y 作为第二个”?)。
Femaref 是对的; 正确的做法是将您想要返回的信息封装在它自己的类型中。
If you really want an array with
ViewState2
andEventValidation2
in it, you can make the following changes:And:
That said, this is really the "quick and dirty" approach, and is not really appropriate if you're going to want this code to be maintainable (when's the last time you read documentation on a function that "returns an array of length 2, with a string representing X as the first element and another string representing Y as the second"?).
Femaref's right; the correct thing to do would be to encapsulate the information you want returned in its own type.
假设您对这个问题的回答是“是”(尽管我建议采用不同的方法,请参见下文),这将满足您的要求:
也就是说,我会遵循该方法。
我会编写一个包含您想要的数据成员的类:
然后我会修改该方法以返回数据类的实例。
Assuming you answer yes to this question (although I'd recommend a different approach, see below) this will do what you're asking:
That said, I would follow the approach afte the line.
I'd write a class that has the data members you want:
Then I'd modify the method to return an instance of your data class.
但这似乎是微不足道的答案。请问它能解决您的问题吗?如果不是,您能更好地说明问题吗?
But it seems to trivial as answer. Please Does it solve your problem? If no, can you specify better the question please?