Results 1 to 5 of 5

Thread: There Must be a Better Way

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    There Must be a Better Way

    I haven't worked much with C#. The language is familiar enough, but using the language in VS 2019 is not. Therefore, I'm way behind the curve on this and figure that I HAVE to be missing a bunch of tips for working with the IDE, because this is painful.

    1) I declared a class which had to implement an interface. That's easy enough, put in the class name : interface name. It doesn't automatically implement the interface?!? I have to tell it to do so? Seriously? Under what circumstances would I implement an interface and NOT want the class to implement the interface? Why does it need to be told? What am I missing there?


    3) I remember somebody (Techgnome, I think it was) mentioning a quick way to get close brackets, parenthesis, etc, but I don't remember what it was. The formatting that I end up with is not to my liking. I prefer a style like this:

    Code:
                     public bool Dividable
    		{
    			get
    			{
    				return false;
    			}
    
    		}
    Which I can do, but not easily. The IDE prefers to put the } on the same line as the {, and is happy with the {} on the same line as the method name, so there are a couple extra keystrokes to change it. I'm not so concerned with not having the brackets on the same line, but it would certainly not be my preference to do it that way.

    Is there a simple stylistic way to get my preferred layout?

    If anybody is wondering, there WAS a question #2, and it was FAR more involved and significant than the trivial questions that remain. It was also a style choice, and after taking a deep dive into the options for the IDE, I found one that changed the behavior to the way I want it. That's one of the great things about the IDE=>If your prefer it to be one way, you can generally get it to behave that way. Thus I leave these two questions, and have removed the one that was more complex and had the simpler answer.
    My usual boring signature: Nothing

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: There Must be a Better Way

    Quote Originally Posted by Shaggy Hiker View Post
    I haven't worked much with C#. The language is familiar enough, but using the language in VS 2019 is not. Therefore, I'm way behind the curve on this and figure that I HAVE to be missing a bunch of tips for working with the IDE, because this is painful.

    1) I declared a class which had to implement an interface. That's easy enough, put in the class name : interface name. It doesn't automatically implement the interface?!? I have to tell it to do so? Seriously? Under what circumstances would I implement an interface and NOT want the class to implement the interface? Why does it need to be told? What am I missing there?


    3) I remember somebody (Techgnome, I think it was) mentioning a quick way to get close brackets, parenthesis, etc, but I don't remember what it was. The formatting that I end up with is not to my liking. I prefer a style like this:

    Code:
                     public bool Dividable
    		{
    			get
    			{
    				return false;
    			}
    
    		}
    Which I can do, but not easily. The IDE prefers to put the } on the same line as the {, and is happy with the {} on the same line as the method name, so there are a couple extra keystrokes to change it. I'm not so concerned with not having the brackets on the same line, but it would certainly not be my preference to do it that way.

    Is there a simple stylistic way to get my preferred layout?

    If anybody is wondering, there WAS a question #2, and it was FAR more involved and significant than the trivial questions that remain. It was also a style choice, and after taking a deep dive into the options for the IDE, I found one that changed the behavior to the way I want it. That's one of the great things about the IDE=>If your prefer it to be one way, you can generally get it to behave that way. Thus I leave these two questions, and have removed the one that was more complex and had the simpler answer.
    One reason for case 1 is that in C# you can implement an interface explicitly or implicitly and you need to tell it which way, I just get into the habit of hitting Ctrl + . once I have typed the class declaration and specified the interface name and then choose how I want the interface to be implemented.

    Not sure about number 3 but perhaps an .editorconfig file might work... https://docs.microsoft.com/en-us/vis...s?view=vs-2019 and https://kent-boogaart.com/blog/edito...r-c-developers might have something useful.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: There Must be a Better Way

    You should be able to adjust your formatting under Tools > Options > Text Editor > C# > Code Style > Formatting.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: There Must be a Better Way

    It's worth noting that if you're setting up a getter function in that exact manner, it can be reduced to:
    Code:
    public bool Dividable() => false;
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: There Must be a Better Way

    Yeah, it will reformat like that, but I prefer the old style.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width