当前位置:编程学习 > C#/ASP.NET >>

关于webservice 嵌套类的初始化的问题

1、我在webservice端有个嵌套的类
如 public class A
    {
        private subA aa;
        public subA AA
        {
           set {aa=value;}
           get {return aa;}
         }
     }
   public class subA
    {
     private string b;
     publice string B
       {
         set {b=value;}
         get {retrun b;}
        }
     }
2、客户端声明
   private A a= new A();
   当给AA赋值时报告AA未初始化错误,如果我在客户端但独再声明一下 subA,将其付给AA则错误消失,但我觉得这样是否太累赘,是不是不符合常理啊!是否还有更简单的方法,因为我这种嵌套的类很多,要是每个都需要一一在客户端单独声明的话,那客户端的工作量可就大了!!

请各位大侠赐教!

不知错误描述清楚否,如果不清楚请提问! --------------------编程问答-------------------- 不懂 --------------------编程问答-------------------- 能贴出你客户端赋值代码吗?

注意:
  private A a= new A(); 
AA类型为subA,
如果给a的AA赋值,只能赋subA 类型的
也就是这样:
private subA  b= new subA();
b.B = "B";
然后是
a.AA = b;

你说的问题我写代码测试过了,所以我认为你错就错在赋值的时候了吧。
是不是给AA赋的不是subA类型的了! --------------------编程问答--------------------
引用 2 楼 guoliwang 的回复:
能贴出你客户端赋值代码吗?

 注意:
   private A a= new A();
 AA类型为subA,
 如果给a的AA赋值,只能赋subA 类型的
 也就是这样:
 private subA  b= new subA();
 b.B = "B";
 然后是
 a.AA = b;

 你说的问题我写代码测试过了,所以我认为你错就错在赋值的时候了吧。
 是不是给AA赋的不是subA类型的了!


测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebA
{
    public class WebB
    {
        private string strB;

        public string StrB
        {
            get { return strB; }
            set { strB = value; }
        }

        private string strValueB;

        public string StrValueB
        {
            get { return strValueB; }
            set { strValueB = value; }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebA
{
    public class WebA
    {
        private WebB _WebB;

        public WebB WebB
        {
            get { return _WebB; }
            set { _WebB = value; }
        }
    }
}


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebServiceTest
{
    /// <summary>
    /// Service1 の概要の説明です
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // この Web サービスを、スクリプトから ASP.NET AJAX を使用して呼び出せるようにするには、次の行のコメントを解除します。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public WebA.WebA GetValue()
        {
            WebA.WebA webA = new WebA.WebA();
            WebA.WebB webB = new WebA.WebB();
            webB.StrB = "StrB";
            webB.StrValueB = "StrValueB";
            webA.WebB = webB;

            return webA;
        }
    }
}


客户端:

        private void btnTest_Click(object sender, EventArgs e)
        {
            Service1 service = new Service1();
            WebA webA = new WebA();
            webA.WebB = new WebB();

            webA = service.GetValue();
        }
--------------------编程问答--------------------
引用 3 楼 guoliwang 的回复:
引用 2 楼 guoliwang 的回复:
 能贴出你客户端赋值代码吗?

  注意:
    private A a= new A();
  AA类型为subA,
  如果给a的AA赋值,只能赋subA 类型的
  也就是这样:
  private subA  b= new subA();
  b.B = "B";
  然后是
  a.AA = b;

  你说的问题我写代码测试过了,所以我认为你错就错在赋值的时候了吧。
  是不是给AA赋的不是subA类型的了!


 测试代码:C# codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WebA
{publicclass WebB
    {privatestring strB;publicstring StrB
        {get {return strB; }set { strB= value; }
        }privatestring strValueB;publicstring StrValueB
        {get {return strValueB; }set { strValueB= value; }
        }
    }
}using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WebA
{publicclass WebA
    {private WebB _WebB;public WebB WebB
        {get {return _WebB; }set { _WebB= value; }
        }
    }
}using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;namespace WebServiceTest
{///<summary>/// Service1 の概要の説明です///</summary>    [WebService(Namespace="http://tempuri.org/")]
    [WebServiceBinding(ConformsTo= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]// この Web サービスを、スクリプトから ASP.NET AJAX を使用して呼び出せるようにするには、次の行のコメントを解除します。// [System.Web.Script.Services.ScriptService]publicclass Service1 : System.Web.Services.WebService
    {

        [WebMethod]publicstring HelloWorld()
        {return"Hello World";
        }

        [WebMethod]public WebA.WebA GetValue()
        {
            WebA.WebA webA=new WebA.WebA();
            WebA.WebB webB=new WebA.WebB();
            webB.StrB="StrB";
            webB.StrValueB="StrValueB";
            webA.WebB= webB;return webA;
        }
    }
}


客户端:privatevoid btnTest_Click(object sender, EventArgs e)
        {
            Service1 service=new Service1();
            WebA webA=new WebA();
            webA.WebB=new WebB();

            webA= service.GetValue();
        }



客户端:privatevoid btnTest_Click(object sender, EventArgs e)
        {
            Service1 service=new Service1();
            WebA webA=new WebA();
            webA.WebB=new WebB();

            webA= service.GetValue();
        }
这个改为:
        private void btnTest_Click(object sender, EventArgs e)
        {
            Service1 service = new Service1();
            WebA webA = new WebA();
            webA = service.GetValue();
        }
--------------------编程问答-------------------- 1 你这不是嵌套类
2 的确,服务器端有的,在客户端用得到的,在客户端就得有同样的声明。
3 不想费事,就用服务引用。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,