So in my normal course of getting some work done, i came across a generic System.NullReferenceException: 'Object reference not set to an instance of an object.'.
So no problem i'm thinking as I start looking for the culprit. After much debugging, I threw my hands up in the air and wanted to give up for the moment... My strategy for bypassing this was to just instantiate an array instead of calling some service for the data.
So i changed:
var data = service.GetData(someId);
to
var data = new DataType[]{};
to get my code compiling and debuggable so i can come back to this.
The kicker: upon doing so, i'm still getting the exception on that line. So JUST an array instantiation is yielding this exception. The exception has zero data to it. No inner exception or stack trace beyond pointing to this line. The class itself is just a poco with no methods (no constructor either) so nothing is happening internally. (see below for the class)
My next thought is that clearly, the code i'm seeing isn't what my debug session is using.
To test this thought I've
Cleaned in VS
manually deleted the bin
manually deleted obj
removed all package directories
restarted my computer
tried it in the new beta of VS
tried it in JetBrains' Rider
still i'm facing this.
Anyone have thoughts on how i can get more details on why this is happening, or is there something else I should be cleaning?
I've also looked at the output of git clean -xdn to see what kind of ephemeral stuff is hanging around that could be removed and nothing of interest there...
no matter what code is actually on the line, if i completely comment out this line and have completely different code there in it's place (as initially described), then that code throws the exception instead. i just had one of my colleagues grab this branch and run it and it was fine for them
3 hours deep so far... no fun
Edit:
as requested, here's a definition:
using System;
using Dapper.Contrib.Extensions;
namespace some_namespace
{
#pragma warning disable 1591
[Table ("CompanyOptions")]
public class CompanyOption
{
[ExplicitKey]
public Guid PKID { get; set; }
public Guid CompanyPKId { get; set; }
public string OptionName { get; set; }
public string OptionValue { get; set; }
public DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? UpdatedOn { get; set; }
public string UpdatedBy { get; set; }
}
}
So it seems there were issues with my installation of some microsoft packages.
I found that running Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r in the package management console to reinstall solved my problem. In looking at the diff that that yielded, the only meaningful thing is the following target framework bumps in my packages.config:
These package references haven't changed recently and the project has been targeting 4.7.1 for quite some time now so not sure why the sudden problem. Seems something became corrupted in the roslyn layer perhaps?
Going to slowly back away from my computer now.
I have a following piece of code:
[DefaultParameterValue(false)]
public bool IsDataAvailable{ get; set; }
This used to work fine when I was using Visual Studio 2013 Professional.
Recently, I upgraded to Visual Studio 2015 Professional version and this line started giving Error:
"CS0592 Attribute 'DefaultParameterValue' is not valid on this declaration type. It is only valid on 'parameter' declarations."
I could not get any help from google why this behavior occurs. Any idea?
Maybe there was a Bug in VS2013 and they fixed it in VS2015. That could be a
reason why it workd in VS2013.
You cant use DefaultParameterValue on Properties cause its for "Parameter".
You can use it for Methods.
If you want a default value for your Setter you can do somehting like this:
private bool _IsDataAvailable =false;
public bool GetIsDataAvailable()
{
return _IsDataAvailable;
}
public void SetIsDataAvailable([DefaultParameterValue(false)] bool b)
{
_IsDataAvailable = b;
}
Greetings
I'm trying to reproduce the C# compiler error CS0840 with the exact code that's given in the website:
class Test36
{
public int myProp { get; } // CS0840
// to create a read-only property
// try the following line instead
public int myProp2 { get; private set; }
}
public Form1()
{
InitializeComponent();
Test36 test = new Test36();
}
I'm running it on .NET 4.0 using Visual Studio Community 2015. Surprisingly, I cannot reproduce it. Compiler doesn't throw any error:
Why the compiler isn't throwing any error?
You're using Visual Studio 2015, which implements C# 6. The fact that you're targeting .NET 4 is irrelevant - most of the C# 6 language features don't depend on framework features at all. The C# 6 code you're using can easily be compiled without reference to any modern CLR or framework features - it could have worked with .NET 1.0 if the language designers had decided to :)
You'll need to set you language level to C# 5 to see an error here. Do that in the project properties / Build / Advanced dialog:
You'll then get this error:
error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 5. Please use language version 6 or greater.
Admittedly that's not the error you actually wanted to see - I think you'll need to use an earlier version of the compiler to get that exact error.
I guess it is because you are on Visual Studio 2015 with C# 6 which allows you to specify properties that are only set from the constructor (aka read-only properties).
See the following example:
class Test
{
public Test() // <-- this one does compile since it is the constructor
{
MyProp = 1;
}
public void SomeMethod() // <-- this one doesn't compile
{
MyProp = 1;
}
public int MyProp { get; } // <-- no CS0840 any more!
}
If I try to compile the following code with scriptCS it fails compilation with a syntax error for the line containing the primary constructor (Error CS 1514 '{' expected to be exact):
public class MyClass(string someName)
{
public string Name { get; } = someName;
}
I know that the new syntax features have to be enabled on a per-project basis in the first CTP of Visual Studio 2014, but couldn't find a similar setting for scriptCS (or any other information about C# 6 and scriptCS). My understanding is, that scriptCS is using Roslyn, so there should be a way to support those features.
So how do I enable the new C# 6 syntax features in scriptCS? Or isn't that possible (yet)?
The main.cs of my project returns the following warning:
Warning 1 The type 'Extensions.MessageDetails' in 'PATH\Extensions.cs' conflicts with the imported type 'Extensions.MessageDetails' in 'path\lib.dll'. Using the type defined in 'path\Extensions.cs'. path\main.cs
What is wrong with my project? How to get rid of the warning?
The code of my project has the following structure:
Extensions.cs
namespace Extensions
{
public class MessageDetails
{
public string message { get; set; }
public string link { get; set; }
public string picture { get; set; }
public string name { get; set; }
public string caption { get; set; }
public string description { get; set; }
public string userid { get; set; }
public string username { get; set; }
public object actions { get; set; }
public object privacy { get; set; }
public object targeting { get; set; }
}
}
lib.dll
namespace MyClassLib {
public class MyClassLibFoo {
public void foo(MessageDetails parameters) {
/* .. */
}
}
}
main.cs
using MyClassLib;
using Extensions;
class Program
{
static void Main(string[] args)
{
MessageDetails md = new MessageDetails();
}
}
In my case, with Visual Studio 2013, I found that one of my class libraries had developed a reference to itself. I think it happened when I added a new project to my solution or it was a bug, but either way it was causing this exact issue.
Check your project references for any circular references.
It seems like Extensions.cs is both part of the project that builds lib.dll and your main.exe
Remove it from one of the project to fix this issue.
I had this kind of issue where I had reverted from a target .NET Framework version of 4.5.2 to 4.0.
Classes in my App_Code folder had methods that called methods in other classes in that folder. When I created a standard folder I named "AppCode", and moved my classes into it, I no longer had the issue.
If I re-created the "App_Code" folder and move my classes back into it, I will have this issue again. I'm convinced it has to do with my .NET Framework version or that Visual Studio just doesn't deal well with changing it after being initially built/targeted to another version.
You can't have two copies of the extensions class, even though the code is the same they are not seen as the same object. Both your dll and main application will need to reference the exact same one.
You could try creating a 'Common Files' class library and add the extensions class to it, that way you will always be using the correct class
I had this problem with a project that is also hosted on NuGet. I checked all project references. Finally, the object browser revealed that the DLL of an older version of my NuGet package was somehow loaded in Visual Studio from the NuGet cache folder ("C:\Users\{username}\.nuget\packages"). I removed the package from the cache folder, it disappeared from the object browser and everything was working fine again.
I had a Shared Project, "Project A," which was included in both "Project B" and "Project C."
"Project A" was added as a Shared Project in "Project B" and "Project C."
"Project A" also included a traditional reference to "Project B."
To correct the problem, I removed the reference to "Project B" from "Project A."
If you really need to have both classes declared or referenced in two separate dll, you can mark your class as internal.
Internal types or members are accessible only within files in the same assembly, therefore it will prevent the collision.
After reading through many answers on SO the solution was still unclear. My situation was similar but the solution was found by:
Example project Name: My.Example.Project
Opening my project
Open the References dropdown
Finding My.Example.Project in the References section
Deleting the reference to My.Example.Project
That fixed it!
I had faced same problem. Just a simple solution for that.
Check your project references there must be same project reference. just remove that, it will work.
sometimes I get this error - it's a bug though in my case.. All I have to do to fix it is change the first letter of my script file name from upper case to lowercase in the file in Explorer /
(or in Unity Engine in my case)
and then change the name / class accordingly in my script. Idk why this happens.. just does - and Idk why this fix works .. but in my case it always does. - Otherwise you probably have 2 copies of the same script / same class name for 2 diff scripts. Hope this helps.
I fixed this error by deleting the .suo file in the directory sturcture vs directory. stop vs then delete restart vs. that worked for me.