1

I'm going crazy now ... I can not understand why if I create the variable "server" in the event button2_Click_2, when trying to access it in the event it button3_Click_1 be null.

What should I do to access it in button3_Click_1?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            var config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            HttpSelfHostServer server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}
2
  • 1
    You declared new local variable "server" in button2_Click_2 Commented Jun 7, 2013 at 0:04
  • Just an observation: "in other context" should read "in other scope" ;) Commented Jun 7, 2013 at 0:05

3 Answers 3

4

You are declaring a new variable named server in you Button2_Click_2 method. You need to assign it to the field, so change

HttpSelfHostServer server = new HttpSelfHostServer(config);

into

server = new HttpSelfHostServer(config);
1

Note the removal of both the local variable declarators var and HttpSelfHostServer in HttpSelfHostServer:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using TCCWindows.Lib;
using System.Web.Http.SelfHost;
using System.Web.Http;


namespace TCCWindows
{
    public partial class FormPrincipal : Form
    {   
        HttpSelfHostServer server;
        HttpSelfHostConfiguration config;

        public FormPrincipal()
        {
            InitializeComponent();
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            config = new HttpSelfHostConfiguration(textBox1.Text);

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });
            server = new HttpSelfHostServer(config);
            server.OpenAsync();              
            MessageBox.Show("Server is ready!");  
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            server.CloseAsync();    
        }
    }

    public class ProductsController : ApiController
    {

        public string GetProductsByCategory(string category)
        {
            return (category ?? "Vazio");
        }
    }
}
0
1

You're referring to server in both button clicks, but at no point do you actually instantiate the object.

After instantiating config in button2_Click_2, you'll want to also instantiate server:

server = new HttpSelfHostServer(config);

But if the button3_Click_1 event runs before button2_Click_2, you'll still get an exception thrown because server will still be null.

Unless you have some way of enforcing which button is clicked, you might want to move the objects being instantiated into the constructor so you're sure they aren't null when you reference them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.