473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to always have specific property?

I'd like to design a class that is runtime typed (generic) and the
types will always have a specific member. For example:

public T MyClass
{
T temp = default(T);

Void Fill
{
T.PopulateMe();
}
}

What ever type I send in always has a method named PopulateMe(). I
imagine the types going into the class will need to implement an
interface with the one member. How do I then tell the generic class
(MyClass) that the member is always available since the parameter is
only T?

Thanks,
Brett

Jun 5 '06 #1
10 1146
"Brett Romero" <ac*****@cygen.com> wrote:
I'd like to design a class that is runtime typed (generic) and the
types will always have a specific member. For example:

public T MyClass
{
T temp = default(T);

Void Fill
{
T.PopulateMe();
}
}

What ever type I send in always has a method named PopulateMe(). I
imagine the types going into the class will need to implement an
interface with the one member. How do I then tell the generic class
(MyClass) that the member is always available since the parameter is
only T?


---8<---
public interface IMyInterface
{
void PopulateMe();
}

public class MyClass<T>
where T : IMyInterface
{
T temp = default(T);

void Fill()
{
temp.PopulateMe();
}
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Jun 6 '06 #2
Thanks Berry.

Let's say I want to use MyClass<> to create Person, Employee, and
Animal types.
Basically, MyClass <>is a container of several different objects.
However, each different instance is unaware that it is sharing
MyClass<> with other types.

I'd like to use the singleton pattern in MyClass<> so each of the three
types are created only once. If a type already exist, I get a
reference back. This is easy enough if there are concrete classes for
each of the three types. However, because I'm doing this via
MyClass<>, it needs to know if I have already created an instance of
MyClass<Person> for example and not create another instance. Instead
of coding a container class for each of the types, I'm using MyClass<>
as a template.

What is the approach here? I could first check if MyClass<> has been
instantiated at all. Each time some one instantiates it with a
different type, I store the type in a private List<>. I check the
List<> to see if the type exist (Person for example). If so, I return
a reference. If Person type has not been instantiated (not in the
List<>), I return a new instance. Any suggestions on a roadmap?
Thanks,
Brett

Jun 6 '06 #3
"Brett Romero" <ac*****@cygen.com> ha scritto nel messaggio
news:11**********************@f6g2000cwb.googlegro ups.com...
Let's say I want to use MyClass<> to create Person, Employee, and
Animal types.
Basically, MyClass <>is a container of several different objects.
However, each different instance is unaware that it is sharing
MyClass<> with other types.


I think generics are not designed for this.
The right approach should be the simple inheritance:

class Animal
{}

class Dog : Animal
{}

class Person : Animal
{}

class Employee : Person
{}

class MyClass
{
public Animal SomeProperty
{}

public void SomeMethod(Animal animal)
{}

public Person GetPerson()
{}

public Employee GetEmployee()
{}

public Dog GetDog()
{}
}
Generics are designed to manage different types in the same way without
knowing about them, into a class of homogenous type.
You need to manage different types into the same class, and you know (you
need to know it if you store different types into the same class) what types
them are.


Jun 6 '06 #4
Brett Romero <ac*****@cygen.com> wrote:
Let's say I want to use MyClass<> to create Person, Employee, and
Animal types.
Basically, MyClass <>is a container of several different objects.
However, each different instance is unaware that it is sharing
MyClass<> with other types.

I'd like to use the singleton pattern in MyClass<> so each of the three
types are created only once. If a type already exist, I get a
reference back. This is easy enough if there are concrete classes for
each of the three types. However, because I'm doing this via
MyClass<>, it needs to know if I have already created an instance of
MyClass<Person> for example and not create another instance. Instead
of coding a container class for each of the types, I'm using MyClass<>
as a template.

What is the approach here? I could first check if MyClass<> has been
instantiated at all. Each time some one instantiates it with a
different type, I store the type in a private List<>. I check the
List<> to see if the type exist (Person for example). If so, I return
a reference. If Person type has not been instantiated (not in the
List<>), I return a new instance. Any suggestions on a roadmap?


You can use the singleton pattern with generics with no problem. For
instance:

using System;
using System.Text;

class Singleton<T> where T : class, new()
{
static T instance;
static object padlock = new object();

public static T Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
Console.WriteLine
("Creating instance of {0}", typeof(T));
instance=new T();
}
return instance;
}
}
}

}

class Test
{
static void Main()
{
StringBuilder x = Singleton<StringBuilder>.Instance;
object o = Singleton<object>.Instance;

Console.WriteLine (o == Singleton<object>.Instance);
}
}

Now, you can use the normal patterns of singletons in here - static
constructors/initializers etc. The only reason I've got constrains of
new() and class for T are to make it easier to demonstrate. You couild
easily use your interface as a constraint instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 8 '06 #5
> You can use the singleton pattern with generics with no problem. For
instance:

using System;
using System.Text;

class Singleton<T> where T : class, new()
{
static T instance;
static object padlock = new object();

public static T Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
Console.WriteLine
("Creating instance of {0}", typeof(T));
instance=new T();
}
return instance;
}
}
}

}

I'm doing something along those lines. My goal is to create a template
collection that can hold any type that implements a certain interface.
The way it works now, each time I give the collection a different
type, that creates a new singleton for that type. If I keep calling
properties on the collection for that type, I always reference the
singleton. It's working just the way I want it to.

public class MetaCollection<TValue> : where TValue : IMyCollection,
new()
{
private static MyCollection<TValue> _instance;

public static MyCollection<TValue> Instance
{
get
{
if( _instance == null )
_instance = new MyCollection<TValue>( );
}
}
}

But I keep getting a new instance back when I do:

MyCollection<someclass> SomeClassColl =
MyCollection<someclass>.Instance;

rather than getting the singleton. If I access a property on the
collection:

MyCollection<someclass>.Instance.Fill();
MyCollection<someclass>.Instance.GiveMeFirstItem() ;

for example, the singleton works fine. For now, I just use the entire
syntax. All I'm trying to do is provide a shortcut to the Instance
syntax. Do you know how I can get the shortcut to work using syntax
similar to the above?

I have actually wrapped the Instance check code inside of the above
methods, which is shorter syntax since I don't have to do ".Instance".
Still, I want to understand why the first line mentioned doesn't give
back a singleton.

Thanks,
Brett

Jun 8 '06 #6
Brett Romero <ac*****@cygen.com> wrote:

<snip>
But I keep getting a new instance back when I do:

MyCollection<someclass> SomeClassColl =
MyCollection<someclass>.Instance;

rather than getting the singleton. If I access a property on the
collection:

MyCollection<someclass>.Instance.Fill();
MyCollection<someclass>.Instance.GiveMeFirstItem() ;

for example, the singleton works fine. For now, I just use the entire
syntax. All I'm trying to do is provide a shortcut to the Instance
syntax. Do you know how I can get the shortcut to work using syntax
similar to the above?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 9 '06 #7
Here it is:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace TestConsole
{
class TestCollection
{
public static void Main(string[]args)
{
Container < Person > p = Container < Person > .Instance;
}
}

public class Container < TValue > : Dictionary < int, TValue > where
TValue:
new()
{
private static Container < TValue > _instance;
private static Dictionary < string, TValue > _indexString = new
Dictionary
< string, TValue > ();

Container(){}

public static Container < TValue > Instance
{
get
{
if (_instance == null)
{
_instance = new Container < TValue > ();
}
return _instance;
}
}

public static bool IndexString(string pIndex, out TValue value)
{
if (_instance == null)
{
_instance = new Container < TValue > ();
}
return _indexString.TryGetValue(pIndex, out value);
}
}

public class Person
{
public Person(){}

public String Hand = "My Hand";
}
}
You'll noticed I can't do

p.IndexString(...)

p is an instance. All I want is a short to the longer syntax.
Everything is working fine except for the one thing. At this point,
I'd like to understand why it keeps giving back an instance rather than
a static reference. But when I do

Container < Person >.IndexString(...)

that is fine. It works on the singleton. What's the difference?

