C# run project while ignoring one class - c#

I want to run my project, and I have one class that makes errors - I will fix it later ,but now I want to run the project without reference to the class that makes errors.
How can I do it?

You can do right click on that file and select exclude from project for now.
It is something like Image bleow.
Other way is to comment the logic that is not desired and continue working on without excluding.

You need to comment your class and all usages of this class. It can be done by selecting code block that you need to comment and pressing Ctrl+K, Ctrl+C.
If you need to uncomment - Ctrl+K, Ctrl+U on selected commented block.
Also you need to note that commenting your class usages in project also might produce new errors.

Comment out its inner code. This way you can still reference that class from your code but it will no longer show errors unless you are refering to method or property of this class which is commented out.
public ProblemClass
{
// public string Name { get; set; }
// ...
// ...
}

Related

What's the easiest way to create a managed visualiser in C#?

I have a background in C++ and recently I started working in C#.
I have written following pieces of code (in Visual Studio):
var list_Loads = database.GetData<Load>().ToList();
var test_list = list_Loads.Where(o => (o.Name.Substring(0, 3) == "123")).ToList();
When I run the program and I move my mouse over both lists, first I get the count, which is very useful, but when I ask for the entries, this is what I get:
0 : namespace.Load
1 : namespace.Load
2 : namespace.Load
...
Not very useful, as you can imagine :-)
So my question: how can I show the Name attributes of those objects?
I thought: no problem. I have a background in native visualisers, so it should be rather easy to turn this into useful information, but then it comes:
In order to alter the way that those objects are represented, there is the first proposal to add a [DebuggerDisplay] "tag" to the definition of that class in source code.
However, as those classes are part of a framework I'm just referring to, I don't have access to the source code and hence I can't modify this.
Then I found another solution, which comes down to: "Write an entire C# project, debug, test and install it and it might work" (see documentation on "Custom visualisers of data" on the Microsoft website).
I almost choked in my coffee: writing an entire project, just for altering the view of an object??? (While, in C++, you just create a simple .natvis file, mention the classname and some configuration, launch .nvload and that's it.
Does anybody know a simple way to alter the appearance of C# object, without needing to pass through the whole burden of creating an entire C# project?
By the way, when I try to load a natvis file in Visual Studio immediate window, this is what I get:
.nvload "C:\Temp_Folder\test.natvis"
error CS1525: Invalid expression term '.'
What am I doing wrong?
Thanks in advance
OP (my emphasis):
In order to alter the way that those objects are represented, there is the first proposal to add a [DebuggerDisplay] "tag" to the definition of that class in source code.
However, as those classes are part of a framework I'm just referring to, I don't have access to the source code and hence I can't modify this.
Does anybody know a simple way to alter the appearance of C# object, without needing to pass through the whole burden of creating an entire C# project?
If you just want to specify [DebuggerDisplay] on a type, you don't have to have access to the source code. You can make use of [assembly:DebuggerDisplay()] and control how a type appears in the debugger. The only downside is that [assembly:DebuggerDisplay()] naturally only affects the current assembly whose code your mouse is hovering over. If you wish to use the customised display in other assemblies that you own, then you must repeat the [assembly:DebuggerDisplay()] definition.
Here's an easy before-and-after example with DateTime. I picked DateTime because we generally don't have access to the source code and it has some interesting properties:
var items = new List<DateTime>
{
DateTime.Now.AddDays(-2),
DateTime.Now.AddDays(-1),
DateTime.Now
};
...which on my machine defaults to:
Maybe I'm fussy and I just want to see:
Day of the week and
Day of the year
...I can do that via:
using System.Diagnostics;
[assembly: DebuggerDisplay("{DayOfWeek} {DayOfYear}", Target = typeof(DateTime))]
...which results in:
Example:
namespace DebuggerDisplayTests
{
public class DebuggerDisplayTests
{
public DebuggerDisplayTests()
{
var items = new List<DateTime>
{
DateTime.Now.AddDays(-2),
DateTime.Now.AddDays(-1),
DateTime.Now
};
}
}
.
.
.
}
Overrides
[assembly:DebuggerDisplay()] can also be used as a means to override pre-existing [DebuggerDisplay] on a 3-rd party type. Don't like what style they have chosen? Is the type showing far too much information? Change it with [assembly:DebuggerDisplay()].

Class always tries to open in Design View, not Code View

I have a class named Print.cs that always stubbornly opens in Design View, when I need to see its Code View (F7). It also has a different icon to the rest of my classes in the Solution Explorer.
I've looked in the Properties and can't see anything relevant. I've also tried deleting and re-creating the class, but the icon comes back.
How can I force Print.cs to always open in Code View?
(Click to enlarge)
NB: disregard the green squiggly line, it's just a warning that unreachable code was detected.
Taken from the suggestions from #LarsTech and #OrkunBekar, since neither posted this as an answer - this works:
[System.ComponentModel.DesignerCategory("Code")]
Added between the namespace and the class, e.g.
using System;
using System.Collections.Generic;
...
namespace POS
{
[System.ComponentModel.DesignerCategory("Code")]
public class Print : PrintDocument
{
...
}
}
Try right click on the file -> Open With -> CSharp Editor (remember to set it as default).
Funny enough, it was totally other thing in my case.
If the filename equal (=) to the first class in it, then Visual Studio decides it is a simple C# file. If you have 2 classes and the first is not equal to the file name, then the solution icon changes and default editor is designer.
I don't know if you have same conditions in that link but opening your class in notepad, changing codes, replacing file then building the solution again may fix the problem.

How to use Resharper File/Live Templates to Generate Exception Class

I like to make a lot of custom exceptions for most error cases I run into, instead of say using InvalidOperation all the time.
I recently got Resharper and thought it's template functionality would be my savior, but I can't seem to get what I want out of the File/Live template function.
What I'm trying to do is essentially type this:
...
throw new [resharper key combo]MyCustomName[enter]
...
and get an exception class named 'MyCustomNameException' in file 'MyCustomNameException.cs'.
Live templates doesn't seem to be the right thing for me -- that expands code inline.
But file templates doesn't seem to either -- it only asks me to specify the name of the file. Is the file template option as close as I can get to what I want here?
Not sure about adding the 'Exception' part, but if you type the full name and press ALT + ENTER, ReSharper will offer to 'Create Class 'MyCustomNameException''. The code it creates is:
internal class MyCustomNameException : Exception
{
}

Is there a way to instruct visual studio to auto add fields at the bottom of the class instead of the top?

namespace Guilds
{
public class Wizard
{
public void Wear(IClothing clothing)
{
Console.WriteLine("Puts on the {Robe} and {WizardHat}".Fmt(clothing));
}
IClothing _clothes;
IWeapon _weapon; // <== I want my fields added at the bottom of the class!
}
}
I am aware that if you put your fields at the bottom, it will start adding subsequent ones to the bottom of the class as well. I would love to have this as the default behavior even for the first field.
This behavior is usually triggered when pressing Ctrl + . on top of an undeclared field.
Use Regionerate and create your own format template. it's free tool to use with visual studio.
Edit: You can also use CodeMaid because it seems that Regionerate and VS2012 do not work together (I have not tested that combination at all though. I have VS2010)
Edit Adding more to my previous reply. CodeMaid is really cool and you can specify the layout in configuration. Also in configuration, you can specify that file should be formatted on save. This way write your code in anyway you want and have it formatted when you press Ctrl+S! I am one happy user of CodeMaid. Also I am using Visual Studio 2013.

When does a property get called?

I have a confusion on some piece of code.
Inside a class I have a property
Class A
{
ClassB objB;
public int TimedValue
{
objB.Timer;
}
}
Inside classB I have
classB
{
public int Timer
{
get
{
// get time value using some algorithm....
}
}
}
My confusion is that I place breakpoints inside the getters, but I dont see the program flow there and stop! Although I see an object being created and full populated with the TimedValue when I look at it in debug mode inside a watch window. Am I missing something on properties?
EDIT: So, ColinE guided me through the right steps, except I could not find the option there. Here is the screen shot where of where it was suppose to be,
My screen shot
I guess this is a VS bug. Anyway Just posted this so that anyone with team system 2008 should make a note of this :)
Typically the debugger is configures to step over properties, so your breakpoint will never be 'hit. Ensure that the following checkbox is not checked:
Tools => Options => Debugging => General => Step over properties and operators
It looks like you're accessing the Field Time (if that's not a typo in your question and you've omitted that part in your post), not the property Timer on classB.

Categories