473,320 Members | 2,080 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,320 software developers and data experts.

Possible to use generic type in one method for other class methods?

I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, TsourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be generic
and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/lib...pe(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve
Jan 14 '08 #1
5 1203
On Jan 14, 10:01 am, "Steve K." <no...@nowhere.comwrote:
I know this code is garbage but it should give you an idea of what I'm
after
<snip>

It's not entirely clear to me, to be honest. It looks like you're
trying to make it "half-generic" which doesn't tend to work terribly
well in my experience.

In particular, generics occur at *compile-time* as well as runtime.
With your example, how would you expect the compiler to validate
assignments based on the results of a call to DoSomething?

You might consider deriving a generic type from the non-generic one,
or vice versa.

Jon
Jan 14 '08 #2
Steve K. wrote:
I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, TsourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be generic
and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/lib...pe(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve

You can use Activator to create an instance of the _t type object, but
you need to return it as System.Object

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jan 14 '08 #3
Hi,
Why are you trying to do that?

What is wrong in doing the entire class generic.
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Steve K." <no***@nowhere.comwrote in message
news:uv**************@TK2MSFTNGP06.phx.gbl...
>I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, TsourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be
generic and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/lib...pe(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve

Jan 14 '08 #4
Hi All,

Apologies if my 2AM post wasn't clear or didn't make sense. I should learn
to not send questions at the tail end of a long work day ;) (On a side
note, have you heard about the phone service that will let you specify
certain numbers to make unavailable after a certain hour? The idea is to
prevent drunken calls to ex boyfriends/girfriends. Funny)

Anyway, the reason for my question can be understood here (2nd bullet under
"What I need to do":
http://pmddirect.com/sklett/PMDTextBoxReview.html

I'd like to avoid making my control class a generic and instead find some
way to pass in data via a generic method and get it back out on the same
type-safe manner.

I hope that clears up any confusion. Thanks for the replies and again I'm
sorry if the question didn't makse sense.

-Steve

"Steve K." <no***@nowhere.comwrote in message
news:uv**************@TK2MSFTNGP06.phx.gbl...
>I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, TsourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be
generic and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/lib...pe(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve

Jan 14 '08 #5

"Steve K." <no***@nowhere.comwrote in message
news:On**************@TK2MSFTNGP02.phx.gbl...
Hi All,

Apologies if my 2AM post wasn't clear or didn't make sense. I should
learn to not send questions at the tail end of a long work day ;) (On a
side note, have you heard about the phone service that will let you
specify certain numbers to make unavailable after a certain hour? The
idea is to prevent drunken calls to ex boyfriends/girfriends. Funny)

Anyway, the reason for my question can be understood here (2nd bullet
under "What I need to do":
http://pmddirect.com/sklett/PMDTextBoxReview.html

I'd like to avoid making my control class a generic and instead find some
way to pass in data via a generic method and get it back out on the same
type-safe manner.
Ah hah. The "right" way to solve your problem is to make the whole class
generic, but then you run into the lack of support for generics in the
designer environment. Not sure what can be done about that.
Jan 14 '08 #6

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

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.