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
  #11 (permalink)  
Old July 19th, 2007, 07:33 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


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
Hello Peter,

Its more of general concepts and not about actually writing code.

Navdeep

Reply With Quote
  #12 (permalink)  
Old July 19th, 2007, 09:11 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

I'm not sure how to explain this concept differently than I have. Maybe I can use this analogy:

When I buy a plane ticket I purchase it online. I go to the airport ticket counter and present my ID to the agent who finds my reservation to verify my right to board the plane. She prints me out a ticket. Now I move on to security. They ask for my ticket to verify that I can enter the secure part of the airport. Then I go to the boarding gate. Before I can board the plane I show my ticket again to the gate agent. Then I get on the plane.

In this analogy, I have "login" credentials (my name, maybe a reservation number). The agent verifies that and issues me a printed plane ticket, which I hold. This is like creating a user class instance with the user information in your application. As I move around the different parts of the airport I present my ticket as my authorization to be there. Similarly, as you launch forms of your application, you pass into them (through the constructor) the instance of the user class containing the user's info (ID code, type, etc). The form code uses that information instead of having to check/verify the user's initial credentials.

As far as having two classes holding user data goes. I think I get what you mean with that question. If you want to have a full user class (that presumably would closely mirror the data in the database) as well as a smaller version that would be used as the credentials class, that is up to you. I see no reason not to just use the full user class, that would simplify matters. Of course, you could also make a base class that has the basic information you need to carry around the application and extend that for the full user class that maps to the database.

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

Thanks a lot Peter for sparing time for me and putting efforts here.


Quote:
quote:Originally posted by planoie
 I'm not sure how to explain this concept differently than I have. Maybe I can use this analogy:

When I buy a plane ticket I purchase it online. I go to the airport ticket counter and present my ID to the agent who finds my reservation to verify my right to board the plane. She prints me out a ticket. Now I move on to security. They ask for my ticket to verify that I can enter the secure part of the airport. Then I go to the boarding gate. Before I can board the plane I show my ticket again to the gate agent. Then I get on the plane.

In this analogy, I have "login" credentials (my name, maybe a reservation number). The agent verifies that and issues me a printed plane ticket, which I hold. This is like creating a user class instance with the user information in your application. As I move around the different parts of the airport I present my ticket as my authorization to be there. Similarly, as you launch forms of your application, you pass into them (through the constructor) the instance of the user class containing the user's info (ID code, type, etc). The form code uses that information instead of having to check/verify the user's initial credentials.

I got it what you have tried to convey.
It may sound stupid but still user can instantiate the class in a form or a value of static property can be changed accidentally anywhere in the form. How can we prevent that ?

Quote:
quote:
As far as having two classes holding user data goes. I think I get what you mean with that question. If you want to have a full user class (that presumably would closely mirror the data in the database) as well as a smaller version that would be used as the credentials class, that is up to you. I see no reason not to just use the full user class, that would simplify matters. Of course, you could also make a base class that has the basic information you need to carry around the application and extend that for the full user class that maps to the database.

-Peter
Ya .. using two classes may not be good idea .. this is what my classes look like

Class clsUser
- user_code
- user_name
- user_pass
- user_type

Class clsUserInfo
- user_code (static)
- user_type (static)

I instantiate clsUser during authentication ( login form ). Fetch all properties. Now pass the info to clsUserInfo properties.

clsUserInfo.user_code = myclsUser.user_code
clsUserInfo.user_type = myclsUser.user_type

Now clsUserInfo is available throughout the project. But it does not solve my purpose of accidentally changing values of clsUserInfo properties.

-Navdeep



Reply With Quote
  #14 (permalink)  
Old July 19th, 2007, 02:32 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

As I outline above, if you only set those values in the user class constructor then no one can change them in the properties. Of course, you could create a new instance of a user class, but there isn't much you can do to prevent that.

If you use a class with static properties there is *no* way you can prevent other code from setting the values, because the static getter/setter is the only way to set them. Well, that's not entirely true, you could write a static method on that same class that assigns the value to the static internal variable. But that isn't preventing assignment, just obfuscating it a bit.

I'm not sure that you are understanding my explanations, and I have run out of ways to explain it. You don't need to use static properties on a class if you hand the instance of the class around between the forms. This will prevent other code from assigning values to the properties.

-Peter
Reply With Quote
  #15 (permalink)  
Old July 19th, 2007, 02:34 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

All else being said, you have to establish some usage guidelines if other developers are going to be adding code. There are lots of things any developer could do to screw up an application, you just have outline how things are used. If you use a class with static members, other developers need to understand that they are not to touch those values, just use them. There is only so much you can do.

-Peter
Reply With Quote
  #16 (permalink)  
Old July 19th, 2007, 08:44 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter,

I understand and will keep these points in mind.

Navdeep

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 11:58 PM.


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