Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
22 Eargesplitten
Oct 10, 2010



Is there a commonly used style guide for C#? I'm coming from Java, which is different, and there is no var. Using var when the type will be unclear to whoever reads through it seems wrong, but I'm open to being corrected.

Adbot
ADBOT LOVES YOU

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

22 Eargesplitten posted:

Is there a commonly used style guide for C#? I'm coming from Java, which is different, and there is no var. Using var when the type will be unclear to whoever reads through it seems wrong, but I'm open to being corrected.

Here's the style guide from the Microsoft .NET Core repository: https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md

EDIT: Also the more verbose Framework Design Guidelines on MSDN: https://msdn.microsoft.com/en-us/library/ms229042.aspx

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Bognar posted:

Here's the style guide from the Microsoft .NET Core repository: https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md

EDIT: Also the more verbose Framework Design Guidelines on MSDN: https://msdn.microsoft.com/en-us/library/ms229042.aspx

Or from MSDN: https://msdn.microsoft.com/en-us/library/ff926074.aspx

https://msdn.microsoft.com/en-us/library/ms229002(v=vs.110).aspx

I just var pretty much everywhere. You can find out the type in a few seconds in the IDE, and it can make refactoring easier. Change an IEnumerable to a List in a method's return type? No need to change anything else.

Calidus
Oct 31, 2011

Stand back I'm going to try science!

Calidus posted:

I have a Solution that contains a MVC 5 project, a business logic project and DAL project. When I try use the MVC scaffolding with my entity frame work objects in my DAL project I keep getting “Object reference not set to an instance of an object.” The MVC project has a reference to my DAL project. I tried creating a test db context inside my MVC project and I got the same error. This is driving me up a wall. I am using VS2013.

Encase anyone else runs into this terrible poo poo. I had to uninstall EF from all of my projects and after reinstalling it I error about missing meta data and being unable to find the CLR for a class. Turns out that classes mapping was messed up in my EDMX model. The really confusing part is that model wasn't even the one being used to created a controller and seemed to work when using it for LINQ and Lamda functions.

chippy
Aug 16, 2006

OK I DON'T GET IT

Ithaqua posted:



I just var pretty much everywhere. You can find out the type in a few seconds in the IDE, and it can make refactoring easier. Change an IEnumerable to a List in a method's return type? No need to change anything else.

I'm not a fan, I think it really impairs readability if it's not immediately obvious what you're assigning to your var (ie in cases like var = New Foo()), or in cases where you're not sure exactly what flavour of collection you're getting back but it doesn't matter too much, like the result of a LINQ query which you're going to further query.

Imo, in the refactoring example you gave, if you didn't explicitly need any of the methods provided by IList/List, you should have been assigning the result of the method to an IEnumerable anyway, not a List. In which case it's just as easy to refactor it. And if you did use a method of List, then changing the return type is going to break your code anyway, so using var hasn't gained you much.

E: Microsoft seems to agree - https://msdn.microsoft.com/en-gb/library/ff926074.aspx - see 'implicitly typed local variables'.

chippy fucked around with this message at 00:54 on Mar 4, 2016

Ochowie
Nov 9, 2007

xgalaxy posted:

Jet Brains C# IDE is out for early access (aka beta). I'm excited to check it out. While running Visual Studio on OS X via parallels is an okay experience it would be nice to have something that offers a bit of a smoother ride performance wise. I hate MonoDevelop. I've used IntelliJ before and loved it for Java programming.

I'm curious how this will play out in the long run. They've built it as a wrapper around calls to Resharper which seems a bit different from their normal IDE's.

B-Nasty
May 25, 2005

I use var as much as I can. It promotes, or should at least, better variable names. After all, I care about what this thing should be used for (intent), not really what it is (type). For example:

code:
Dictionary<string, decimal> g = GetGrades();
var gradePointAvgByName = GetGrades();
The latter is shorter and far more obvious as to what's going on when I say:

code:
if(gradePointAvgByName["Bob"] == 4.0)
Type is always discover able later, intent/purpose is not. Of course if you're doing :

code:
var g = GetGrades()
You can go straight to hell.

EssOEss
Oct 23, 2006
128-bit approved
Tabs are so much better than spaces. Discuss.

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer
I'm conflicted about var because I generally prefer explicit over implicit, but for some reason I'm ok with using var whenever possible, even though it admittedly makes things harder to read sometimes.

A lot of the time you can avoid the whole problem by not having all that many variables to begin with. If I have a variable that's only used on the line directly below its assignment I often fold it into the next function call (unless it makes things totally unreadable). Maybe most people consider that even worse though.

epswing
Nov 4, 2003

Soiled Meat
I tend to explicitly define my types when I'm working on "important" code. Within the inner inner loop of an important function, I want to be careful that I, and everyone else, know exactly which types are in play. If it's a ToString function meant to dump Trace data, var's fine. Everything in the middle, use your judgement.

