Je veux faire un setter et un getter dans un WebService. Le getter marche mais pas le setter sauf en mettant l'attribut à setté en static .

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
 
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Etat : System.Web.Services.WebService
{
    private bool state = true;
 
    public Etat()
    {
 
        //Supprimez les marques de commentaire dans la ligne suivante si vous utilisez des composants conçus 
        //InitializeComponent();
    }
 
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
 
    [WebMethod]
    public void setState(bool b)
    {
        state = b;
    }
 
    [WebMethod]
    public bool getState()
    {
        return state;
    }
}
L'application client :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ModifEtat
{
    public partial class Form1 : Form
    {
 
        private MonServiceWeb.Etat etat;
 
        public Form1()
        {
            etat = new MonServiceWeb.Etat();
            InitializeComponent();
        }
 
        private void buttonTrue_Click(object sender, EventArgs e)
        {
            etat.setState(true);
        }
 
        private void buttonFalse_Click(object sender, EventArgs e)
        {
            etat.setState(false);
        }
 
        private void buttonGetEtat_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this,"etat: " + etat.getState());
        }
    }
}