A more complete auto-format in Visual Studio - visual-studio-2013

Of course you might immediately think "This question has been asked before!".
But it hasn't been answered.
All answers seem to suggest Ctrl+K,D or some variant will help me. I have looked quite a bit. Ctrl-K,D does lots of things for me and I use it all the time. But it falls short on the examples below.
I am currently using Visual Studio 2013.
Take this class:
public class RubbishAutoFormat
{
public List<string> IsntThisRubbish { get; set; }
public RubbishAutoFormat()
{
IsntThisRubbish = new List<string>
{
"Yes", "It", "Is"
};
}
}
I can hit Ctrl+K,D as many times as I like, but bugger all happens.
If I delete and re-enter the ; at the end of the list initialiser, I get this loveliness:
public class RubbishAutoFormat
{
public List<string> IsntThisRubbish { get; set; }
public RubbishAutoFormat()
{
IsntThisRubbish = new List<string>
{
"Yes",
"It",
"Is"
};
}
}
If I have a big class with a lot of badly formatted code of this ilk, my only option at the moment is to categorically traipse through all of the relevant semi-colons and delete and retype them to trigger the correct "smart" auto-format in Visual Studio.
How can I make this happen for a whole document at a time? "Developer logic" in me stipulates: "It's already done for parts of the code, how do I trigger the loop for all of the code?" :)
Someone please! It's driving me crazy :)

Related

How to ask Yes/No questions using FormFlow?

I just can't figure out how to ask a yes/no question while filling a form with FormFlow. You can't use the Confirm method to fill a bool,
var builtForm = new FormBuilder<BuyingACar>()
.Field(nameof(NewCar))
.Field(nameof(Model))
.Field(nameof(Brand))
.Build();
I need the field "NewCar" to be a prompt with buttons "yes" and "no". I don't want to use an enum since it will send a 0/1 when a button's pressed.
edit: My final goal is to fill a bool field.
It should work perfecly with booleans. It accepts "Yes", "No", "y" and "n" as possible answers. If you use the special Pattern Language {||} you can edit the prompt to include the options.
Here is an example.
public enum Brands
{
Volvo, BMW
}
[Serializable]
public class BuyingACar
{
[Prompt("Would you like to buy a car? {||}")]
public bool NewCar { get; set; }
public Brands? Brand { get; set; }
public static IForm<BuyingACar> BuildForm()
{
return new FormBuilder<BuyingACar>()
.Build();
}
}
And the result:

Realm error: The lhs of the binary operator 'GreaterThan' should be a member expression.

I tried to get all persons who have dogs older than 10 using the following line of code :
var persons = realm.All<Person>()
.Where(person => person.Dogs.Count(dog=>dog.Age > 10) > 0);
But I got this error:
System.NotSupportedException:
The lhs of the binary operator 'GreaterThan' should be a member expression.
Unable to process `person.Dogs.Count(dog => (dog.Age > 10))`
Does it mean that we are not able to filter on relations in Realm ?
Here are the models that I copied from Realm documentation:
public class Dog : RealmObject
{
public string Name { get; set; }
public int Age { get; set; }
public Person Owner { get; set; }
}
public class Person : RealmObject
{
public string Name { get; set; }
public IList<Dog> Dogs { get; }
}
Yeah sorry, this type of query is not yet supported. We are working on improving LINQ coverage, but I'm afraid you won't be able to perform this particular query in the near future.
Two workarounds that may or may not be applicable in your situation:
1) Add a boolean HasOldDogs property on your Person class. Note that in order to perform queries on it, it would have to be a persisted property so you would have to update it explicitly whenever you modify the Dogs property or any of the contained Dog instances.
2) Perform the query in memory. var persons = realm.All<Person>().ToList().Where( ... ) will work. However, this will perform the query with LINQ to Objects in memory which works best if you don't have a massive number of Person instances.
If you can describe in more detail what your situation requires, we might be able to think up alternatives.
may be you have to break your query to two queries instead of one.
Seems there are some issues with Realm that might require someone from Realm team to explain :)

ReSharper changes intellisense behaviour when initilizing objects

