|
AmbiguousName wrote: But at the moment I want to do this without static.
I'll shut up then
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Static can be abused and lead to very bad programming practices which is probably why they are considered bad.
However, a corectly implemented Singleton[^] is a special use case that fits your situation perfectly so should be used IMO.
|
|
|
|
|
Thank you for the link. This was really informative for me.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Singletons are just static classes hidden behind a layer of obfuscation. They are worse
|
|
|
|
|
BobJanova wrote: Singletons are just static classes hidden behind a layer of obfuscation
Would you care to expand on this as I'm pretty sure that's incorrect?
|
|
|
|
|
Sure. Access to a true singleton is via a static method (the getter of an Instance property in C#, but it's still a method). By definition, you can only get one instance, so there's only one available area for state storage, and the type of that instance is known at instantiation time.
The only one of those things that is not directly repeatable by a static class is the last, if you have some sort of factory that returns an instance for a singleton based on something else. Even then, you can delegate the methods which have actually changed from static methods. And such a method would normally be indicative that you should be using dependency management instead of manually coded singletons, anyway.
Let's look at a simple example, in pseudo:
singleton class MyClass {
public static singleton Instance { create { return new MyClass() } };
private int nextID = 100;
public int NextValue() { return nextID++; }
}
This can be directly replaced with
static class MyClass {
private static int nextID = 100;
public static int NextValue() { return nextID++; }
}
... that is, by making all methods and state static, and removing the singleton code. This also means that you aren't wasting memory for an object instance that is there simply to wrap methods and application-wide (i.e. static) state.
|
|
|
|
|
Thanks for your reply.
There are a couple of other things that you can do with a singleton that you can't do with a static class...
A singleton, being a class instance, can be passed as a parameter, be inherited from, implement interfaces and probably many other things too.
I take your points (all valid) on board, but I still feel that there is a distinct, and often benificial, difference to a static class.
|
|
|
|
|
You are probably right, I overstated that one slightly. However, if you're passing an object around, most of your code isn't interested in it being a singleton, and you should perhaps wonder if it needs to be one at all.
|
|
|
|
|
The problem here is that the sc instance in Tab1 is a local variable, which in fact is never set so it's null. You need to refer to the single instance you created before.
Actually it seems like what you want is a singleton of MainDlg. SingleClass is a complete waste of space as it's just delegating to MainDlg. So how about
class Singleton<T> where T: new() {
private static object lockObject = new object();
private static T instance;
public static T Instance {
get {
if(instance == null){
lock(lockObject) {
if(instance == null) instance = new T();
}
}
return instance;
}
}
(I think that's the standard singleton pattern as current best practice in C#?)
And then when you want access to the main dialog for UI purposes you can do
public class Tab1
{
public void Function_InTab()
{
Singleton<MainDlg>.Instance.ShowMessage();
}
}
Edit: added .Instance into the last function, and static to the singleton code (duh).
modified 18-Jul-12 8:26am.
|
|
|
|
|
I'm afraid that your Singleton<T> implementation will not guarantee that only a single instance will exist. There is nothing to prevent calling new on the T class. In fact invoking your Singleton implementation twice will create a new instance each time!
Here's how I deal with singleton's:
public sealed class Foo
{
#region Singleton
private static readonly Lazy<Foo> _Instance = new Lazy<Foo>(() => new Foo());
public static Foo Instance { get { return _Instance.Value; } }
private Foo()
{
}
#endregion
public void Method()
{
}
}
Usage like:
Foo.Instance.Method();
It is NOT possible to create a singleton without the Instance property and the _Instance backing field being static . Something has to exist outside the specific instance of the class.
There are lots of articles about the singleton pattern, I suggest you read a few.
|
|
|
|
|
It will guarantee it if the person writing the code respects the interface – which considering he has access to the source of Foo is all you can rely on anyway.
Putting the singleton code inside the class means that it is duplicated in every class that you want Singleton behaviour for. I prefer to rely on an intelligent developer and avoid code duplication. This is a similar approach to that taken by dependency injection frameworks, where you still can manually create objects, but you are trusted not to do so if it's inappropriate.
In fact invoking your Singleton implementation twice will create a new instance each time!
Now that I believe not to be the case, since Singleton<T>.Instance checks for the existence of an instance (in fact that is the entire purpose of the Singleton class).
Edit: though I had forgotten to declare the instance field and property static
modified 18-Jul-12 8:27am.
|
|
|
|
|
Your edit corrected the basis for my comment that the Singleton did NOT guarantee only one instance!
|
|
|
|
|
I am Doing a project on Barcode using c#. I am facing difficulty in finding the related libraries that is needed to generate the barcode. I found some barcode classes and methods in Microsoft.policy.RecordsManagement.Policy namespace under Microsoft.policy.dll.
But this library is not preferable as we need share point to use it.
Other than this I am finding some third party barcode generators.Is there any free libraries to generate the barcode other than Free 3 of 9.?I can generate a barcode using free 3 0f 9. can anyone please help me out in improvising on my info?
I would like to use code 39 or code 128 Symbologies.basically linear barcodes.I'm looking for a free to use c# library/code to create barcodes.(open source or free)?
|
|
|
|
|
|
A number of barcodes are available as fonts. You will still need to add start and end symbols, calculate checkdigit, etc, but the actual printing is done for you. As a starting point, take a look here[^]
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
thanks a lot.I have tried with free 3 of 9 i.e code39 font true type.But the question is it flexible to create the barcodes under font types without the need of libraries to be referenced to the project?
|
|
|
|
|
If you can do it with code 39 (3 of 9), you should be able to use other codes (such as code 128) in the same way. The checkdigits, begin/end codes will be different, but the rules for each type of code are well-defined and easy to find on the web.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I would like to know the native libraries that are available in windows for generating,scanning and to print it.
|
|
|
|
|
There are none. There is nothing built into Windows or the .NET Framework that is used specifically for scanning and generating barcodes.
You either have to write the code to do so or find libraries that support the features your app requires.
|
|
|
|
|
You are saying will have to buy those libraries then?Is it possible to create an own barcode component library?
|
|
|
|
|
No, I'm not. There are free libraries out there, but what you use depends on the barcode format you're using. Not every library supports every format.
OF COURSE it's possible to write you're own. You just need a very deep understanding of the barcode format you're using and some ingenuity.
|
|
|
|
|
I am basically just concentrating on Code39 and code128 symbologies.so trying to find the native libraries related to these two.
For scanning,If we are using the interface as serial port then there are some namespaces related to it. System.io.ports.
|
|
|
|
|
Why not make it easy on yourself and use one of the free barcode fonts[^] that are available?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I did generate using one of the barcode fonts free font 3 of 9.
How does these fonts work?I mean how is it different from other barcode libraries?can these barcode fonts be used for commercial purpose too?Is it feasible enough to use in commercial applications?
|
|
|
|
|
Akshay_88 wrote: How does these fonts work?
Fonts are fonts, they are merely a pattern of dots for each available character. If you wish to use them for commercial purposes then you need to look up the specification for each font that you are using to ensure it complies with the standards.
One of these days I'm going to think of a really clever signature.
|
|
|
|