如何从一个静态方法获取数据到另一个静态方法? [英] how to get data from one static method to another static method?

查看:77
本文介绍了如何从一个静态方法获取数据到另一个静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我尝试使用此代码

hi friends iam trying this code

public static List<banks> getselbnkdetails(int bnkid)
{
    BABanks babanks = new BABanks();
    string name; int id; bool status;
    List<banks> lst = new List<banks>();
    DataTable dt = DABanks.getselbnkdetails(bnkid);

    foreach (DataRow dr in dt.Rows)
    {
        Banks obj = new Banks();
        obj.Bankid = Formats.ConvertToInt(dr["_bnkid"]);
        obj.Bankname = Formats.ConvertToString(dr["_bankname"]);
        name = Formats.ConvertToString(dr["_bankname"]);
        id = Formats.ConvertToInt(dr["_bnkid"]);
        status = Formats.ConvertToBoolean(dr["_active"]);
        obj.Active = Formats.ConvertToBoolean(dr["_active"]);
        lst.Add(obj);
    }
    return lst;

}


上面代码中的
我将值赋给'name','id','status'变量....然后im在同一个类中的另一个静态方法中使用那些变量。但是变量值显示为null ..如何从一个静态方法获取数据?

谢谢...


in the above code i assign value to 'name','id','status' variables....then im using that variables in another static method in same class..but that variables values showing null..why?how to get data from one to another static method?
thank you...

推荐答案

你在这里错过了软件开发的一个非常重要的方面......范围...

你的变量作用于方法并且不能从它外面的任何地方访问过,静态不会改变它!

在某些方面静态方法之间共享数据反对静态的想法,但不禁止,所以如果你真的想做它,您所要做的就是将这些变量移动到类范围...
You have missed here a very important aspect of software development...Scope...
Your variables scoped to the method and for that can not been accessed from anywhere outside from it, and static does not change it!!!
In some way sharing data between static methods oppose the idea of static, but not forbidden, so if you really want to do it, all you have to do is to move those variables to the class scope...


静态方法可以通过方法参数将对象传递给另一个静态方法;对于同一类的方法,它也可以通过这个类的静态成员传递,我不推荐。



至于null,只需使用调试器。显然,结果取决于操作的顺序(你没有全面显示代码),你的任何 Formats.ConvertTo * 都可以返回null ...



这通常是糟糕的代码设计和风格。真正的问题是硬编码的立即常量,例如_bnkid,_ bankname。如果你拼错其中任何一个,编译器将不会警告你。此外,你重复相同的 ConvertTo * 更多一次。这就是我通常所说的反编程。



这是我的结论:只需以简洁的方式重写所有代码,进行一些单元测试和调试每个部分分开。等等...



-SA
Static method can pass an object to another static method via the method argument; in the case of the methods of the same class, it can also be passed via a static member of this class, which I would not recommend.

As to null, just use the debugger. Apparently, the result depends on the order of operation (you did not show code comprehensively), and any of your Formats.ConvertTo* can return null…

This is generally bad code design and style. The real problem is hard-coded immediate constants, such as "_bnkid", "_bankname". If you misspell any of them, the compiler won't warn you. Besides, you repeat the same ConvertTo* more that once. This is what I usually call "anti-programming".

Here is my conclusion: just re-write all your code in neat manner, do some unit testing and debug each part separately. And so on…

—SA


这篇关于如何从一个静态方法获取数据到另一个静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