|
|
 |
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
 | This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

March 28th, 2009, 10:29 AM
|
|
Authorized User
|
|
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Using Static variables in Static Class
Hi!
I have in doubt that How should you use the static variables in static class.
Present Scenario is below.
public static class Class1
{
public static string Metod1
{
Guid A, B, C;
...
...
return "";
}
public static string Metod2
{
Guid A, B, C;
...
...
return "";
}
}
The code in front of me, like above code. So it's weird in my opinion.
because "Guid userId, themeId, packageId;" line is used in every metods which is included Class1.
I convert to this code like below lines.
public static class Class1
{
private static Guid A, B, C;
public static string Metod1
{
...
...
return "";
}
public static string Metod2
{
...
...
return "";
}
}
So I dont have to write down "Guid A, B, C;" this lise in every static metods. Because There approximitly 15 static methods and they contain this line.
What do you say about it? is it correct my idea?
Please reply to my doubt...
Thanks
JoinTTT
|

March 29th, 2009, 07:13 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 151
Thanks: 2
Thanked 32 Times in 32 Posts
|
|
Hi JoinTTT,
The two classes are actually using the Guids in completely different ways.
In your first example, you are creating the variable A, B and C in each method. Although the method is static, these variables are still just local variables within each method and so the Guids in Method1 will be totally separate, and not accessible, to those in Method2.
Your second class is declaring the Guids as static to the whole class and so will refer to the same variable data in every method.
So if you want the values of the Guids to be kept throughout the application and to use the same actual variable data in each Method, then use the second class. If the Guids are used separately in each method, and are just used to create the return value for the method, then use the first class.
If you are unsure then please post a more complete definition of some of the methods and we may be able to help you work out which way to go.
HTH
Phil
|

March 29th, 2009, 08:23 AM
|
|
Authorized User
|
|
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Detail Explanation
Hi Phil,
Thanks for your your reply. As a matter of fact First class is in my project. İt locates in BLL layer. and I think that I can refactor it as i said before. For example when user logged in. User has a guid and these guid is necessary for same mthods that takes Guid parameter.
Is it dangerous these guid variables on top of the class?
|

March 29th, 2009, 05:23 PM
|
|
Authorized User
|
|
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Adding before explanation...
public static class Class1
{
public static string Metod1
{
Guid A, B, C;
bll.validmethod(A,B,C)
...
...
return "";
}
public static string Metod2
{
Guid A, B, C;
bll.validmethod(A,B,C)
...
...
return "";
}
}
Shortly , If you have answer, I would like to learn.
I want to refactor this class.
These guid variables do samething in each metod. Can I use these 3 variables from another way. Because every method include in them.
if this is correct way, NO PROBLEM!
|

March 29th, 2009, 06:08 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 151
Thanks: 2
Thanked 32 Times in 32 Posts
|
|
Hi,
If the Guids are specific to each user, then you should not set them as static in the class, but keep them within each method. Putting them at class-level will mean they get mixed up when different users call the methods and the wrong user's data will be passed to them.
Phil
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |