C# error, member with same name - c#

I writed this code, but, when I try to build, the compiler returns:
1>code.cs(16,16,16,44): error CS0542: 'DataGridViewPercentageColumn': member names cannot be the same as their enclosing type
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
namespace TestSample
{
internal static class DataGridViewPercentageColumn
{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
}
How I can solve this, and, why this error happens??
Thanks

You cannot nest two classes with the same names. Your inner class DataGridViewPercentageColumn has the same name like the outer class. You have to rename one of these classes like:
internal class DataGridViewClasses{
public class DataGridViewPercentageColumn : DataGridViewColumn
{
public DataGridViewPercentageColumn() : base(new DataGridViewPercentageCell())
{
}
}
public class DataGridViewPercentageCell : DataGridViewTextBoxCell
{
public DataGridViewPercentageCell()
{
this.Style.Format = "0%";
}
}
}
Btw: You cannot have any other members or properties inside a class named like the class itself.

Your wrapper class has the same name as one of the internal classes.
The issue is DataGridViewPercentageColumn.

The problem here is that you've defined a class within another class, both of which have the same name. Give the internal static class a different name.

Related

Xamarin error: Cannot implicitly convert type 'MasterDetail.G2.Main' to 'Xamarin.Forms.Page'

When I try to change the main page it gives me the error even when I use navigation page.
Should I use as to change the type to portable type? What is the best practice?
error:Severity Error CS0029 Cannot implicitly convert type 'MasterDetail.G2.Main' to 'Xamarin.Forms.Page'
Main.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MasterDetail.G2
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Main : ContentView
{
public Main()
{
InitializeComponent();
}
}
}
app.xaml.cs
using MasterDetail.G2;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MasterDetail
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new Main();
}
protected override void OnStart()
{
}
protected override void OnSleep()
{
}
protected override void OnResume()
{
}
}
}
change
public partial class Main : ContentView
to
public partial class Main : ContentPage
It is not certain what you want to do, but you can't cast some class to another class that doesn't inherit from it. ContentPage doesn't inherit from ContentView or your Main class.
change the derived class from ContentView to ContentPage
on Main.xaml, change to

Can not access static class from .dll without namespace prefix

I have a class library project with definition like this:
namespace MyNamespace
{
public static class MyClass
{
public static string GetString()
{
return "test";
}
}
}
When I reference the built .dll of this project in my another project/solution, I can access the class MyClass like MyNamespace.MyClass.GetString() but I can't access by referenceing the namespace on top of c# file. Ex.
using MyNamespace;
namespace ProjectNameSpace
{
public class TestClass
{
public void TestRun()
{
string result = MyClass.GetString(); // Getting error: are you missing an assembly reference?
}
}
}
It may be a silly question, but I am not getting why this is happening. Any help on this is really helpful to me.
Are you sure that the name of the namespace was different from the name of the class in the library? I have just run into a problem similar to this one, and after a few minutes of experimenting I realised that was the problem.
Here's an image showing the existance of the problem. Note that testLib is in references and is also using'd.
Here's the code of the library
namespace testLib
{
public class Class1
{
public Object getMe()
{
return this;
}
}
public static class Class2
{
public static int getRandomNumber()
{
return 7;
}
}
public class testLib
{
public static void testMe()
{
Console.WriteLine("working just fine...");
}
}
}
Here's the code of the tester class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testLib;
namespace libTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class2.getRandomNumber());
testLib.testLib.testMe();
testLib.testMe();//when i hover here, it says "The type or namespace does not exist in the namespace testLib (are you missing an assembly reference?)"
Console.ReadKey();
}
}
}

Class Library Namespace Hierarchy Not Working

