Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 17th, 2007, 05:58 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global Access to a Variable ?

I have a confusion regarding designing/coding my classes.
My application has a login form. It uses user class. This user class fetches the user_code,user_name,_user_pass,user_type from DB (access). Now I check user_pass with the password entered by user on login screen. All well until now.

Now I want a global access to user_code and user_type on all my forms. What would be the easiest way to do that.
Reply With Quote
  #2 (permalink)  
Old July 18th, 2007, 08:49 AM
Friend of Wrox
 
Join Date: May 2006
Location: Delhi, Delhi, India.
Posts: 106
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your user class already contains the value of password, so why u don't match the value entered by the user with this one.

Actully u need to pass the user_name and password to db and check whether any record exists or not.
qery - Select cout(*) from UserTanle where user_name = @uname and user_pass = @upass

Bijgupt
Reply With Quote
  #3 (permalink)  
Old July 18th, 2007, 10:01 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes this is what I am doing. It works for me.
But my problem is I need user_code throughout the application once it is authenticated by matching user_name and user_password. How should I make it available globally keeping in mind the OOPs concepts.

Thanks.

Reply With Quote
  #4 (permalink)  
Old July 18th, 2007, 01:52 PM
Authorized User
 
Join Date: Nov 2006
Location: Valparaiso, IN, USA.
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

You must have a main class that launches your application such as Program.cs. If you add a private static variable and a public statuc 'get' property to that class it will be available throughout your application.

I.E. :

static class Program {
  ...
  static private string m_UserCode = "";
  ...
  static public string UserCode {
    get {
      return m_UserCode;
    }
  }
} // end of class Program

You can set the variable from within class Program but have a read only global value available.

If you need to set the UserCode from another class you could do it if you made the variable protected.



What you don't know can hurt you!
Reply With Quote
  #5 (permalink)  
Old July 18th, 2007, 08:51 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by David_0223
 You must have a main class that launches your application such as Program.cs. If you add a private static variable and a public statuc 'get' property to that class it will be available throughout your application.

I.E. :

static class Program {
  ...
  static private string m_UserCode = "";
  ...
  static public string UserCode {
    get {
      return m_UserCode;
    }
  }
} // end of class Program

You can set the variable from within class Program but have a read only global value available.

If you need to set the UserCode from another class you could do it if you made the variable protected.



What you don't know can hurt you!
Thanks David. I have tried similiar thing. I created a another class in my Business Object layer called clsUserDetails(static) with two static public properties ie. user_code and user_type which I need throughout the application.

Now I have two classes clsUser ( with all required properties ) and clsUserDetails ( with two static properties ). I need to instantiate clsUser so it is not a static class. Now my question is

1) Is it a good OOPS practice to have a repeat user_code and user_type in both classes. This is my main concern.

2) Anybody can accidentally change user_code and user_type throughout the life of application. It needs to be set only once. Should that be readonly properties and set in clsUserDetails constructor.

Thanks.



Reply With Quote
  #6 (permalink)  
Old July 18th, 2007, 09:33 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

My suggestions would be to use a single instance of your user details class. Set the appropriate values when the class is constructed. Modify the constructor of the necessary forms such that you must pass user details into it. Each form could have a private instance of the user details or you could abstract that out to a base form class from which all applicable forms are derived. I believe this would be the best approach because it keeps your other forms decoupled from external dependencies (such as static class vars or hooks back into a parent form).

-Peter
Reply With Quote
  #7 (permalink)  
Old July 18th, 2007, 09:42 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 My suggestions would be to use a single instance of your user details class. Set the appropriate values when the class is constructed. Modify the constructor of the necessary forms such that you must pass user details into it. Each form could have a private instance of the user details or you could abstract that out to a base form class from which all applicable forms are derived. I believe this would be the best approach because it keeps your other forms decoupled from external dependencies (such as static class vars or hooks back into a parent form).

-Peter
Thanks Peter.
Can you please elaborate a bit on these


Quote:
quote:My suggestions would be to use a single instance of your user details class.
Quote:
quote:
Each form could have a private instance of the user details or you could abstract that out to a base form class from which all applicable forms are derived.
How can I ensure this ?

- Do you agree with two classes with user_code and user_type repeating in both the classes.



Reply With Quote
  #8 (permalink)  
Old July 18th, 2007, 10:22 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

From what you have explained so far, it sounds like you need to pass around the user detail information throughout the application so my approach would be to pass the user details class around to the forms that need it.

The form you do the login on creates the instance of the user details class. Presumably, you are then launching other forms from that. So these other forms have constructors that expect an instance of the user details. Short of writing the code for you I'm not sure I can elaborate more.

Regarding the second bit, you need to create a class that extends the base windows form class (the class that your forms normally inherit). This class will have an instance of the user details. Then your forms will extend that class instead of the default form class so they get to use that user details instance.

I'm not sure I follow what you mean about two classes with user code and user type. Generally, I'd say you should avoid this if they are holding the same data.

-Peter
Reply With Quote
  #9 (permalink)  
Old July 19th, 2007, 12:10 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 From what you have explained so far, it sounds like you need to pass around the user detail information throughout the application so my approach would be to pass the user details class around to the forms that need it.

The form you do the login on creates the instance of the user details class. Presumably, you are then launching other forms from that. So these other forms have constructors that expect an instance of the user details. Short of writing the code for you I'm not sure I can elaborate more.

Regarding the second bit, you need to create a class that extends the base windows form class (the class that your forms normally inherit). This class will have an instance of the user details. Then your forms will extend that class instead of the default form class so they get to use that user details instance.

I'm not sure I follow what you mean about two classes with user code and user type. Generally, I'd say you should avoid this if they are holding the same data.

-Peter
Hello Peter,

I will try to make it more clear. I will frame my question in a different way. I need a user class.

I am creating a winform application with VS2005. These application has a login screen apart from other screen. Login screen authenticates matching username and password with database. The class should have properties like user_code, user_name,password and user_type.

Now how should I design the three layer ie BO,BL,DAL so that

1. I have global access to two properties user_code and user_type
2. These properties should be set just once in the life of application ie once set the developer should not be able to set it again.

I hope it makes sense.

Navdeep

Reply With Quote
  #10 (permalink)  
Old July 19th, 2007, 06:51 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, I understand what you are explaining. I think I have outlined a way of doing this. The suggestions I have made eliminate the need for "global access" because you hand around an instance of the user's credentials and information from form to form as you instantiate the forms.

Regarding the three layer design question: That might be better suited for another thread. It seems this one is particular to the handling of user info in the app forms.

Are you struggling with the general concepts I have described or the specific code? I don't have the free time to write the application for you (i.e. implement a general concept), but I'll certainly provide assistance if I know specifically what you need help with.

-Peter
Reply With Quote
Reply


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
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
access javascript global variable in code behind dsekar_nat ASP.NET 1.0 and 1.1 Professional 1 March 14th, 2006 01:39 AM
global variable melvik C# 37 March 2nd, 2005 02:07 PM



All times are GMT -4. The time now is 05:34 PM.


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