From: Mark Hurd on
"Captain Jack" <CaptainJack1024(a)comcast.net> wrote in message
news:zJudnY9SeaukFQfWnZ2dnUVZ_gCdnZ2d(a)giganews.com...
> "Mark Hurd" <markhurd(a)ozemail.com.au> wrote in message
> news:%23Ofin8ewKHA.5132(a)TK2MSFTNGP05.phx.gbl...
>> "Captain Jack" <CaptainJack1024(a)comcast.net> wrote in message
>> news:_tKdnQt1w8KPlwXWnZ2dnUVZ_vidnZ2d(a)giganews.com...
>>> Kinda stumped on this one... I've got a generic class, defined like
>>> this:
>>>
>>> [ ... ]
>>
>> You need to know two things:
>>
>> 1. GetType can reference generic types by including Of with out a
>> type name.
>> E.g. GetType(GenClass(Of))
>>
>> 2. Type.MakeGenericType(T) is the runtime equivalent of (Of T).
>> E.g. GetType(GenClass(Of)).MakeGenericType(GetType(Integer)) Is
>> GetType(GenClass(Of Integer))
>> returns True.
>
> That was indeed what I was looking for, thank you. I may have painted
> myself into a bit of a corner, though, so I'm reviewing the whole
> thing. The main reason I'm using a generic class in this case is so I
> that I can have a class that doesn't have to go through all kinds of
> hoops to properly compare a property found at run time in another,
> unrelated object to a constant pre-loaded from the database, as well
> as nicely enforcing the correct type in each different element of my
> list. That does work, no problems, and the code above makes the
> process of instantiating the classes much more elegant (if I switch
> the numeric code in the database to a string that names the type, even
> better). By having the generic class descend from a non-generic class,
> it's easire to define the list object, which was a nice plus.
>
> Each of the generic classes has a property that returns a value (the
> property is brilliantly named "Value") of the type defined in the
> generic part. Since the property "Value" is defined in the generic
> class, of course, when I pull an item from the list as the BaseClass,
> "Value" isn't recognized. I could cast the item to type Object, of
> course, but that shoots elegance right in the gut and leaves it for
> dead by the side of the road. Shadowing a dummy property in the base
> class doesn't seem to get me anywhere either. I'm sure there's some
> way around this, ultimately, but I'm beginning to think I
> over-complicated this one in my neverending quest for "beautiful"
> code.
>
> --
> Jack

Yes, well your problem here is that you want to "generically" refer to a
"specified" property, Value.

So either, don't and redesign your solution, or redesign the caller to
also be generic.

E.g.

Sub CheckValue(Of T)(ByVal G As GenClass(Of T))
FailIf(G.Value Is Nothing)
etc.
You won't have many tests you can make on the generic Value, but you
could now specialise by type e.g
Select Case GetType(T)
Case GetType(String)
Dim StringValue = DirectCast(G.Value, String)
FailIf(StringValue.Length=0)
etc.

Type inference may mean your calls to CheckValue don't need the (Of
type) specified, or you'll have to use the MakeGenericMethod call at the
bottom of my last post.

--
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)

From: Captain Jack on
"Mark Hurd" <markhurd(a)ozemail.com.au> wrote in message
news:OwHC8OlwKHA.4552(a)TK2MSFTNGP04.phx.gbl...
>
> Yes, well your problem here is that you want to "generically" refer to a
> "specified" property, Value.
>
> So either, don't and redesign your solution, or redesign the caller to
> also be generic.

Yeah, I'm pretty sure that my problem was in jumping in feet first without
thinking through the scope of the problem.

Ultimately, the issue is that each element with the Generic definition needs
to perform a test using data of whatever type it's declared with. However,
that value doesn't really need to be looked at directly; I realized over the
weekend that what I really need is the test results (all of which can, in
the end, be expressed as Boolean). The test results will, in turn be used to
index a single value that will either be zero or empty if the group of tests
works out to False, and otherwise be a specific value if True. I can still
make use of the (really helpful) code for instantiating the generic object,
because I can hide the test code and the value being tested inside the code.

I re-tooled it a bit to make a single top level object that contains the
collection of generic classes, and gave it a separate generic parameter for
it's value type. The calling program will always know what *that* value's
type should be, and it all still works out.

I fell victim to the mistake of trying to simplify my problem and asked a
for a solution that solved the simplification (which is what I got) but I
still didn't meet my goal, because I didn't ask for help with the whole
thing. I preach this all the time to users when they're asking for
something; I should learn to follow my own advice. :-)

Thanks again for all the help. I think this one is licked, and I'll try to
think the next one through a little more.

--
Jack