0

I am struggling to understand why the below code works perfectly well on a computer but not on another. Expected behaviour:

  • When currentUserSAMAccountName is null or empty String.IsNullOrEmpty matches and fill the Conf.OwnerUserName
  • When currentUserSAMAccountName is not null it goes it displays "Current user is:"

Can anyone offer me a second pair of eyes looking at code?

public static Conference GetUpdateConference(this Microsoft.Office.Interop.Outlook.AppointmentItem appointmentItem, string currentUserSAMAccountName) 
{
  // ...
  if (String.IsNullOrEmpty(currentUserSAMAccountName))
    Conf.OwnerUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  else {
    Console.Write("Current user is: " + currentUserSAMAccountName + ".");
    // ...
  }
}

// Call of the function with this.currentOnbehalfUser that might be null or not initialised.
Item.GetUpdateConference(this.currentOnbehalfUser?.GetSAMAccountName());

// result is Current user is: . // No space 

I have check the following references:

  1. Use of documentation of the Null-conditional operators: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-
  2. Similar post :
Salim
  • 495
  • 3
  • 20
  • 3
    https://idownvotedbecau.se/itsnotworking – Uwe Keim Sep 13 '22 at 04:41
  • 1
    Also, reading "[How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)" might help. – Uwe Keim Sep 13 '22 at 04:42
  • do you realise that you haven't explained us what's "working perfectly" versus "not perfectly"? – Pac0 Sep 13 '22 at 04:49
  • 1
    Please edit your question to include the result of `Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(currentUserSAMAccountName))`. – ProgrammingLlama Sep 13 '22 at 04:50
  • You need provide an error stack trace and error message to get detailed information about SAM account. – Alan Turing Sep 13 '22 at 04:53
  • @Artur What are you talking about now? OP clearly states _"// result is Current user is: . // No space"_. Without an exception, where should OP find the error message and stack trace you seek? – ProgrammingLlama Sep 13 '22 at 04:54
  • You need to log string variable passing into the function, on different systems, that is the root cause of the issue – Alan Turing Sep 13 '22 at 04:55
  • Also, in general case, "." has meaning of the machine name, not the user name, and if you are running code under the machine account name, it is possible to get "." instead of the current user principal if code is executed without user profile, aka remote execution, etc. so there are many cases when you get "." in currentSAMaccount name depending of how you invoke execution of your code – Alan Turing Sep 13 '22 at 05:00
  • Thanks for your much appreciated answers. Pac0: Thanks corrected. DiplomacyNotWar: Thanks very useful. I will get the new trace. Artur: there is no error IsNullOrEmpty simply does not match. Not sure to understand your second and third comment. – Salim Sep 13 '22 at 05:12

0 Answers0