Hello:
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?
Thanks,
Travis 15 1364
On Fri, 29 Aug 2008 11:28:39 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
Hello:
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?
Could you be more specific? Generally, you can just write "default(T)"
where T is the type parameter for your generic type and it will work.
Taking as an assumption that that's _not_ working for you, it would be
helpful if you could explain why. What are you doing that causes that
approach to fail? Some sample code would help.
Pete
<je**********@gmail.comwrote in message
news:f6**********************************@8g2000hs e.googlegroups.com...
Hello:
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?
Its a little ambiguous. If T is a type parameter then default(T) _is_ the
answer.
If T is simply a place marker for your unknown T then how is it unknown?
Do you want to create a default value for the type of an object referenced
by an object type?
A bit more code would help direct us?
--
Anthony Jones - MVP ASP/ASP.NET
I am doing this at runtime. If it was at compile time, I would just
use T directly.
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
else
{
return null;
} je**********@gmail.com wrote:
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?
default(T) is intended for just that ! http://msdn.microsoft.com/en-us/library/25tdedf5.aspx
<quote>
Generic code: Specifies the default value of the type parameter. This
will be null for reference types and zero for value types.
</quote>
If it is a reference type you may want to put a restriction on
T tht is must have a parameterless constructor and use new T() !
Arne
On Fri, 29 Aug 2008 11:51:01 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
For what it's worth, it's hard for me to understand your distinction
between "doing this at runtime" and "at compile time". In particular,
while the code is written before "compile time", none of the code executes
until "runtime".
From your elaboration, I gather that what you really mean is that you
don't actually have even a type parameter T to work with (again, some
actual sample code would have been helpful in expressing the question).
With that in mind...
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
else
{
return null;
}
I have no idea. We still don't really know what you're doing. Your
question was presented as if it was in the context of a generic type or
method, but now I'm not so sure.
The code you posted certainly _could_ do what you want. But it would box
the value type as returned. That's not exactly the same as what
"default(T)" would return. But if the code accomplishes your goal, that
seems to me to be the same as "would work". Right?
Pete
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
je**********@gmail.com wrote:
I would like to get default(T). However, I don't know what T is until
runtime; I just have a Type. Is there an equivilent means of getting
the results of default(T)?
default(T) is intended for just that !
http://msdn.microsoft.com/en-us/library/25tdedf5.aspx
<quote>
Generic code: Specifies the default value of the type parameter. This
will be null for reference types and zero for value types.
</quote>
If it is a reference type you may want to put a restriction on
T tht is must have a parameterless constructor and use new T() !
Why? The default for all reference types is null.
--
Anthony Jones - MVP ASP/ASP.NET
I have no idea. *We still don't really know what you're doing. *Your *
question was presented as if it was in the context of a generic type or *
method, but now I'm not so sure.
The code you posted certainly _could_ do what you want. *But it would box *
the value type as returned. *That's not exactly the same as what *
"default(T)" would return. *But if the code accomplishes your goal, that *
seems to me to be the same as "would work". *Right?
Pete
Is there a way to avoid the boxing?
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 29 Aug 2008 11:51:01 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
For what it's worth, it's hard for me to understand your distinction
between "doing this at runtime" and "at compile time". In particular,
while the code is written before "compile time", none of the code executes
until "runtime".
From your elaboration, I gather that what you really mean is that you
don't actually have even a type parameter T to work with (again, some
actual sample code would have been helpful in expressing the question).
With that in mind...
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
else
{
return null;
}
I have no idea. We still don't really know what you're doing. Your
question was presented as if it was in the context of a generic type or
method, but now I'm not so sure.
The code you posted certainly _could_ do what you want. But it would box
the value type as returned. That's not exactly the same as what
"default(T)" would return. But if the code accomplishes your goal, that
seems to me to be the same as "would work". Right?
If the function is a generic type then according to the posted code it has
to be returning object anyway.
I suspect the OP hasn't really looked into Generics.
--
Anthony Jones - MVP ASP/ASP.NET
On Aug 29, 1:13*pm, "Anthony Jones" <A...@yadayadayada.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 29 Aug 2008 11:51:01 -0700, jehugalea...@gmail.com
<jehugalea...@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
For what it's worth, it's hard for me to understand your distinction
between "doing this at runtime" and "at compile time". *In particular,
while the code is written before "compile time", none of the code executes
until "runtime".
*From your elaboration, I gather that what you really mean is that you
don't actually have even a type parameter T to work with (again, some
actual sample code would have been helpful in expressing the question).
With that in mind...
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
* * * * * * * * if (type.IsValueType)
* * * * * * * * {
* * * * * * * * * * return Activator.CreateInstance(type);
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * return null;
* * * * * * * * }
I have no idea. *We still don't really know what you're doing. *Your
question was presented as if it was in the context of a generic type or
method, but now I'm not so sure.
The code you posted certainly _could_ do what you want. *But it wouldbox
the value type as returned. *That's not exactly the same as what
"default(T)" would return. *But if the code accomplishes your goal, that
seems to me to be the same as "would work". *Right?
If the function is a generic type then according to the posted code it has
to be returning object anyway.
I suspect the OP hasn't really looked into Generics.
--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
- Show quoted text -
Unfortunately, I can't use generics at this low level. This code is
using reflection to move data between data object properties and a
DataRow columns. I don't know the type I am converting to, since all I
am given is a PropertyInfo. All I have to work with an
PropertyInfo.GetValue and PropertyInfo.PropertyType.
On Fri, 29 Aug 2008 12:11:56 -0700, je**********@gmail.com
<je**********@gmail.comwrote:
[...]
>The code you posted certainly _could_ do what you want. Â*But it would box Â* the value type as returned. Â*That's not exactly the same as what Â* "default(T)" would return. Â*But if the code accomplishes your goal, that Â* seems to me to be the same as "would work". Â*Right?
Is there a way to avoid the boxing?
Without knowing the actual type? No, not really. To unbox a value type,
you need a place to put it. The only way to get a place to put it is to
know the type at compile-time (either through the use of a generic, or
simply being explicitly typed).
Pete
Anthony Jones wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
>je**********@gmail.com wrote:
>>I would like to get default(T). However, I don't know what T is until runtime; I just have a Type. Is there an equivilent means of getting the results of default(T)?
default(T) is intended for just that !
http://msdn.microsoft.com/en-us/library/25tdedf5.aspx
<quote> Generic code: Specifies the default value of the type parameter. This will be null for reference types and zero for value types. </quote>
If it is a reference type you may want to put a restriction on T tht is must have a parameterless constructor and use new T() !
Why? The default for all reference types is null.
Exactly.
(and also what it is said in the text I quoted)
Which is why I think the original poster may be interested
in new T() instead of default(T) for reference types.
null is usually not so useful.
Arne
On Aug 29, 1:13*pm, "Anthony Jones" <A...@yadayadayada.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 29 Aug 2008 11:51:01 -0700, jehugalea...@gmail.com
<jehugalea...@gmail.comwrote:
I am doing this at runtime. If it was at compile time, I would just
use T directly.
For what it's worth, it's hard for me to understand your distinction
between "doing this at runtime" and "at compile time". *In particular,
while the code is written before "compile time", none of the code executes
until "runtime".
*From your elaboration, I gather that what you really mean is that you
don't actually have even a type parameter T to work with (again, some
actual sample code would have been helpful in expressing the question).
With that in mind...
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
* * * * * * * * if (type.IsValueType)
* * * * * * * * {
* * * * * * * * * * return Activator.CreateInstance(type);
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * return null;
* * * * * * * * }
I have no idea. *We still don't really know what you're doing. *Your
question was presented as if it was in the context of a generic type or
method, but now I'm not so sure.
The code you posted certainly _could_ do what you want. *But it wouldbox
the value type as returned. *That's not exactly the same as what
"default(T)" would return. *But if the code accomplishes your goal, that
seems to me to be the same as "would work". *Right?
If the function is a generic type then according to the posted code it has
to be returning object anyway.
I suspect the OP hasn't really looked into Generics.
--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
- Show quoted text -
Good point about returning an object. Boxing is inevitable.
On Aug 29, 11:04*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
I have an instance of Type. I would like to emulate the functionality
that default(T) provides. Would this work:
* * * * * * * * if (type.IsValueType)
* * * * * * * * {
* * * * * * * * * * return Activator.CreateInstance(type);
* * * * * * * * }
* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * return null;
* * * * * * * * }
I have no idea. *We still don't really know what you're doing. *Your *
question was presented as if it was in the context of a generic type or *
method, but now I'm not so sure.
It seems pretty clear to me. He is trying to write something like
this:
object GetDefaultValue(Type type);
in which case his code would indeed work.
On Sun, 31 Aug 2008 01:29:50 -0700, Pavel Minaev <in****@gmail.comwrote:
It seems pretty clear to me. He is trying to write something like
this:
object GetDefaultValue(Type type);
Sure. You have the benefit of having the whole thread to look at now. :p
(Not one of the three of us who replied to the original post figured out
the desired behavior from that original post...I think that's sufficient
evidence that the question was ambiguous at best; we can't have _all_
missed something obvious, even if I myself have been known to do so from
time to time).
in which case his code would indeed work.
Well, maybe. It certainly doesn't do the exact same thing that the
"default" operator would. In particular, he would get a boxed value type
back.
Pete
Peter Duniho wrote:
(Not one of the three of us who replied to the original post figured out
the desired behavior from that original post...I think that's sufficient
evidence that the question was ambiguous at best; we can't have _all_
missed something obvious, even if I myself have been known to do so from
time to time).
Well, I've figured it from the very first post, but it may help that
I'm _not_ a native English speaker :)
in which case his code would indeed work.
Well, maybe. It certainly doesn't do the exact same thing that the
"default" operator would. In particular, he would get a boxed value type
back.
For the method with the sig given, it's irrelevant, since it'd have to
return a boxed value anyway. It would be possible to make a generator
returning unboxed values using a generic method and a bit of
reflection, but since it would still be boxed to object when used, why
bother? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John L. Clark |
last post by:
I am curious as to the rationale, and effect, of having default
namespaces not applying (directly) to attributes (see
http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute
without a...
|
by: cppaddict |
last post by:
Hi,
I am writing a program and needs to know one of its object members
before it can be initialized. It doesn't really matter for my
question (which a C++ question, not a windows question), but...
|
by: shmartonak |
last post by:
For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?
If I'm using pointers to access array...
|
by: John Wood |
last post by:
Even though CSC does its best to detect use of unassigned variables, it
often misses it... for example if you just declare a double in a class
without assigning a default value, it has a default...
|
by: JustSomeGuy |
last post by:
Hi I have a function that takes a refrence to a std::list<myType> &
for ex:
myMethod(int a=0, int b=0, std::list<myType> & lst=NULL)
Now most of the time the lst is not passed and so I would...
|
by: Edward Diener |
last post by:
Given
value class X
{
public:
// Not allowed: X():i(100000),s(10000) { }
// Allowed
void InitializeDefaults() { i = 100000; s = 10000; }
private:
int i;
|
by: Zytan |
last post by:
I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the...
|
by: Jess |
last post by:
Hello,
I understand the default-initialization happens if we don't initialize
an object explicitly. I think for an object of a class type, the
value is determined by the constructor, and for...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |