Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 28th, 2009, 09:29 AM
Authorized User
 
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JoinTTT
Exclamation 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
  #2 (permalink)  
Old March 29th, 2009, 06:13 AM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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
  #3 (permalink)  
Old March 29th, 2009, 07:23 AM
Authorized User
 
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JoinTTT
Default 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?
  #4 (permalink)  
Old March 29th, 2009, 04:23 PM
Authorized User
 
Join Date: Mar 2007
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to JoinTTT
Thumbs up 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!
  #5 (permalink)  
Old March 29th, 2009, 05:08 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with static variables behaviour shujamughal ASP.NET 2.0 Professional 4 July 21st, 2007 01:56 AM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Declaring static variables in ASP lakshmi_annayappa ASP.NET 1.0 and 1.1 Basics 1 May 24th, 2007 06:49 AM
Static Variables For HTML Header Parmy XSLT 1 February 20th, 2006 03:46 PM
who destroy static variables(no instance of class) MikoMax J2EE 1 March 31st, 2004 08:01 AM



All times are GMT -4. The time now is 02:03 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.