Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: what is the "game engine"???? Pin
molesworth14-Sep-11 7:27
molesworth14-Sep-11 7:27 
QuestionGetThreadPreferredUILanguages Pin
marca29213-Sep-11 22:56
marca29213-Sep-11 22:56 
AnswerRe: GetThreadPreferredUILanguages Pin
Luc Pattyn14-Sep-11 1:52
sitebuilderLuc Pattyn14-Sep-11 1:52 
AnswerRe: GetThreadPreferredUILanguages Pin
BobJanova14-Sep-11 2:14
BobJanova14-Sep-11 2:14 
QuestionWhere to use Try/Catch block? [modified] Pin
Adam_Dev13-Sep-11 22:21
Adam_Dev13-Sep-11 22:21 
AnswerRe: Where to use Try/Catch block? Pin
BobJanova13-Sep-11 22:40
BobJanova13-Sep-11 22:40 
GeneralRe: Where to use Try/Catch block? [modified] Pin
Adam_Dev13-Sep-11 23:01
Adam_Dev13-Sep-11 23:01 
GeneralRe: Where to use Try/Catch block? Pin
BobJanova14-Sep-11 2:09
BobJanova14-Sep-11 2:09 
I don't think there's anything particularly wrong with returning from a catch. The rule of thumb is that (as Pete says below) you should catch an exception where you intend to deal with it, and if you consider it dealt with you can return from the function. In this case, the calling code will not know that there was an exception in some lower layer. If you think the calling code should be informed, don't catch the exception and let the calling code deal with it.

What you've done in GetAdmin makes no sense. You're catching the exception you're throwing and doing nothing with it! Don't use exceptions to control the flow of code. If an empty or null user name/role is an error (i.e. in normal operation it should never happen), then throw the exception; if you want it to be valid input, but not an admin, then return false.

The only reason I was using "try { ... } catch { throw; }" was to make sure the original exception was passed on to the top calling method that would handle the exception and log where the exception occurred.

If you don't catch an exception, it automatically propagates up the stack. Handle it where it makes sense to do so, and once only. (Not always true, but generally you should not rethrow an exception.)

Since you included it originally I'm inclined to believe that the null or empty username is an error and GetAdmin, at least, should throw in that case. That means that, logically, it's invalid input to ValidateUser as well, I think, so I'd catch the error at the request level:

// Class1
public void ProcessPerson(Request arg)
{
    try
    {
        if(Class2.ValidateUser(arg.Username, arg.Role)
        {
            //Do something
        }
        else
        {
            // log failure to authenticate
        }
    }
    catch(Exception ex)
    {
        //log the error and create a nice message to the user
    }
}
 
//Class 2:
public static bool ValidateUser(string Username, string Role)
{
    using(MyClass myClass = new MyClass())
    {
        // if ( x ) return true else return false is redundant
        return myClass.GetAdmin(Username, Role);
    }
}
 
// MyClass
public bool GetAdmin(string Username, string Role)
{
    if (String.IsNullOrWhiteSpace(UserName) || String.IsNullOrWhiteSpace(Role))
                throw new ArgumentException("Username/Role cannot be empty or null");

    using(MyClassAdapter myClassAdapter = new MyClassAdapter())
    {
        return myClassAdapter.GetAdmin(Username, Role); 
    }
}


It also seems like there are far too many layers involved in the authentication process. You have Class1 (which receives the request), Class2, MyClass and MyClassAdapter all involved in finding out this simple piece of information (whether a user/role combination is admin). What I typically do is at login time I populate a single UserInfo object which contains the role and user-specific access overrides, and then I can just authenticate like:
if(user != null && user.HasAccess(AccessTypes.Admin)) { ... }

(setting user to null if no user is logged in). HasAccess would look a bit like:
public bool HasAccess(AccessTypes access){
 return access == access & (Access | role.Access & ~RemovedAccess);
}

I'm not sure if you can do that with enums but logically that is what AccessTypes would be, and UserInfo.Access, UserInfo.RemovedAccess and Role.Access would all be of that type. You might have to cast to int or uint to get the bit matching to work.
AnswerRe: Where to use Try/Catch block? Pin
Pete O'Hanlon13-Sep-11 22:57
mvePete O'Hanlon13-Sep-11 22:57 
GeneralRe: Where to use Try/Catch block? [modified] Pin
Adam_Dev13-Sep-11 23:10
Adam_Dev13-Sep-11 23:10 
GeneralRe: Where to use Try/Catch block? Pin
Pete O'Hanlon13-Sep-11 23:50
mvePete O'Hanlon13-Sep-11 23:50 
GeneralRe: Where to use Try/Catch block? Pin
Adam_Dev14-Sep-11 0:26
Adam_Dev14-Sep-11 0:26 
AnswerRe: Where to use Try/Catch block? Pin
Luc Pattyn14-Sep-11 2:11
sitebuilderLuc Pattyn14-Sep-11 2:11 
GeneralRe: Where to use Try/Catch block? Pin
BobJanova14-Sep-11 2:22
BobJanova14-Sep-11 2:22 
GeneralRe: Where to use Try/Catch block? Pin
Adam_Dev14-Sep-11 2:33
Adam_Dev14-Sep-11 2:33 
AnswerRe: Where to use Try/Catch block? Pin
Luc Pattyn14-Sep-11 2:51
sitebuilderLuc Pattyn14-Sep-11 2:51 
JokeArrayList Pin
Subodh Kumar Jain13-Sep-11 20:38
Subodh Kumar Jain13-Sep-11 20:38 
GeneralRe: ArrayList Pin
Wayne Gaylard13-Sep-11 21:04
professionalWayne Gaylard13-Sep-11 21:04 
GeneralRe: ArrayList Pin
PIEBALDconsult14-Sep-11 2:27
mvePIEBALDconsult14-Sep-11 2:27 
GeneralRe: ArrayList Pin
Pete O'Hanlon14-Sep-11 3:51
mvePete O'Hanlon14-Sep-11 3:51 
QuestionError in Custom IIS MMC SnapIn Page ...... Pin
Shazz_Shivang200413-Sep-11 20:23
Shazz_Shivang200413-Sep-11 20:23 
QuestionWho to write HTML code using Csharp Pin
amit_ghosh1813-Sep-11 8:20
amit_ghosh1813-Sep-11 8:20 
AnswerRe: Who to write HTML code using Csharp Pin
Eddy Vluggen13-Sep-11 8:33
professionalEddy Vluggen13-Sep-11 8:33 
AnswerRe: Who to write HTML code using Csharp Pin
Matt Meyer13-Sep-11 8:44
Matt Meyer13-Sep-11 8:44 
AnswerRe: Who to write HTML code using Csharp Pin
Ian Shlasko13-Sep-11 8:52
Ian Shlasko13-Sep-11 8:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.