您的位置:知识库 » .NET技术

类和结构体解析

作者: VisualStudio  来源: 博客园  发布时间: 2008-10-14 23:43  阅读: 6599 次  推荐: 0   原文链接   [收藏]  

系列文章导航:

创建一个示例和WebMethod特性解析

WebService特性和数组类型解析

类和结构体解析

利用YAHOO公开API做天气预报Web服务

Webservice 的设计和模式

Remoting和Webservice的区别


在Web服务中,我们添加GetStuInfo方法,它使用了StuInfo类为传入的strStuNum返回的学生信息的历史数据。代码如下:

        [WebMethod(Description = "通过类返回学生信息的历史数据")]
        
public StuInfo GetStuInfo(string strStuNum)
        
{
            StuInfo stuinfo 
= new StuInfo();

            
//遍历数组,寻找strStuName
            for (int i = 0; i < strStuInfo.GetLength(0); i++)
            
{
                
if (String.Compare(strStuInfo[i, 0], strStuNum, true== 0)
                
{
                    stuinfo.strStuNum 
= strStuNum;
                    stuinfo.strStuYear 
= strStuInfo[i, 1];
                    stuinfo.strStuCollege 
= strStuInfo[i, 2];
                    stuinfo.strStuClass 
= strStuInfo[i, 3];
                    stuinfo.strStuName 
= strStuInfo[i, 5];
                    stuinfo.Info 
= "通过类返回学生相关信息";

                    
//保存StuInfo数据
                    stuinfo.Information[0= new StuInfo.StudentInfo();
                    stuinfo.Information[
0].intStuRank = 5;
                    stuinfo.Information[
0].strStuCollege = "人文法律学院";

                    stuinfo.Information[
1= new StuInfo.StudentInfo();
                    stuinfo.Information[
1].intStuRank = 3;
                    stuinfo.Information[
1].strStuCollege = "化工生物学院";

                    
return stuinfo;
                }

            }

            stuinfo.strStuNum 
= strStuNum;
            stuinfo.strStuName 
= "没有找到人员";
            
return stuinfo;
        }

 

在GetStuInfo方法中,在使用每个类之前都要初始化,遍历strStuInfo数组,找到数据然后返回。类变量是从数组总获得数据的,然后类自身会被返回。如果strStuNum学生学号没有在数组中找到,那么在一个GetStuInfo类的字段中会设置消息,然后被返回。

在浏览器中打开服务,调用GetStuInfo方法,

(1)输入数组中存在的学号(譬如200511020120),将会返回的结果如图:

 

 

(2)输入数组中不存在的学号(随便输了),将会返回的结果如图:

 

在Web服务返回的StuInfo对象中,好像私有字符串strInfo是不可见的。因此,公共方法必须有get和set访问器。如果我们修改了StuInfo类,初始化私有字符串的值,并且去掉set访问器,代码如下:

 

        private string strInfo = "类中初始化私有变量的值";
        
public string Info
        
{
            
get
            
{
                
return strInfo;
            }

        }

 

然后,我们必须把GetStuInfo方法中给Info属性赋值的代码去掉,因为这个公共属性是只读的。
运行Web服务,然后在GetStuInfo方法中设置断点,来验证返回的StuInfo对象,那么调试器将会显示strInfo属性,但是,strInfo属性不会被Web服务返回,因为他没有被读写。

 

0
0
 

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