p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   Global Access to a Variable ? (http://p2p.wrox.com/showthread.php?t=60185)

navdeep July 17th, 2007 05:58 AM

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.

bijgupt July 18th, 2007 08:49 AM

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

navdeep July 18th, 2007 10:01 AM

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.


David_0223 July 18th, 2007 01:52 PM

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!

navdeep July 18th, 2007 08:51 PM

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.




planoie July 18th, 2007 09:33 PM

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

navdeep July 18th, 2007 09:42 PM

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.




planoie July 18th, 2007 10:22 PM

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

navdeep July 19th, 2007 12:10 AM

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


planoie July 19th, 2007 06:51 AM

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


All times are GMT -4. The time now is 09:13 AM.

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