Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a region of code "read only" in visual studio?

Every so often, I'm done modifying a piece of code and I would like to "lock" or make a region of code "read only". That way, the only way I can modify the code is if I unlock it first. Is there any way to do this?

like image 589
danmine Avatar asked Jan 25 '09 19:01

danmine


3 Answers

The easiest way which would work in many cases is to make it a partial type - i.e. a single class whose source code is spread across multiple files. You could then make one file read-only and the other writable.

To declare a partial type, you just use the partial contextual keyword in the declaration:

// MyClass.First.cs:
public partial class MyClass
{
    void Foo()
    {
        Bar();
    }

    void Baz()
    {
    }
}

// MyClass.Second.cs:
public partial class MyClass
{
    void Bar()
    {
        Baz();
    }
}

As you can see, it ends up as if the source was all in the same file - you can call methods declared in one file from the other with no problems.

like image 102
Jon Skeet Avatar answered Oct 07 '22 22:10

Jon Skeet


Compile it into into a library dll and make it available for reference in other projects.

like image 40
rick schott Avatar answered Oct 07 '22 20:10

rick schott


Split up the code into separate files and then check into a source control system?

like image 35
Douglas Leeder Avatar answered Oct 07 '22 20:10

Douglas Leeder