|
Sorry about that. Posted in the right forum.Thanks for informing.
|
|
|
|
|
I have 3 classes overriding a function of base abstract class A. The child classes are 1, 2 & 3. Child classes override function "public abstract void hello(var1, var2)".
Following is what I want to implement:
abstract class A
{
public abstract void hello(var1, var2);
public abstract void hello(var1, var2, var3);
}
** Currently the above code throws error and wants me to implement the new inherited abstract function in class 1 & 2.
|
|
|
|
|
No, an abstract method in class A must be implemented by all (non-abstract) derived classes.
If the new method is only available in class 3, then add it to class 3. You can't add it to class A, because it's not valid for every instance of a class derived from class A.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
You implementation force Class 1 and 2 to implement "hello(var1, var2,var3)" just because you want to have a extra method for Class 3. This is not a good design to follow.
You should follow Open to extend and close to modification methodology.
you might create an extension of class A and implement it for Class 3. Please refer the approach below:
abstract class A
{
public abstract void hello(var1, var2);
}
abstract class A1 : A
{
public abstract void hello(var1, var2, var3);
}
Implement Class A for Class 1 and Class 2
Implement Class A1 for Class 3
Maddy
|
|
|
|
|
I'm not fully convinced about the quality of this idea, yet decided to give it a try.
Use Case:
I got an abstract class Factory , which then is derived by other classes - Nothing new so far. What I want to have is a single instance of each and every class deriving from Factory . Why? No particular reason, just wondering whether it can be don the way I imagine:
public abstract class Factory
{
private readonly object _instanceLock = new object();
private Factory _instance = null;
internal Factory GetInstance(Type t)
{
if (_instance != null) return _instance;
if(t != typeof(Factory)) throw new InvalidOperationException("Factory can't created an instance not derived from " + typeof(Factory).FullName);
lock (_instanceLock)
{
if (_instance != null) return _instance;
Thread.MemoryBarrier();
var assembly = Assembly.GetAssembly(t);
_instance = (Factory)assembly.CreateInstance(t.FullName);
}
return _instance;
}
protected Factory() { }
}
|
|
|
|
|
Marco Bertschi wrote: Why? No particular reason I'm missing the definition of the object that is being created, but I'm also missing the concept - a factory for a single class?
You are building a generic car-factory, to produce cars that do not come from an assembly-line?
What does it add, what advantages do you gain, compared to simply writing a static-class and using that?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: I'm missing the definition of the object that is being created
A Factory (Stupid naming on my side, probably)
Eddy Vluggen wrote: but I'm also missing the concept - a factory for a single class?
YNOES. As mentioned, the term factory isn't greatly chosen. The idea behind it was to manage documents which are very specific tied to the main application. Some customers prefer databases, some file system store.
My idea however was to have this Big Mama Factory class over two other classes, one managing storage of database stored files the other one managing file system files (God, this terminology sucks).
Do not ask me why I went for singleton first, I then realized that it doesn't add anything an interface couldn't do - But at this point I already wrote it, and now I want to know whether my glitch in the matrix *could* have any *potential* use cases in the real world.
Eddy Vluggen wrote: What does it add, what advantages do you gain, compared to simply writing a static-class and using that?
I shortened the example a bit - From the expectations an interface would fit a lot better.
|
|
|
|
|
Marco Bertschi wrote: Some customers prefer databases, some file system store. Go for a strategy pattern
Marco Bertschi wrote: Do not ask me why I went for singleton first, I then realized that it doesn't
add anything an interface couldn't do That's what got me confused; why singletons? The terminology hints that something is wrong; a factory returns an object it creates (from a class), where in the singleton there is only one instance of the object.
Marco Bertschi wrote: I then realized that it doesn't add anything an interface couldn't do Have the factory return an object, and drop it in an interface; could work in a similar way as we now use the database-classes; you can program against the interface, and have a class instantiate whatever concrete class that you want and pass it along. Similar to that IDbConnection that you can open, you can create a SqlConnection, or a concrete MySqlConnection or a SQLiteConnection.
You could do similarly for your store; the factory should decide which object should be created, and the rest of the application only uses the interface. Create one implementation for the database, one for the filesystem.
It also does not mean that you only want "one" of those objects; you want to be able to instantiate them multiple times, as you would with a database-connection. Yes, there's only one server, but I can talk to it from multiple connections
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Marco Bertschi wrote: What I want to have is a single instance of each and every class deriving from Factory .
The code you've posted won't do that.
Firstly, if you pass anything other than typeof(Factory) to your GetInstance method, you'll get an InvalidOperationException - typeof(DerivedFactory) != typeof(Factory) .
Secondly, if you pass typeof(Factory) to your GetInstance method, you'll get a MissingMethodException - you can't create an instance of an abstract class.
Thirdly, even if you fix the previous errors, you would only ever be able to have a single instance of one derived class in a single AppDomain. The static field is specific to the Factory class; you don't get a different copy for each derived class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I agree with the others. There are a lot of problems with trying to do this.
The biggest I have is WHY? What advantage is there to creating a Factory for a class that can only ever have a single instance?
|
|
|
|
|
Hi,
Is it possible to use extension methods in C# using .NET 2 ?
I'm using Visual Studio 2013.
Thanks
|
|
|
|
|
No.
Extension methods were introduced as part of V3.0 - if you want to use them, you must target .NET 3.0 or higher.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
v3.5, shirley? They were added with LINQ.
And, as Sascha said, if you're using the right version of the compiler, you can still use extension methods even if you're targeting v2.
There's even a library[^] which lets you use LINQ in v2, so long as you're using the right compiler.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I didn't know that!
And any day when I learn something new is...well...another day really.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
Yes. To make it work, you have to place this somewhere in your project:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute { }
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
ok im trying to make a script placer witch will move loads of files at once and rename them depending what name is in the text box.
the only issue i have it im not sure on how to do this i know its a pain i know the basics of c# made a few programs just dont know how to connect a text box and button to move and rename multiple files all at once if anyone knows how to do this or could help me out a little with example code would be amazing i dont want anyone to create this for me just need a little bit of a understanding in this
thank you all for your time
thanks in advance elfenliedtopfan5
|
|
|
|
|
Many of the class methods in the documentation come complete with example code. For instance, https://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.110).aspx[^]
You can lead a developer to CodeProject, but you can't make them think.
The Theory of Gravity was invented for the sole purpose of distracting you from investigating the scientific fact that the Earth sucks.
|
|
|
|
|
can you have me? source code open, save as, and help menu..
example we work on notepad and then save with format (txt) and then open on our program (this program read the txt) then have result after that save as txt again..
in help menu, how to display new form (about) etc.. i dont understand.. help me to finished this project
|
|
|
|
|
I'm afraid that you don't seem to understand the purpose of this site. If you have a need of software, take a look at our articles. If you're lucky, you'll find examples there that give you the help and information you need. If you cannot find a suitable article, then you need to provide us with a detailed explanation of what problems your code is facing. Show us the relevant code sections and explain clearly what errors you are seeing.
What we don't do is write your code for you. That's your responsibility I'm afraid.
|
|
|
|
|
Member 11773338 wrote: help me to finished this project
It doesn't really sound like you have started - much less need help to finish!
This is all pretty basic stuff: it's the kind of problem you give to students as homework after a week or two of a C# course, so pretty much you should know everything you need to solve it - probably in your course notes.
So give it a try: start small, and break it up into separate tasks. If you can't do one of them, break that up into smaller tasks and try again. Pretty quickly, you should find solving the little problems solves the big ones!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hello guys. I used following tutorial as a guide for adding/modifying a satellite assembly.
Creating Satellite Assemblies for Desktop Apps.
I am able to create
1- new .resx file
2- create a .resource file from it
3- and create satellite assembly from the .resource file.
I use following two commands for creating assemblies
1: resgen string.en-US.resx
2: al /target:lib /embed:string.en-US.resources /culture:PATH TO DEBUG\en-US\TestApp.resources.dll
I create a new folder for culture I am interested in, in the Debug folder and place the newly created satellite assembly in it. When I run it, I get the exception
Could not find any resources for the specified culture or the neutral culture. Make sure "TestApp.string.resources" was correctly embedded or linked into assembly "TestApp" at compile time or that all the satellite assemblies required are loadable and fully signed.
One thing of interest here is: if I just recompile my project then changes made are picked up. But if I don't recompile my project then running the .EXE from debug folder gives this exception. WHAT COULD BE WRONG ??? (hopefully I described well) Thanks for any input.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
The exception mesage says that resources are completely missing - not only for a specific culture. I'd suggest to get the project running without satellite assemblies first, and then add them.
By the way, en-US is typically the default culture, unless you explicitely set it to something else.
|
|
|
|