none
Does shadowing through scope work when the programming elements are of different types? RRS feed

  • Question

  • Hi,

    MY question is: 

    If two programming elements have the same name but are of different types and have different scopes, then can one of them shadow the other through scope? For example, in the below case the name TEMP has been assigned to both an integer variable and a sub-routine with different scopes but it is not working. So is there a way to make it work (apart from qualifying it with Module1)...i.e. to make the TEMP integer variable shadow the TEMP() subroutine? OR does shadowing through scope only work when the two programming elements are of the same type?

    Module Module1
    
        Public t As Integer = 42
        Sub temp()
            Console.WriteLine("TEMP from sub printTemp: {0}", t)
        End Sub
    
        Sub Main()
            Dim temp As Integer = 5
            Console.WriteLine("TEMP from sub Main(): {0}", temp)
            temp()  'error comes here. but Module1.temp() works fine
            Console.ReadLine()
        End Sub
    End Module

    Error is as follows:

    PLease help


    Sougata Ghosh



    • Edited by sougata12 Tuesday, December 3, 2019 7:39 PM
    Tuesday, December 3, 2019 7:34 PM

Answers

  • Hi sougata12, 

    Thank you for posting here.

    >> If two programming elements have the same name but are of different types and have different scopes, then can one of them shadow the other through scope?

    No, you cannot. Based on your code, the only way to fix the issue is changing ‘temp()’ to ‘Module1.temp()’.

    It is impossible to shadow a local variable, and the hidden module scoped variable can be referred by qualifying its name(e.g. writing Module1.temp() instead of temp()).

    Here’s a reference you can refer to.

    Scope of variables

    Hope it can help you.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    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 sougata12 Friday, December 6, 2019 4:44 PM
    Wednesday, December 4, 2019 5:45 AM

All replies

  • Hi

    Are you specifically trying to trip over these things?

    If you fully qualify the line to prevent the compiler from thinking you are referencing the Temp dimmed earlier, then it would work - try Module1.Temp()


    Regards Les, Livingston, Scotland

    Tuesday, December 3, 2019 7:50 PM
  • Hi sougata12, 

    Thank you for posting here.

    >> If two programming elements have the same name but are of different types and have different scopes, then can one of them shadow the other through scope?

    No, you cannot. Based on your code, the only way to fix the issue is changing ‘temp()’ to ‘Module1.temp()’.

    It is impossible to shadow a local variable, and the hidden module scoped variable can be referred by qualifying its name(e.g. writing Module1.temp() instead of temp()).

    Here’s a reference you can refer to.

    Scope of variables

    Hope it can help you.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    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 sougata12 Friday, December 6, 2019 4:44 PM
    Wednesday, December 4, 2019 5:45 AM
  • Hi

    Are you specifically trying to trip over these things?

    If you fully qualify the line to prevent the compiler from thinking you are referencing the Temp dimmed earlier, then it would work - try Module1.Temp()


    Regards Les, Livingston, Scotland

    Hi,

    Sorry for this delayed response. To answer your question, yes I am specifically trying to trip over these things. Now you may want to know the reason ..so here goes:

    The reason is that I think one needs to understand the nuances of any concept or alternatives to any particular approach. Hence I keep experimenting by tweaking the basic points to try and understand the limitations or scope of a particular function/command. whenever there is a shred of doubt in my mind, I look out for help here since explanation from experts like you helps me to be sure. I have no access to any teachers. Hence to resolve a doubt/or to be sure that I have understood something thoroughly I have only the forums like these to reach out to. 


    Sougata Ghosh


    • Edited by sougata12 Friday, December 6, 2019 5:00 PM
    Friday, December 6, 2019 4:53 PM
  • Hi

    OK, good work. Lots more to go 😊


    Regards Les, Livingston, Scotland

    Friday, December 6, 2019 5:00 PM