Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi friends iam trying this code
C#
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;

}

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...
Posted
Updated 12-Jan-16 20:25pm
v3
Comments
BillWoodruff 13-Jan-16 4:10am    
posting code with obvious spelling errors that will never compile wastes your time ... and ours.

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...
 
Share this answer
 
Comments
User-11630313 13-Jan-16 2:38am    
thnq..for your responce its helpful
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
 
Share this answer
 
v3
Comments
User-11630313 13-Jan-16 2:37am    
thanks for your advise...its working
Sergey Alexandrovich Kryukov 13-Jan-16 2:53am    
Great. My pleasure.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900