edmund745
Jun 5, 2010
I'm trying to use a treeview control in VB. Are these things retarded or what?

Consider the following:
1. I have about 30 items to add in, in 3 levels. I can add them in to where they show up properly.
2. Some of these items have the same display text; in particular, some of the root items have the same text as the second-level items contained in them. Sometimes there may be multiple items with the same text.
3. I need to find exactly which item a user clicks on. How can I do that?

I can get the text on the selected node using treeView.SelectedNode.ToString() or TreeView.SelectedNode.Text, but that isn't much help since the text on some them is the same.***

If I try to use treeView.SelectedNode.Index, that works but it only gives me the root index. It says nothing about the upper levels.
For example,,,,,, I have four items at the root level. If I set up a messagebox to show me the selectednode.index in the treeview's afterSelect event, all I can get it to show is a number of zero to 3--which corresponds to the ROOT of the item I am clicking on.

I find LOTS of other people online asking this same question, and all the replies just say use either way above, that is not useful. Is there another way to do this with a treeview or not?
It seems like nearly-worthless as it is (if you can only really get the selected index of the root items???); I am amazed that they would make this work only halfway. It draws a pretty picture, but,,,, it doesn't appear to function usefully.
What I was imagining was that each node would have a index number into ONE zero-base index for ALL the nodes, that you could ask the control to give you--but I find no indication of that.

***I had thought of trying to use a short string of unique but non-printing characters in the node text after the real text; this would be a way to tell the nodes apart by text even when the apparent text was the same. Does it ignore non-printing characters?

Calidus
Oct 31, 2011

Stand back I'm going to try science!
fsharpConf 2016

https://channel9.msdn.com/

chippy
Aug 16, 2006

OK I DON'T GET IT

edmund745 posted:

I'm trying to use a treeview control in VB. Are these things retarded or what?

Yes.

quote:

***I had thought of trying to use a short string of unique but non-printing characters in the node text after the real text; this would be a way to tell the nodes apart by text even when the apparent text was the same. Does it ignore non-printing characters?

If I recall correctly, they have a 'Tag' property (typed to Object) that you could use for this.

e: Yep. https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag(v=vs.110).aspx

chippy fucked around with this message at 19:28 on Mar 4, 2016

Calidus
Oct 31, 2011

Stand back I'm going to try science!
Entity Framework Question: What is the advantage of using EF Designer from Database apposed to Code First From Database?

edmund745
Jun 5, 2010

chippy posted:

.... If I recall correctly, they have a 'Tag' property (typed to Object) that you could use for this.

e: Yep. https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag(v=vs.110).aspx
Thanks so much, that's what I needed.
As you (I) put nodes into the treeview, I use a counter and store that in the tag property, as well as in the things that are being added.

I don't know why they didn't just have a separate node# property that was a zero-base index for all the nodes, in the order that they were added. You may not always want that, but most of the time it would seem the thing to do...

Inverness
Feb 4, 2009

Fully configurable personal assistant.
For the record I prefer these as they perfectly matched with my own preferences.

Edit:

22 Eargesplitten posted:

Is there a commonly used style guide for C#? I'm coming from Java, which is different, and there is no var. Using var when the type will be unclear to whoever reads through it seems wrong, but I'm open to being corrected.
My preference for using var is only when the type is elsewhere in the same statement:
C# code:
// GOOD
var x = new Cake();

// GOOD
var x = GetValue<Cake>();

// BAD
var x = GetValue();

// BAD
var x = GetCake();

Inverness fucked around with this message at 20:42 on Mar 4, 2016

chippy
Aug 16, 2006

OK I DON'T GET IT
They're pretty old but I mostly agree with what these pieces have to say on the use of var:

https://blogs.msdn.microsoft.com/ericlippert/2011/04/20/uses-and-misuses-of-implicit-typing/
http://www.brad-smith.info/blog/archives/336

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
This is basically the tabs v. spaces argument of the C# world, everyone's gonna have their own preference and you likely won't be able to change anyone's opinion. That said, I might as well add to it.

I use var for literally everything, anywhere and everywhere. I have never once run into a situation where I thought "I don't know the type of this variable, man I wish they had written the type in the declaration." Instead, it's always "I don't know the type of this variable, guess I'll hover over it and wait half a second for the type to be displayed." If I did less work in an IDE and more reading code through an external tool like Github then maybe it would be different.

To me, descriptive variable names are significantly more important than the type declaration. That will help your understanding everywhere that variable is used rather than just at the point of declaration. When you're working on that 500 line eldritch horror function (every codebase has one), you don't want to be jumping back and forth to look at declarations to figure out the types because the variable names don't give enough information.

