none
How to qualify a parameter variable? RRS feed

  • Question

  • In the following code, how do I accomplish the assignment? Is there anything I can prefix the second "Data" instance in the assignment so that it refers to the parameter variable?

    Test.Data is not used but it can be qualified with this.Data.

    Yes, I know how to accomplish it via other round-about sot of ways. But I prefer the more direct way as I have shown.

    Thanks.

    public static class Test
    {
      public static byte Data = 255;
    
    
      public struct RESULT
      {
        public byte Data;
        ...
      }
    
    
      public static RESULT GetResult( byte Data )
      {
        return new RESULT() { Data /*RESULT.Data*/ = Data /*parameter.Data*/ };
      }
    
    }



    • Edited by Dev10110110 Tuesday, March 19, 2019 7:42 AM
    Tuesday, March 19, 2019 7:35 AM

Answers

  • There's another way of looking at it. If the language is properly designed in the first place, I wouldn't have this problem. What C# needs is a local scope specifier so that local.Data would specify the variable in the local scope. A theoretical universal solution would be:

    outer.Data refers to the the parent of the immediate scope. So outer.Data in the stated code context would refer to parameter.Data or local-variable.Data.

    outer.outer.Data would then refer to Test.Data, etc.

    I know 1001 ways for accomplishing what I needed. But I was looking for a specific way. Since it isn't possible, the answer is therefore: NOT POSSIBLE.

    A NOT POSSIBLE answer is legitimate. This allows the language designers to correct things they have missed. Natural languages are ever evolving and ever improving. There's no reason why computer languages shouldn't be like that.

    I didn't know it wasn't possible. With your help, now I know. Many thanks.


    • Marked as answer by Dev10110110 Wednesday, March 20, 2019 3:47 AM
    • Edited by Dev10110110 Wednesday, March 20, 2019 3:53 AM
    Wednesday, March 20, 2019 3:47 AM

All replies

  • The correct workaround to this is to follow the defacto guidelines that C# developers use when naming variables. Pascal casing is for types and (public) members. Camel casing is for local variables and parameters. Fields are camel cased and prefixed with a "_". The reason we have these guidelines is to avoid the exact problem you're running into.

    By using camel casing for parameters you will never collide with a type's members and hence never have this issue. There is no infrastructure in the language to "identify" a parameter. Some people want to use Hungarian notation on parameters and that would work around it as well but given named parameters in C# this has never been recommended. 

    So, in summary, change your Pascal cased to camel cased and your problem goes away.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, March 19, 2019 1:59 PM
    Moderator
  • There's another way of looking at it. If the language is properly designed in the first place, I wouldn't have this problem. What C# needs is a local scope specifier so that local.Data would specify the variable in the local scope. A theoretical universal solution would be:

    outer.Data refers to the the parent of the immediate scope. So outer.Data in the stated code context would refer to parameter.Data or local-variable.Data.

    outer.outer.Data would then refer to Test.Data, etc.

    I know 1001 ways for accomplishing what I needed. But I was looking for a specific way. Since it isn't possible, the answer is therefore: NOT POSSIBLE.

    A NOT POSSIBLE answer is legitimate. This allows the language designers to correct things they have missed. Natural languages are ever evolving and ever improving. There's no reason why computer languages shouldn't be like that.

    I didn't know it wasn't possible. With your help, now I know. Many thanks.


    • Marked as answer by Dev10110110 Wednesday, March 20, 2019 3:47 AM
    • Edited by Dev10110110 Wednesday, March 20, 2019 3:53 AM
    Wednesday, March 20, 2019 3:47 AM