Curly Bracket Quirk

ellpee

Active member
Joined
Apr 16, 2023
Messages
27
Programming Experience
10+
Hi again, C# gurus. Total beginner ellpee is back with a brainteaser for you. I'm still in C# kindergarten, experimenting in Visual Studio with some rudimentary console projects, and have noticed something really odd.
To set the stage, imagine I'm starting from scratch and trying to write a simple solution. First I set up a namespace with its curly brackets, and inside that I declare a class with its curly brackets, and inside that I write some code, some of it with curly brackets. That brings me to here:
C#:
namespace myNS
{
    class myClass
    {
        //maybe some simple code here, e.g., variables
        //then let's throw in a simple constructor
        public void myClass(int myint, double mydouble, etc.)
        {
            //some more simple code here for constructor purposes
        }  //closing curly bracket for constructor object
    }  //closing curly bracket for class
}  //closing curly bracket for namespace
NOW THEN, to the quirky part. Sometimes, when I write code to fit between the various sets of curly brackets, the code editor goes rogue and deletes the closing curly bracket for the class section, and that results in a bunch of squiggly underlines and other error flags in my code. By trial and error, using the undo key and/or commenting out lines of code, eventually I get back to a point where the missing class bracket magically reappears and I can try something else to get me to my goal.

So what I'm wondering is not so much what code statements are to blame, that seems to vary from one time to the next. But I'm just curious why does the code editor take it upon itself to remove that important curly bracket. Seems to me all the squigglys and screwdrivers and lightbulbs ought to suffice to tell me I got something wrong and need to fix it; I don't get the point of the "curly bracket cure." Can anyone enlighten me?
 
Last edited by a moderator:
Solution
Can you provide a specific set of steps that we can follow to reproduce the behaviour you describe?

Too complicated, sorry, just ignore this. I was hoping someone would say "Oh yeah, when the code editor goes rogue like that it's because you made this simple mistake." Doesn't bother me enough to post a whole bunch of code. Thanks for your reply though. Will mark this one solved if I can remember how.
Can you provide a specific set of steps that we can follow to reproduce the behaviour you describe?

Too complicated, sorry, just ignore this. I was hoping someone would say "Oh yeah, when the code editor goes rogue like that it's because you made this simple mistake." Doesn't bother me enough to post a whole bunch of code. Thanks for your reply though. Will mark this one solved if I can remember how.
 
Solution
I didn't realise I was asking for a whole bunch of code. I thought I was asking for steps to perform to reproduce the stated behaviour. If that involves code, I would expect it to be very little, much as you posted originally, but building it up step by step. We can still help but, unless you can tell us how to see what you are seeing, we're just taking a stab in the dark. That is especially bad if the issue is specific to your system because, if we don't see the stated behaviour, we can never know whether we did what you did or not. Anyway, it's your call. If you want help then we can provide it but if it's not worth the effort then that's fine too.
 
If you toggle the state of the "Automatic brace completion" in Settings (use the search function) does the "problem" go away or change?
 
Hello ellpee
I quickly saw an error in your code, I don't know if it is the cause of your problem but I hope at least it helps you in your learning.
When you declare a constructor (the method which instantiate your class, that is to create an object) it does not take a returning type, neither void, but yes, it takes the name of your class, all of them in case you have many overloads. You just write something like:

public MyClass() { ... }

public MyClass(int myInt, double myDouble) { ... }

public MyClass(string myString) { ... }

Regards
Pablo
 
@Pablo but does that make VS delete closing curly braces?

Actually I don't know, but what I know is sometimes you can see an error marked on a certain line and the mistake is in some surrounding one. Sometimes, also, you fix an error and three of them disappear. I see you are an experienced programmer, so it's not needed I tell you those occurrences of VS. In this precise case I don't know the why of the missing brace, but I would prove fixing what I said. Maybe the chances of that fixing the issue of the brace are too few, but, anyway, the error of the constructor's returning type will need to be fixed, wether the earlier or the later. Besides, you lose nothing by trying.
Regards
 
Back
Top Bottom