开发者

C# Call function in a class from another class

I'll start of by saying I'm not a developer. Yes this is a c# nightmare. But this is a one time tool and thats it. Quick and Dirty it just needs to work and thats it.

I have the following code:

        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.DocumentNo开发者_如何学运维de.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 new string[] { ViewState2, EventValidation2 };
    }

I want to call get_status from a button on my Main.cs which will show 2 Message Boxes with ViewState2 and EventValidation2.

Again, I'm not a developer, this is probably the wrong way of doing things. But I just need a quick and dirty solution to get this job done once.


Make the function static by adding the static keyword to the function definition:

static public string[] get_status(string local_fname)

Use the class name to reference the function from your Main class.


try this:

foreach(string s in get_status(localFname))
{
    MessageBox.Show(s);
 }

As you said, it is quick and dirty and I stayed faithful to that paradigm.

And yes, if you need to acces another class, make the method static or just simply create an instance and call the method on it. I hope I have understood the problem correctly.


if you are using visual studio, go to the Button you want to click, double-click the button. This will create an eventhandler. In the eventhandler you should call the above method.

protected void Button1_Click(object sender, eventArgs e)
{
   string local_fname = someValue;
   string results[] = get_status(local_fname);
   MessageBox.Show(results[0]);
   MessageBox.Show(results[1]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