xgalaxy
Jan 27, 2004
i write code
I guess since we are on the style talk right now...

What does everyone have their right margin set to? And how do you prefer to wrap long lines?
Do you prefer hard wrapping, soft wrapping, chopping? Or does it depend on if its a method, method call, etc?

I kind of go back and forth on chopping vs hard wrapping and usually go with what looks best at that instance instead of a hard rule on this.
I'm curious what other people do.

I was in a code base at one point where they always chopped, regardless if it was a long line or not. So every method definition and every method call had its arguments chopped.

xgalaxy fucked around with this message at 22:20 on Mar 4, 2016

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
I hate soft wrapping - I think it just makes everything more confusing. I tend to hard wrap things if necessary but my tolerance for line length is higher than 80 characters, probably closer to 120. I keep VS open on an entire widescreen monitor and so do most of my coworkers, so I don't see a huge need to keep to a limit that was defined in low resolution 4x3 aspect ratio days - especially with how verbose idiomatic C# can be. ReSharper makes hard wrapping less of an annoyance since it tends to do it for you when place a semicolon - of course that in itself can be an annoyance depending on what you're doing.

I have no idea what you mean by chopping.

Bognar fucked around with this message at 22:39 on Mar 4, 2016

xgalaxy
Jan 27, 2004
i write code

Bognar posted:

I have no idea what you mean by chopping.

This is chopping:
code:
public void HereIsAMethod(string firstParam,
    string secondParam,
    string thirdParam)
{
    HereIsAnotherMethod(firstParam,
        secondParam,
        thirdParam);
}

// or some prefer:
public void HereIsAMethod(
    string firstParam,
    string secondParam,
    string thirdParam)
{
    HereIsAnotherMethod(
        firstParam,
        secondParam,
        thirdParam);
}

As an example. Alignment of the chopped lines is another aspect and point of debate.

xgalaxy fucked around with this message at 22:57 on Mar 4, 2016

sarehu
Apr 20, 2007

(call/cc call/cc)

xgalaxy posted:

I guess since we are on the style talk right now...

What does everyone have their right margin set to? And how do you prefer to wrap long lines?
Do you prefer hard wrapping, soft wrapping, chopping? Or does it depend on if its a method, method call, etc?

I decided to try soft-wrapping recently and I like it. No more worrying about re-indenting and such, and argument lists are a pleasant unreadable jumble.

But seriously they're readable, you just read them, I think that's one place where being all prim isn't really that useful, aside from promulgating an air of professionalism in the "broken windows" sense.

Edit: And by soft-wrapping that applies to comments, too, and they're wrapped at the Nth character with no "word" wrap.

Edit: Of course, you could imagine it interfering with the view of indentation, and it's probably bad in that situation, in which case take it easy and hard-wrap some lines.

sarehu fucked around with this message at 23:11 on Mar 4, 2016

chippy
Aug 16, 2006

OK I DON'T GET IT

Bognar posted:

To me, descriptive variable names are significantly more important than the type declaration. That will help your understanding everywhere that variable is used rather than just at the point of declaration. When you're working on that 500 line eldritch horror function (every codebase has one), you don't want to be jumping back and forth to look at declarations to figure out the types because the variable names don't give enough information.

I totally agree, but I'm not sure why it keeps coming up in relation to the var debate. I try and make my variable names nicely descriptive whether or not I'm using explicit typing.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

chippy posted:

I totally agree, but I'm not sure why it keeps coming up in relation to the var debate. I try and make my variable names nicely descriptive whether or not I'm using explicit typing.

Because the most commonly quoted reason that people don't use var (knowing the variable type at the point of declaration) is usually unnecessary if the variable is named properly. There's no reason you can't have both descriptive variable names and explicit typing, but the former mostly obviates the need for the latter.

Calidus
Oct 31, 2011

Stand back I'm going to try science!
I am terrible hack who just writes ugly code than has ReSharper pretty it up for me.

raminasi
Jan 25, 2005

a last drink with no ice

xgalaxy posted:

This is chopping:
code:
public void HereIsAMethod(string firstParam,
    string secondParam,
    string thirdParam)
{
    HereIsAnotherMethod(firstParam,
        secondParam,
        thirdParam);
}

// or some prefer:
public void HereIsAMethod(
    string firstParam,
    string secondParam,
    string thirdParam)
{
    HereIsAnotherMethod(
        firstParam,
        secondParam,
        thirdParam);
}

As an example. Alignment of the chopped lines is another aspect and point of debate.

I do this, but it's only because I hate it the least. I don't like it.

Cuntpunch
Oct 3, 2003

A monkey in a long line of kings

Calidus posted:

Entity Framework Question: What is the advantage of using EF Designer from Database apposed to Code First From Database?

