Extension Methods with Custom Classes - c#

I'm attempting to extend my custom classes and running into a problem where it cannot find the extension method.. I have and can extend any built in classes or even ones contained within DLL's. I don't know if this is a compilation error or if I'm doing something wrong. Threw together a small program for an example, won't compile..
Here's the extension:
namespace ExtensionMethodTesting.Extension
{
public static class Extension
{
public static void DoSomething(this ExtensionMethodTesting.Blah.CustomClass r)
{
}
}
}
Here's the Custom Class:
namespace ExtensionMethodTesting.Blah
{
public class CustomClass
{
public static void DoNothing()
{
}
}
}
Here's the code calling it:
using ExtensionMethodTesting.Blah;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExtensionMethodTesting.Extension;
namespace ExtensionMethodTesting
{
class Program
{
static void Main(string[] args)
{
CustomClass.DoNothing();
CustomClass.DoSomething();
}
}
}
I must be missing something... Anyways the exact error just for clarification is:
Error 1 'ExtensionMethodTesting.Blah.CustomClass' does not contain a definition for 'DoSomething' c:\users\damon\documents\visual studio 2013\Projects\ExtensionMethodTesting\ExtensionMethodTesting\Program.cs 16 25 ExtensionMethodTesting

Extension methods require an instance of an object. You'll have to new up a CustomClass to use it.
var custom = new CustomClass();
custom.DoSomething();
See this answer as to why that is.

You need to instantiate an object of the CustomClass to use its extension method.
CustomClass obj = new CustomClass();
obj.DoSomething();

Related

C# error while calling class

I learned basic and now I want to learn OOP in C#
I have this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace uceni_cs
{
class Zdravic
{
public void pozdrav()
{
Console.WriteLine("Ahoj světe ! ");
}
}
}
But when I try to call it using this code
namespace uceni_cs
{
class Zdravic
{
public void pozdrav()
{
Console.WriteLine("Ahoj světe ! ");
}
}
Zdravic trida = new Zdravic();
}
In code Zdravic trida = new Zdravic();
is error. A namespace cannot directly contain members such as fields or methods.
What I am doing wrong ? I just want to call the class.
Thanks
In C# there is no such a thing global variable so you can't just create new instance of Zdravic type that does not belong to any class.
I suggest you to read General Structure of a C# Program, and c# Classes and Structs.
You need to create an entry point to your application and instantiate the class there.
class EntryPoint
{
static void Main()
{
Zdravic trida = new Zdravic();
trida.pozdrav();
}
}
Create your class object in main method and then use the class properties using that object.
Zdravic trida = new Zdravic();
in main method of you program/application.

Why is the class not accessible in unit test?

I have created a unit test for a method in a class called game.cs. For some reason, when I reference the class, I am unable to create a new instance. How do I make this class accessible so I can test my code?
File Hierarchy and solution:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BowlingKataTDD;
namespace BowlingKataTDDTest
{
[TestClass]
public class BowlingKataTDDUnitTests
{
[TestMethod]
public void DoesGameExist()
{
//arrange
BowlingKataTDD.
}
}
}
BowlingKataTDD Project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BowlingKataTDD
{
class Game
{
static void Main(string[] args)
{
}
}
}
The reason you do not see the classes is that they are non-public (internal by default).
There are two solutions to this:
If you would like to make your classes visible to outside users, make them public
If you would rather not publish your classes, use InternalsVisibleTo attribute.
To use the second solution, open AssemblyInfo.cs and add the following line:
[assembly: InternalsVisibleTo("BowlingKataTDDTest")]
BowlingKataTDDTest is the name of your assembly, as defined in the project file.
Make the class public, as well as any members of that class which need to be invoked externally (such as by a unit test):
public class Game
{
public static void Main(string[] args)
{
}
}
This is a common mistake with calling libraries with new projects, and I was able to resolve it. Often easy to forget to change the modifier to public from the project in Visual Studio, because by default a new project template creates the class though without it being set to public. When changing the Game class to public, am able to instantiate the Game object.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BowlingKataTDD;
namespace BowlingKataTDDTest
{
[TestClass]
public class BowlingKataTDDUnitTests
{
[TestMethod]
public void DoesGameExist()
{
//arrange
BowlingKataTDD.Game game = new BowlingKataTDD.Game();
}
}
}

Expected class, delegate, enum, interface, or struct (CS1518)

simple code here and the answers I find don't seem to work.
I'm using
SharpDevelop Version : 3.2.1.6466
.NET Version : 2.0.50727.5485
The problem is the error code
Expected class, delegate, enum, interface, or struct (CS1518).
Any ideas?
Program.cs codes:
using System;
using System.Threading;
namespace Threshold
{
public class Class1
{
public Class1()
{
Heritage YOLO = new Heritage();
YOLO.Fractal();
}
}
static void Main()
{
//do nothing
}
}
The cs file it calls is:
using System;
using System.Threading;
namespace Threshold
{
public class Heritage
{
int Fractal()
{
//Do stuff.
}
}
internal partial class DefineConstants
{
public const string DRIVERPATH = "d:\\tc\\bgi";
}
}
Please help with a fix.
Thanks.
Your main method is outside the class. Put it inside.

How to call an extension method without using

