Object initializers - VS intellisense turns "new" into "new object" - visual-studio

Let's say I have a function that takes an object as a parameter and returns a string...
public string Foo(object values) { // return a string }
When working in Visual Studio 2008 (ASP.NET MVC Web Application), I try to pass an object intializer to this function like so:
string x = Foo(new { x = 1, y = 2 });
While I'm typing this out, the intellisense takes over after I type "new " (without quotes) and I end up with "new object " before I can type the opening curly brace.
I imagine the intellisense is trying to help out by assuming I'm going to create something that matches the function declaration, but in this specific case it's annoying. Is there a way I can turn this off for object?

I don't know which of the settings that does this, but I always turn off "Auto list members" and "Parameter information" in the "Text Editor" settings in the options, and it doesn't do that for me.
To get the behaviour that you mention I now have to press Ctrl+Space.

Related

How to generate smarter/complex snippets in Visual Studio/Visual Studio Code?

Problem: I'm looking for a way to create complex snippets. At our company we have larger functions which almost seem boilerplate-ish, and I feel can be made much easier.
Desired solution: I want to create something, similar to how snippets work, but suitable for more complex generation of code. For instance, see the following code, which is typical for what we generate:
private readonly DependencyOne dependencyOne;
private readonly DependencyTwo dependencyTwo;
public ClassName(DependencyOne dependencyOne, DependencyTwo dependencyTwo)
{
this.dependencyOne = dependencyOne;
this.dependencyTwo = dependencyTwo;
}
Basically I only want type the two classnames, and from that generate the constructor and the two associated fields. If possible I want to add these fields at the correct position in the code, pretty much like how IntelliSense's Quick Fix automatically finds the correct position in your code to place the fields.
The reason why I can't just generate it above the constructor, is because there are some methods which will be generated which aren't constructors and therefore don't reside on the top of the code.
How do I achieve this desired solution?
Solution with Visual Studio Code 1.24:
In visual studio code you can specify the snippets as you want by creating a snippet JSON file. Please refer to this doc to know how to create a new snippet in VS Code.
write the following in language.json, language would be whatever language for which you want to create the snippet :
"Constructor - A unique name" : {
"prefix" : "constructor",
"body": [
"private readonly ${DependencyOne} ${dependencyOne};",
"private readonly ${DependencyTwo} ${dependencyTwo};",
"",
"public ClassName(${DependencyOne} ${dependencyOne}, ${DependencyTwo} ${dependencyTwo})",
"{",
" this.${dependencyOne} = ${dependencyOne};",
" this.${dependencyTwo} = ${dependencyTwo};",
"}",
],
"description": "description of what it does"
}
after following the steps in doc and writing the json, you would be able to use the snippet by typing constructor as mentioned as "prefix" of the snippet.
And with release v1.25 the following works:
"Constructor and variables" : {
"prefix" : "ctor",
"body": [
"private readonly ${1/(.*)/${1:/capitalize}/} ${1:var1};",
"private readonly ${2/(.*)/${1:/capitalize}/} ${2:var2};",
"",
"public ClassName(${1/(.*)/${1:/capitalize}/} $1, ${2/(.*)/${1:/capitalize}/} $2)",
"{",
" this.$1 = $1;",
" this.$2 = $2;",
"}",
],
"description": "your description"
},
For this you will only type two names - I have made it so you type the uncapitalized version and the snippet will automatically capitalize the classnames. It would be easy to reverse those but would be a lot more code. After you enter the second classname/var hit tab and your code will capitalize correctly.You can replace the "var1/var2" with whatever you want.

Why does one of my fields have "#" in front of its name in debug?

I have a class with a local variable called "group", as you can see I give it a value in the constructor (the value I assign it is null if it matters):
The strange thing is, when I debug my project, the name "group" suddenly becomes "#group":
I don't have this for any other variables, is this something of Visual Studio 2015? Never saw this before in earlier versions. If it's not a bug, what does it mean?
Edit: "#group" and "group" can both be used in code:
The word "group" is a keyword, as used in LINQ:
var x = from y in collection
group y by p.name into g ....
In C# keywords can be uses as names, if they are prefixed by # and used like this:
var #class = "car";

"Type mismatch in expression." Visual Studio 2013 with Access 2013 DB

I'm trying to do a simple "Advanced Search Button". This project is using 3 tables within 1 database. The page loads up but when I try to hit the "search button" after entering values into the textbox of website, it gives me "Type mismatch in expression."
Here is the code for search button;
SqlDataSource1.SelectCommand = "SELECT m.musterino, m.adi, m.soyadi, m.adres, u.urunno, u.parca, u.marka, u.modelisim, u.modelno, u.fiyat, s.satisno, s.odeme, s.urun, s.garanti, s.tarih FROM musteri m, satis s, urunler u WHERE m.musterino = s.musterino AND u.urunno = s.urunno AND s.satisno = #ara"
SqlDataSource1.SelectParameters.Add("ara", TextBox1.Text)
SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty)
If the field s.satisno is of numeric type then this line is ambigous.
SqlDataSource1.SelectParameters.Add("ara", TextBox1.Text)
You add a parameter without specifying its type. So you leave open any kind of interpretation on it and because you add a string it is possible that something doesn't go as you expect here.
I would try to be more specific on the type of the value passed for the parameter
SqlDataSource1.SelectParameters.Add("ara", DbType.Int32, TextBox1.Text)

String value with double quote in C#

I was trying to do autocomplete for my input box. When user start typing "I, then I should exactly search the keyword what user has typed ("I). When keys pressed, I was getting the string value as "\"I. How can i do the search based on what user has entered without stripping off any character from the string. Pls provide me any suggestion to help my issue.
Sample Code
public JsonResult AutoBibs(string searchTerm)
{
model = (from line in db.BibContents
where (line.Value.StartsWith(searchTerm) || line.Value.Contains(" " + searchTerm))
select new PoDetails
{
BibId = line.BibId
}).ToList();
return model;
}
The " always appends with an Escape character while processing the String variables in C# i.e. it appends "\" at the beginning. It would not change your functionality and you can still continue with your Auto Complete feature. Generally you can find this during in DEBUG mode only.
Read this MSDN article for more details.

How to avoid visual studio warning when using C# variable in the javascript section of a razor view

In a razor view, I have a script snippet using the model property values:
<script>
var #(Model.DialogModel.DialogVar) = new AjaxDialog("#(Model.DialogModel.DialogHtmlId)");
</script>
Both DialogVar and DialogHtmlId are of type String and the script works fine, but visual studio shows there is an "Expected Identifier" error around the "=" sign.
How to suppress the error prompt without turning the "Show syntax errors" off globally?
Put the entire line into a razor expression like:
var dlg = new AjaxDialog("#(Model.DialogModel.DialogHtmlId)");
#("var " + Model.DialogModel.DialogVar + " = dlg;")

Resources