I am trying to achieve the namespace hierarchy used in C# in my class library. Here is what I am trying to do:
namespace Parent
{
namespace Child
{
Class ChildClass { }
}
Class ParentClass { }
}
After compiling the class library It did not work as expected. Here is my expected working principle.
To access the ChildClass one has to using Parent.Child. But one can access ParentClass just by using Parent.
I can do this without compiling the class library but adding the cs file to the project. But when I compile as DLL and add it as a reference in a project I can't access the sub-namespaces.
UPDATE: I have different files for each class. When I write all namespaces and classes into one file it seems to work. But why?
Is there anyway to achieve this in C#?
I think your classes missing public; Following code works for me.
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
public class ParentClass
{
}
}
I can create;
Parent.ParentClass p;
Parent.Child.ChildClass c;
Which is your expected working principle.
EDIT: separate cs file for each class approach;
ParentClass.cs
namespace Parent
{
public class ParentClass{ }
}
ChildClass.cs
namespace Parent
{
namespace Child
{
public class ChildClass { }
}
}
This seems to be working for me.
You are nesting classes and namespaces and it all seems a little confused. Why don't you keep a flatter namespace structure and do the nesting in your classes. Keep in mind that you don't need to nest namespaces or classes to maintain a parent child relationship.
Have a read of the following: Parent child class relationship design pattern
This should get you started in the right direction:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
using System;
using System.Collections.Generic;
public class ChildClass
{
private ParentClass parent;
public ChildClass(ParentClass parentIn)
{
parent = parentIn;
}
public ParentClass Parent
{
get { return parent; }
}
}
public class ParentClass
{
private List<ChildClass> children;
public ParentClass()
{
children = new List<ChildClass>();
}
public ChildClass AddChild()
{
var newChild = new ChildClass(this);
children.Add(newChild);
return newChild;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var p = new ParentClass();
var firstChild = p.AddChild();
var anotherChild = p.AddChild();
var firstChildParent = firstChild.Parent;
var anotherChildParent = anotherChild.Parent;
}
}
}

Windows Phone 8 - Saving a List to IsolatedStorage

I am trying to save a collection of object that are a type of my class. I get an error stating:
The collection data contract type 'System.Collections.Generic.List cannot be deserialized because it does not have a public parameterless constructor.
Adding a public parameterless constructor will fix this error.
Here is my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MojProjekt
{
class Lekarna
{
public string Ime { get; set; }
public Lekarna()
{
}
}
}
And here is how I save to the IsolatedStorage:
List<Lekarna> lekarneList = new List<Lekarna>();
// here I then fill the list ...
IsolatedStorageSettings localStorage = IsolatedStorageSettings.ApplicationSettings;
localStorage.Add("lekarneList", lekarneList;
localStorage.Save();
make the class public
public class Lekarna

extending classes that must be used in an interface

I have created an interface as shown below. The DTO object is a complex value object with 3 parameters.
public interface IOperation
{
DTO Operate(DTO ArchiveAndPurgeDTO);
}
I need people that impliment this interface to be able to inherit from the original Value object and extend it where required.
My assumption was that they could simply inherit the DTO object, add (for example) another property and use it in the same class that impliments this interface.
When I try to use the extended value object, Visual Studio complains that I am no longer implimenting the interface.
How can I impliment this functionality.
Thanks in advance for any ideas, and/or suggestions.
Gineer
Edit:
DTO Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Company.ArchiveAndPurge
{
public class DTO
{
public DTO(String FriendlyID)
{
friendlyId = FriendlyID;
}
private String friendlyId = String.Empty;
public String FriendlyId
{
get { return friendlyId; }
set { friendlyId = value; }
}
private String internalId = String.Empty;
public String InternalyId
{
get { return internalId; }
set { internalId = value; }
}
private Boolean archivedSuccessfully = false;
public Boolean ArchivedSuccessfully
{
get { return archivedSuccessfully; }
set { archivedSuccessfully = value; }
}
}
}
Extended DTO:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Company.MSO.ArchiveAndPurge
{
public class DTO: Company.ArchiveAndPurge.DTO
{
private Boolean requiresArchiving = true;
public Boolean RequiresArchiving
{
get { return requiresArchiving; }
set { requiresArchiving = value; }
}
}
}
Interface Implementation where VS Complains:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Company.ArchiveAndPurge.Contracts;
using Company.ArchiveAndPurge;
namespace Company.MSO.ArchiveAndPurge
{
public class ResolveFriendlyId: IOperation
{
#region IOperation Members
public DTO Operate(DTO ArchiveAndPurgeDTO)
{
ArchiveAndPurgeDTO.InternalyId = ArchiveAndPurgeDTO.FriendlyId;
return ArchiveAndPurgeDTO;
}
#endregion
}
}
As I understand it, you probably had something like:
public class ExtendedOperation : IOperation
{
public ExtendedDTO Operate(ExtendedDTO dto)
{
...
}
}
That doesn't work in two ways:
You can't change the return type when implementing an interface method
You can't change the parameter list when implementing an interface
In particular, you wouldn't be implementing IOperation in a way which would be compatible with code like this:
IOperation operation = new ExtendedOperation();
operation.Operate(new DTO());
I suspect you might want to make the interface generic:
public interface IOperation<T> where T : DTO
{
T Operate(T dto);
}
Use Generics:
public interface IOperation<T> where T : DTO
{
T Operate(T ArchiveAndPurgeDTO);
}

Categories