using System;
class Runner
{
static void Main()
{
A a = new A();
// how to say a.PrintStuff() without a 'using'
Console.Read();
}
}
class A { }
namespace ExtensionMethod
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
How would I call the extension method without a 'using'? And not ExtensionMethod.AExtensions.PrintStuff(a), since that doesn't make use of extension method.
It is possible to directly call your extension like so since it is simply a static method, passing the instance it will act on as the first this parameter:
A a = new A();
ExtensionMethod.AExtensions.PrintStuff(a);
This might be confusing to other developers who happen across this code if you followed this pattern for more commonly used extension methods. It would also make chaining extension calls such as LINQ appear more like a functional language because you would be nesting each call instead of chaining them.
that is possible if Extension Method and class A in same namespace,
If you have to use different namespaces then you have to use using, i don't think there is a way to do this without using. But you may reduce the number of using by putting all the extensions in one namespace like for Linq (System.Linq.Extensions)
Note : You can remove the namespace for Extension methods, then it will make them globally available
It needs the using to know where the function lives.
One example of this in Linq. Without the System.Linq using - you won't have linq enabled for any of your IEnumerable<T>'s
However, you can define the extension method in the same namespace as the caller to avoid putting in a using. This approach will however not work if it's needed in many namespaces
This makes me feel dirty, but you can put your extension methods in the System namespace.
This namespace is included by default in your question
using System;
class Runner
{
static void Main()
{
A a = new A();
// how to say a.PrintStuff() without a 'using'
Console.Read();
}
}
class A { }
namespace System
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
Ruminations on the creation of extension methods for a type ExtendableType:
Name the class ExtendableTypeExtensions
Declare the extension class partial so that clients can add extension methods following the same pattern; and
Put the extension methods in the same namespace as the base type
unless you have a very good reason to follow a model like that of LINQ:
A substantial family of extension methods,
That all apply to multiple base classes.
As of C# v6.0 (circa 2015) you can use using static to access a specific class's static members without including it's whole namespace.
An example, using your code, would be:
using System;
using static ExtensionMethod.AExtensions;
class Runner
{
static void Main()
{
A a = new A();
a.PrintStuff();
Console.Read();
}
}
class A { }
namespace ExtensionMethod
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
You can add extensions method without namespace.
This will affect the whole systems which is not recommended.
public static class StringExtensions
{
public static void HelloWorld(this String s)
{
Console.Write("Hello World");
}
}
string str = "s";
str.HelloWorld();
In our projects extensions are placed in the same namespace as class extension for. Your example:
A.cs:
using System;
namespace ANamespace
{
class A { }
}
AExtensions.cs:
namespace ANamespace
{
static class AExtensions
{
public static void PrintStuff(this A a)
{
Console.WriteLine("text");
}
}
}
Now when you add using for ANamespace for using the A class, all extensions for A class will be included too.

Why do extension methods not work with namespace aliasing?

This may be an ignorant question, but I'm unsure why I can not use namespace aliasing and extension methods together.
The following example works just fine:
Program.cs
using System;
using ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new Domain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
However, when I alias domain as follows in Program.cs:
using Domain = ExtensionMethodTest.Domain;
The extension method no longer works..
This can be rather frustrating when I'm dealing with converting various domain objects to contract objects (let's say I have 4 domain assemblies and 4 contract assemblies) for use in a web service. Using aliasing would be very handy as I could alias as follows and continue to use the various extension methods (such as ToContract, etc.):
using BillingContracts = Namespace.Billing.Contracts;
using IssuingContracts = Namespace.Issuing.Contracts;
etc...
I look forward to the answer.. I'm sure it's straight forward, but I, for the life of me, can't figure out why it doesn't work.
Thanks!
Make sure to still add a non-aliased using statement:
Program.cs
using System;
using ExtensionMethodTest.Domain; //DON'T FORGET A NON-ALIASED USING
using MyDomain = ExtensionMethodTest.Domain;
namespace ExtensionMethodTest
{
class Program
{
static void Main(string[] args)
{
var m = new MyDomain.MyClass();
var result = m.UpperCaseName();
}
}
}
MyClass.cs
using System;
namespace ExtensionMethodTest.Domain
{
public class MyClass
{
public string Name { get; set; }
}
}
MyClassExtensions.cs
using System;
namespace ExtensionMethodTest.Domain
{
public static class MyClassExtensions
{
public static string UpperCaseName (this MyClass myClass)
{
return myClass.Name.ToUpper();
}
}
}
I also love to use namespace aliasing but its not working in case of Extension methods. So one thing that i did is, I changed the namespace of extension class to same namespace that my main project has (although my extension class resides in sub folder of main project).
Suppose I have a project myFirstProj which surely has namespace myFirstProj for root classes. My extension class is present in myFirstProj/Common/myExtensionClass with contains namespace myFirstProj.Common { //myExtensionClass }.
So now what I did is, I changed the namespace of myExtensionClass from namespace myFirstProj.Common{ //myExtensionClass } to namespace myFirstProj{ //myExtensionClass } .
Now i can use my extension methods in my whole project myFirstProj event without specifying using statement for my extension class.
I know this isn't a standard way to that but I haven't found any other workaround for it expect this one because for my Project there is a requirement to go with namespace aliasing for project namespaces.

Categories