473,666 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cast generic IFoo<T> : last level (I hope)

Hello again,

My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...

abstract class BaseStaticOverr ide<Twhere T : BaseStaticOverr ide<T>
{

private static T _instance;

private static object _lock = new object();

public static T Instance
{

get
{

if (_instance == null)
{

lock (_lock)
{

if (_instance == null)

_instance =
(T)Activator.Cr eateInstance(ty peof(T), true);

}

}

return _instance;

}

}
}

abstract class C1Base<T: BaseStaticOverr ide<Twhere T : C1Base<T>
{

protected C1Base()
{

}

public void WriteLine()
{

Console.WriteLi ne(Line);

}
protected abstract string Line
{

get;

}
}
class C1 : C1Base<C1>
{

protected C1()
{

}

protected override string Line
{

get { return "C1"; }

}

}

C1 can be considered as a static method with my code and so C1 and
C1Base<C1are the same class.
The problem occures when I don't know which class implement C1Base
(C1Base<?>). Since C1Base<?can be considered as static, I think it would be
possible to do this:
C1Base<?>.Insta nce.WriteLine() ; // which will return "C1"

Hope you understand what I want to do.

Thanks in advance for your help.
Jul 25 '08 #1
4 1185
On Jul 25, 1:29*pm, Alphapage <Alphap...@disc ussions.microso ft.com>
wrote:

<snip>
Hope you understand what I want to do.
Not really, I'm afraid. You're referring to C1 as a static *method*
but it's a *class*.

On a somewhat different note, your use of double-checked locking isn't
threadsafe. If you make _instance volatile it would be safe, but
generally I'd just use a static initializer instead. See
http://pobox.com/~skeet/csharp/singleton.html

Jon
Jul 25 '08 #2
Your reference to Java only served to confuse me further :-)

What (in .NET) is wrong with his double-lock approach?

Jul 25 '08 #3
On Jul 25, 1:50*pm, "Peter Morris" <mrpmorri...@SP AMgmail.comwrot e:
Your reference to Java only served to confuse me further :-)

What (in .NET) is wrong with his double-lock approach?
Without the "volatile" flag there's still the possibility that a
client will see a non-null reference before all of the writes have
been flushed, I believe. In other words, it could see a half-
constructed object.

Lock-free programming is always tricky, and almost never a good idea
for mere mortals like ourselves. I'll leave it to Joe Duffy, who knows
what he's talking about. (Even he's worried at the moment, as one
memory model isn't quite what he thought it was...)

Using a static initializer is simpler, safer, and still lazy (if you
include the static constructor).

Jon
Jul 25 '08 #4
On Fri, 25 Jul 2008 05:29:01 -0700, Alphapage
<Al*******@disc ussions.microso ft.comwrote:
Hello again,

My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my
particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...
Okay, I won't. :) Especially the first...I try very hard to never write
things like that even when I know them to be true, and there's nothing
here to suggest it is.

On the other two points, I guess it just remains to be seen. As Jon says,
I don't think this code example clears things up much, if at all. In
addition to his comment, I'll point out that I've never seen correct code
where the type parameter is constrained to inherit the type in which it's
being used.

It's possible that there's some esoteric use of generics that I'm
unfamiliar with where it comes up, but the few times I've played around
with that construct myself, I've gotten nowhere and coded myself into
logical impossibilities . If you're sure that that's the right tack to
take, you need to at least elaborate on _why_ you're declaring your
generic classes that way.

Pete
Jul 25 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1066
by: Jack Addington | last post by:
I have a tree view object that I am trying to load via database calls. I'm trying to make it generic so that I can extend and re-use it. One of the things I want to be able to do is to load the child nodes on demand based on the parent data (basically grouping sets of data objects by types and status for clearer navigation). When a user expands a node I want to call the appropriate method to get the data. I have create a TreeViewLogic...
2
2031
by: Jasper Kent | last post by:
I'm trying to do the equivalent of using typedefs with templates in C++ to avoid long instantiation names. I can do this okay: using BigDictionary = System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, string>>; class MyClass {
2
4695
by: Dave Thorens | last post by:
Hi. My VB6 application has several collections of custom classes, and each class has differing properties (ie. the classes are not necessarily related). I want to write a generic function which can be passed any collection in order to populate a control based on the objects in the collection. I figure I would have to pass the type of object into the function too in order to be able to cast each item of the collection, plus, say, a string array...
4
5329
by: Sameh Ahmed | last post by:
Hello there Is there a way to get the last control that was in focus before the current control? please note that I don't want the next or previous control in the tab order, I need the last one used by the user. Thanks in advance. Regards Sameh
3
2645
by: David Veeneman | last post by:
I'm writing a custom Find() function on a collection class derived from List<T>. The function will return an object of the type held in the collection. How do I specify the return type of the function? Thanks. -- David Veeneman Foresight Systems
5
3736
by: Nathan Sokalski | last post by:
I have a control that I want displayed in all items except the last one. I figured the best way to do this was to determine whether the current item was the last from within the ItemDataBound event using code such as the following: If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False but I cannot find a property that contains the total number of items before
7
38605
by: Andrus | last post by:
public class BusinessObjectGeneric<EntityType: BusinessObject where EntityType : BusinessEntity, new() { public BusinessObjectGeneric<EntityType() { } ..... causes error in constructor declaration:
6
2916
by: Andrus | last post by:
I need to create generic table field level cache. Table primary key (PrimaryKeyStructType) can be int, string or struct containing int and string fields. FieldName contains table field name to be cached. Remove() should remove table row from cache. Row is identified by table primary key. Trying compile class causes error Operator '==' cannot be applied to operands of type
15
2377
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a bit akin to ask: "the System.Web.UI and System.Windows.Controls namespace both contains a Control class, could I use one in place of the other? common they have the same name!" If you want to use a method common to both you should do as Alun...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.