I'm going to presume you are using the strict literal sense of 'EF Designer from Database'(aka the project template name) - where you are functionally doing a Database-first approach - and not a designer-first to generate both code *and* DB.

That being said, it sure is helpful if you already *have* a database set up and just need to hook EF into it?

Calidus
Oct 31, 2011

Stand back I'm going to try science!

Cuntpunch posted:

I'm going to presume you are using the strict literal sense of 'EF Designer from Database'(aka the project template name) - where you are functionally doing a Database-first approach - and not a designer-first to generate both code *and* DB.

That being said, it sure is helpful if you already *have* a database set up and just need to hook EF into it?

Yes if you already have your database and you want to hook EF into to, VS has two templates to do that. I am trying to figure out which one better suits my needs.

amotea
Mar 23, 2008
Grimey Drawer
code:
char* foo;
or
code:
char *foo;
or
code:
char * foo;
?

No Safe Word
Feb 26, 2005

In this thread?

code:
string foo;

Mongolian Queef
May 6, 2004

No unsafe keyword posted:

In this thread?

code:
string foo;

Gul Banana
Nov 28, 2003

code:
var foo = string.Empty;

raminasi
Jan 25, 2005

a last drink with no ice
Maybe they're doing unsafe character pointer operations :colbert:

durtan
Feb 21, 2006
Whoooaaaa
Is this an appropriate place to ask for help regarding Netduino or .NET Micro Framework 4.3? I just got a Netduino 3 Wifi but I'm struggling to find current and accurate documentation online and I'd like to move some servos and steppers.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

durtan posted:

Is this an appropriate place to ask for help regarding Netduino or .NET Micro Framework 4.3? I just got a Netduino 3 Wifi but I'm struggling to find current and accurate documentation online and I'd like to move some servos and steppers.

Either here or the embedded thread: http://forums.somethingawful.com/showthread.php?threadid=3500975

However, to my knowledge there aren't a whole lot of people working with the combination of .NET and embedded stuff so you might have trouble getting answers.

durtan
Feb 21, 2006
Whoooaaaa

Bognar posted:

Either here or the embedded thread: http://forums.somethingawful.com/showthread.php?threadid=3500975

However, to my knowledge there aren't a whole lot of people working with the combination of .NET and embedded stuff so you might have trouble getting answers.

Cool, thanks!

crashdome
Jun 28, 2011

Calidus posted:

Yes if you already have your database and you want to hook EF into to, VS has two templates to do that. I am trying to figure out which one better suits my needs.

I don;t think anyone has answered you so here's my take: I use the Db first w/ EDMX designer and modify T4 templates because I do very little, if any, modifying beyond implementing an interface on the data model classes. I also never touch the EDMX designer except to "update from database". I also keep track of db changes in a seperate database project in my solution for posterity and I'll explain why... Changes to the production databases are usually done manually because, most if not all I deal with of any significant size all are still SQL 2008 or less, locked pretty tight, and spread across multiple instances. I keep track of any changes via the database project in the event it (for some reason) needs to be rebuilt or I need to spin up a new dev db. My application/solution itself never gets authority to make any db changes so, I just pull in changes from a dev or prod SQL server into the EDMX and also the DB project and pray. I'm not on the latest latest EF btw - in case something is new that I haven't seen yet.

brap
Aug 23, 2004

Grimey Drawer
Database versioning is a bitch of a problem.

Visual Studio has Schema Compare, so is there a way to do something like compare your shiny new database project to the production database and come up with a conversion script that brings the production database up to date with all the changes in the project?

My experience is Schema Compare is mainly usable as a litmus test for whether your handwritten conversion script is good or not...it's very painful, especially because DBAs make changes that don't get back to any database project. Just comparing the new DB project with the DB project for your last production release does you no good.

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

fleshweasel posted:

Database versioning is a bitch of a problem.

Visual Studio has Schema Compare, so is there a way to do something like compare your shiny new database project to the production database and come up with a conversion script that brings the production database up to date with all the changes in the project?

You can use sqlpackage.exe and/or the DAC Framework libraries to run your project's .DACPAC file against your production DB and generate a migration script and/or a pretty XML summary of the changes that will be applied, before you let it actually take care of the migration.

You need to be dealing with SQL 2008R2 or higher (though in theory SQL2005+ can work with the right service packs). Windows XP is unsupported, but you can make it work as long as you install the right registry keys (I had to find a way to make it work because we had a few hundred XP machines still out there :v:).

All in all it's not the most awesome thing ever but IMO it's a massive upgrade over any kind of manual migration tool. If you still need to do any hand-massaging you can include both pre- and post-deployment scripts as well.

Adbot
ADBOT LOVES YOU

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug
Huh, Microsoft announced SQL Server for Linux today. Should be interesting to see how that plays out. I'm guessing they're targeting the people who want something better than MySQL but less expensive than Oracle.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply