locked
C# - Method with an enum as parameter cannot select when using the method. RRS feed

  • Question

  • Good Day Everyone

    I created a class that has method, that is calling the enum as a parameter, check my codes below.

    public enum EncDecType
    {
       Encrypt,
       Decrypt,
    }
    
    public string BasicEncryptDecrypt(string getString, EncDecType GetType)
    {
          some codes.....
    }
    
    
    

    but when I'm using the method shows like this.

    When I put a "ref" it shows like this

    I was a VB.Net developer and I'm converting my codes to C#, but this one becomes a challenge with me, I hope someone can help me on this

    Thanks and regards


    • Edited by Aron29 Wednesday, June 3, 2020 1:11 AM no image
    Wednesday, June 3, 2020 12:26 AM

Answers

  • Hi Aron29,

    Thank you for posting here.

    The "ref" keyword requires that the parameter is an object, not a value.

    For example,

    int i = 1;


    "i" is an object and "1" is a value.

    In your code, EncDecType.Encryp is a value, you should do it like below.

       EncDecType encDecType = EncDecType.Encrypt;
       BasicEncryptDecrypt(str, ref encDecType);
                

    Moreover, the method you call should be the overload method of BasicEncryptDecrypt that Ante said, not the method shown initially.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Aron29 Wednesday, June 3, 2020 4:49 AM
    Wednesday, June 3, 2020 2:42 AM

All replies

  • Greetings Aron.

    We can't see the problems. It looks like you tried to insert pictures into your post but it didn't work. Please try again, or describe what you are seeing in words.

    Wednesday, June 3, 2020 12:45 AM
  • Good Day Ante

    I have updated my post, my apologise.

    Wednesday, June 3, 2020 1:12 AM
  • Those errors only make sense if the method was defined using the 'ref' keyword, like so.

          public string BasicEncryptDecrypt(string getString, ref EncDecType GetType)
          {
             //some codes.....
          }
    Are you sure you are calling the right method, like the one you showed in your first post? Could there be another method with the same name and the 'ref' that you are calling instead by accident?

    Wednesday, June 3, 2020 1:25 AM
  • I see EncDecType GetType. Object.GetType is a method in all objects. If you are using GetType for something else then the compiler is likely confused.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, June 3, 2020 1:31 AM
  • Hi Aron29,

    Thank you for posting here.

    The "ref" keyword requires that the parameter is an object, not a value.

    For example,

    int i = 1;


    "i" is an object and "1" is a value.

    In your code, EncDecType.Encryp is a value, you should do it like below.

       EncDecType encDecType = EncDecType.Encrypt;
       BasicEncryptDecrypt(str, ref encDecType);
                

    Moreover, the method you call should be the overload method of BasicEncryptDecrypt that Ante said, not the method shown initially.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Aron29 Wednesday, June 3, 2020 4:49 AM
    Wednesday, June 3, 2020 2:42 AM