Note: I'm aware the dictionary is empty. This is just demonstrating
static access to a property.

Thanks,
Brett

Jun 9 '06 #8
Brett Romero <ac*****@cygen.com> wrote:

<snip>
p is an instance. All I want is a short to the longer syntax.
Everything is working fine except for the one thing. At this point,
I'd like to understand why it keeps giving back an instance rather than
a static reference.


It's not at all clear what you mean - which is why I asked for a short
but complete program which demonstrates the problem. You've given a
program with a Main method, which is good, but the Main method doesn't
actually *do* anything. Note that if you change Main to:

Container < Person > p = Container < Person > .Instance;
Container < Person > p2 = Container < Person > .Instance;
Console.WriteLine (p==p2);

it prints "True" as expected.

What does it do which you don't expect?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 10 '06 #9
Container < Person > p = Container < Person > .Instance;
I can go along referencing the singleton this way:

Container < Person > .Instance

which is fine. For simplicity and to avoid so much typing, I'd like to
reference it this way:

Container < Person > p = Container < Person > .Instance
From now on, I reference "p" to access anything on the singleton:


p.IndexString(...)

p is now my shortcut syntax. But notice IndexString(...) is not
available on p. It's only available on the full syntax, which is:

Container < Person > .IndexString(...)

p is an instance and doesn't have the static property IndexString(...)
available to it. I'm only compressing the full syntax into a variable
that I'd like to be the singelton. Instead, it keeps coming back with
the instance.

Which part are you not sure on?

Thanks,
Brett

Jun 10 '06 #10
Brett Romero <ac*****@cygen.com> wrote:
Container < Person > p = Container < Person > .Instance;


I can go along referencing the singleton this way:

Container < Person > .Instance

which is fine. For simplicity and to avoid so much typing, I'd like to
reference it this way:

Container < Person > p = Container < Person > .Instance


And that's fine.
From now on, I reference "p" to access anything on the singleton:


p.IndexString(...)

p is now my shortcut syntax. But notice IndexString(...) is not
available on p. It's only available on the full syntax, which is:

Container < Person > .IndexString(...)


That's because it's a static method. Static methods are never available
through instances, because it can easily cause confusion. (Consider
calling Thread.Sleep "on" another thread - it looks like you're telling
the other thread to sleep, when in fact Thread.Sleep only ever makes
the current thread sleep.)

You need to decide whether you want it to be a static method which
automatically uses the singleton, or an instance method which should be
called *on* the singleton.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 10 '06 #11

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

Similar topics

1
by: Stefan W via .NET 247 | last post by:
I'm relatively new to C#, (I have to use it, now that I'veinherited someone else's projects). I'm looking for a way toiterate through the controls on a form; if the control is of acertain type, I...
17
by: orekinbck | last post by:
Hi There Say I want to check if object1.Property1 is equal to a value, but object1 could be null. At the moment I have code like this: if (object1 != null) { if (object1.Property ==...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
6
by: Kirk | last post by:
I have several routines that use the Try-Catch-EndTry method. When an error occurs in one of these routines, I display it like this: Catch ex As Exception MsgBox("Error #" & Err.Number & ": " &...
0
by: peter.bittner | last post by:
I have developed a Windows application in Visual Studio .NET 2003 and created a Setup project for it. In the File System Editor I have added a shortcut to the User's Desktop folder to point to the...
4
by: Jason Richmeier | last post by:
I am sure this has been asked at least once before but I could not find anything when searching. If I set the value of the ExitCode property to 1066 for a windows service, the text "A service...
1
by: Eran.Yasso | last post by:
Hello all, This is my first time using listview. I was looking in google for a way to write to a specific column. I have 3 columns in my list view. I want to be able to write to a specific...
2
by: Max2006 | last post by:
Hi, asp:TreeView question: Is there any way to specify NodeIndent property for a specific level or node? So far I can define NodeIndent for all levels. Thank you, Max
5
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have a custom control (not a listbox or other hierarchical or list control) that I would like to bind to a specific row and column from a DataTable. Each row of the datatable will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.