Can't create methods in a c# class ( Visual studio 2017) - c#

please i just encountered this problem and i dont know how to go about it. I've tried to reset Visual studio settings to no avail.
I just created a new class and when i try to create a method in it, i get the following errors;
property or indexer must have at least one accessor,
property or indexer cannot have a void type
the problem is that i'm actually trying to create a method and not a property.
below is the class
public static class IoCContainer
{
public static void Setup
{
}
}
I don't know if i mistakenly toogle a visual studio setting or something.

The problem is that you're missing the parenthesis after Setup. Changing to the following should fix your issue:
public static void Setup()
{
}
The compiler thought you were trying to create a Property due to the missing parens.

You are missing parentheses () at the end of your function name.
So your code should be:
public static class IoCContainer
{
public static void Setup()
{
}
}

Related

Customize "quick fix" behaviour in Visual Studio

I'm on Visual Studio for Mac 8.0.9 and would like to change the behaviour of several "quick fixes" (the options in the menu when I hit ALT + enter).
Specifically, I'd like to change the Create and initialize field 'myParam' option.
Right now it does this:
/// before quickfix
public class Test
{
public Test(object myParam) { }
}
/// after quickfix:
public class Test
{
public readonly object myParam
public Test(object myParam)
{
this.myParam = myParam
}
}
In our code base we're using underscores for private fields, as inspired by these naming conventions. Therefore I'd like to change the behaviour to this:
/// after quickfix:
public class Test
{
public readonly object _myParam
public Test(object myParam)
{
_myParam = myParam
}
}
Is this possible?
[Edit]
Things I tried:
looked for an option like the one described in the accepted answer
here. Seems to be no UI option in VS Mac
Tried configuring a .editorconfig file as described
here. Tried putting
it in the solution root as well as one of the root of one of the
containing project, but it doesn't seem to work for me

For C# Project, how to raise a custom build error if someone tries to use specific method from framework class?

For a C# Project, I want to include a build step or something integrated in project that should raise build error if any developer is trying to use a specific method from framework classes, instead I want developers to use extension method for same. However I want to impose this as the compile time error. As an example, for a name sake I want developer on given project not to use string.Intern, instead should always use string.SpecialIntern. What are different ways to achieve this? I tried to use Roslyn-code-analysis but could not really write working rule for this, so I am not sure if tha'ts the right solution to this problem. Can someone guide me in details how to solve this with some examples?
This sounds like something you could accomplish with a custom code analyzer. I haven't tried it yet, but I believe it is possible to write your own analyzers.
This article from Microsoft claims to tell you how to do it:
https://learn.microsoft.com/en-us/visualstudio/extensibility/getting-started-with-roslyn-analyzers?view=vs-2017
Here's a direct link to the tutorial referenced in that article:
https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix
AFAIK there's no way to achieve what you're trying to do.
However, a solution would be to simply call the extension method.
public static class Extension
{
public static bool DoStuff(this Class stuff)
{
return false;
}
}
public class Class
{
public bool DontCallMe()
{
return this.DoStuff();
}
}
Class myClass = new Class();
myClass.DontCallMe();
Obviously, that only works if you can change the code of your class (which I suppose you aren't able to)
If that method is marked as virutal, you could create a Wrapper-Class which overrides that method.
public static class Extension
{
public static bool DoStuff(this Class stuff)
{
return false;
}
}
public class Class : Base
{
public override bool DontCallMe()
{
return this.DoStuff();
}
}
public class Base
{
public virtual bool DontCallMe()
{
return false;
}
}
Another approach would be to do what I described in this this post.

No constructors defined

I have some code base which has is calling the following:
SetHazardDataService();
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
}
}
With a member function declared in another class/file
using Analytics.Foo.DataServices
public void MyDataService()
{
var DbDataSvc = new HDB();
}
originally, I see the same definition used elsewhere but with (no idea if that works):
protected void MyDataService()
I included the public method in my class
I'm now trying to recreate that functionality, but I get the following issue:
The type Analytics.Foo.DataServices.HDB' has no constructors defined
I'm not sure what the issue is - any suggestions for why this is the case. There is no constructor that I can see. Plus I'm not able to see the other code working/but it doesn't give the same issue.
You need to create a constructor to class HDB, like this:
namespace Analytics.Foo.DataServices
{
class HDB:IDataService
{
public HDB()
{
}
}
}

Why is the constructor of a generic class with a default-valued enum parameter not able to call protected methods of that class?

A simple test case:
using System;
public class Test<T>
{
public enum TestEnum
{
A,
B
}
public Test (TestEnum a = TestEnum.A)
{
DoSomething ();
}
protected void DoSomething()
{
}
}
The compiler (this is using Mono in a Unity3D project, .NET4.0 target) gives an error on the call in Test() to DoSomething(). If I remove the default parameter on TestEnum a, it builds just fine. MonoDevelop wants to call the default parameter TestEnum<>.A, but that doesn't compile, neither does TestEnum<T>.A (obviously I wouldn't have expected these to work but using MonoDevelop's autocomplete that's what I get).
EDIT: the specific error is: the name DoSomething doesn't exists in the current context
As said so in the comments, this is a compiler bug.
It seems as your Mono development environment doesn't really like the protected keyword.
Use {public,private} for now.

How to hide public methods from IntelliSense

I want to hide public methods from the IntelliSense member list. I have created an attribute that, when applied to a method, will cause the method to be called when its object is constructed. I've done this to better support partial classes. The problem is that in some environments (such as Silverlight), reflection cannot access private members, even those of child classes. This is a problem since all of the work is done in a base class. I have to make these methods public, but I want them to be hidden from IntelliSense, similar to how the Obsolete attribute works. Frankly, because I am anal about object encapsulation. I've tried different things, but nothing has actually worked. The method still shows up in the member drop-down.
How do I keep public methods from showing up in IntelliSense when I don't want them to be called by clients? How's that for a real question, Philistines! This can also apply to MEF properties that have to be public though sometimes you want to hide them from clients.
Update:
I have matured as a developer since I posted this question. Why I cared so much about hiding interface is beyond me.
Using the EditorBrowsable attribute like so will cause a method not to be shown in IntelliSense:
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public void MyMethod()
{
}
You are looking for EditorBrowsableAttribute
The following sample demonstrates how to hide a property of a class from IntelliSense by setting the appropriate value for the EditorBrowsableAttribute attribute. Build Class1 in its own assembly.
In Visual Studio, create a new Windows Application solution, and add a reference to the assembly which contains Class1. In the Form1 constructor, declare an instance of Class1, type the name of the instance, and press the period key to activate the IntelliSense drop-down list of Class1 members. The Age property does not appear in the drop-down list.
using System;
using System.ComponentModel;
namespace EditorBrowsableDemo
{
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
int ageval;
[EditorBrowsable(EditorBrowsableState.Never)]
public int Age
{
get { return ageval; }
set
{
if (!ageval.Equals(value))
{
ageval = value;
}
}
}
}
}
To expand on my comment about partial methods. Try something like this
Foo.part1.cs
partial class Foo
{
public Foo()
{
Initialize();
}
partial void Initialize();
}
Foo.part2.cs
partial class Foo
{
partial void Initialize()
{
InitializePart1();
InitializePart2();
InitializePart3();
}
private void InitializePart1()
{
//logic goes here
}
private void InitializePart2()
{
//logic goes here
}
private void InitializePart3()
{
//logic goes here
}
}

Categories