Let's assume I have a class
public class Dummy
{
public int DummyInt { get; set; }
public string DummyString { get; set; }
}
and somewhere in the code I have:
var dummy = new Dummy
{
In VS10, if I press now 'a' I will get a list of members: DummyInt,DummyString. But with ReSharper switched on nothing will be shown if I press 'a'. I must press 'd' to get both fields listed.
I went through ReSharper options, but I can't find it anywhere. Is it possible to switch that off?
R# 7.1
Resharper offers a couple different symbol completions. The basic one is called Symbol Completion and has a default key binding of Ctrl-Space. The more intelligent one is called Smart Completion and has a default key binding of Ctrl-Shift-Space.

String.Format() with HTML attributes in a View

I have a model with some inheritance worked into it, but here's the thing, I want to apply different HTML attributes depending on which type it is. So my thought was to create a property, I'll call it DisplayString, which is used as the first argument to String.Format. DisplayString is used to allow me to do something like this:
Classes:
public class Reaction
{
public string ReactionString { get; set; }
public string DisplayString { get; set; }
public Reaction(string reactionString)
{
ReactionString = reactionString;
DisplayString = "It was {0}!"
}
public Reaction(string reactionString, string displayString)
{
ReactionString = reactionString;
DisplayString = displayString;
}
}
public class GoodReaction : Reaction
{
public GoodReaction()
{
base("awesome");
}
}
public class BadReaction : Reaction
{
public GoodReaction()
{
base("horrible");
}
}
public class AverageReaction : Reaction
{
public GoodReaction()
{
base("alright", "It kinda was {0}...");
}
}
View:
#model Reaction
#String.Format(Model.DisplayString, "<strong>" + Model.ReactionString + "</strong>");
So basically every subclass has a string I want to display, but I have varying text AND varying markup. Of course doing it this way just results with the strong tags rendered as text. So what are my options? Feel free to ask any questions needed to clarify, hopefully I got my question across properly. Thanks!
EDIT:
So I wrapped it in Html.Raw() and that did the trick, although I'm sure there is something bad about doing that and I should probably encode each piece, then add tags, then raw, or something, but for my purposes it works.
I would create a display template for each concrete class, and MVC will be smart enough to pick the right one.
E.g when you call #Html.DisplayFor(model => model.Reaction), where Reaction is declared as the base class, MVC will work out what type it is, then look for a template matching that type, if it doesn't find it, it will look for a template of the base type, then finally fallback to the default template. I do this a lot in my application.
Sounds like you should also make the Reaction class abstract, and the ctor's protected.

Linq and SubSonic - returning nested complex types

I'm new to SubSonic and reasonably new to LINQ as well, so I'm just trying to put a little app together.
I've got the templates all sorted and running okay, but I've run into a bit of trouble with this LINQ statement (simplified slightly, the real statement has some other joins but they don't affect this particular problem so I've removed them for brevity):
var addresses = from address in Database.Addresses.All()
select new Address()
{
MyNestedType = new NestedType()
{
Field1 = address.ADDR1
}
};
If I execute this statement I get the error Invalid cast from 'System.String' to 'NestedType'. when I try to enumerate the results.
I'm probably overlooking the obvious but I can't see anywhere that I request such a conversion.
Both Field1 and address.ADDR1 are strings.
Any ideas what I'm doing wrong?
Edit:
I've had another look at this and in an effort to provide more information, I've created a small, complete example using SimpleRepository and an SQLite database that demonstrates the issue. Using SimpleRepository the error I get is different (Sequence contains no elements) but the result is the same. Here's the complete code:
public class DatabaseAddress
{
public int Id { get; set; }
public string Address1 { get; set; }
}
public class Address
{
public NestedType MyNestedType;
}
public class NestedType
{
public string Field1 { get; set; }
}
static class Program
{
[STAThread]
static void Main()
{
var repo = new SimpleRepository("Db", SimpleRepositoryOptions.RunMigrations);
DatabaseAddress address1 = new DatabaseAddress();
address1.Address1 = "Test";
repo.Add(address1);
var all = repo.All<DatabaseAddress>();
var addresses = from address in repo.All<DatabaseAddress>()
select new Address { MyNestedType = new NestedType { Field1 = address.Address1 } };
}
}
In this example, all contains the object added to the database, but addresses returns "Sequence contains no elements".
If I use anonymous types instead of concrete types in the select statement it works.
There's obviously a gap in my knowledge here; any help appreciated.
Please see my question and answer here.
Here's how you can test if it is the same issue:
In that sample code you posted, change Field1 in your NestedType to be named Address1. Re-run your sample. If it works, same issue and the fix I answered with in the linked question should solve it for you.
You have to call ToList(), otherwise the SubSonic provider tries to do something with MyNestedType and it doesn't exist in the database.
var addresses = from address in repo.All<DatabaseAddress>().ToList()
select new Address { MyNestedType = new NestedType { Field1 = address.Address1 } };
Update: It also works if you call ToList afterwards, i.e.:
addresses.ToList().ForEach(address => Console.WriteLine("Address.MyNestedType.Field1 = {0}", address.MyNestedType.Field1));
I guess there is a bug in the SubSonic query provider, because it does work for anonymous types, as you mentioned.
Try this
var nestedTypes= from address in Database.Addresses.All()
select new NestedType()
{
Field1 = address.ADDR1
};

Resources