Dereferencing

M

Michael Maes

Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
H

Herfried K. Wagner [MVP]

* "Michael Maes said:
How would I handle Dereferencing in vb.Net 2003.

Something like:

Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

Store (name, control) pairs in a 'Hashtable'.
 
C

CJ Taylor

Are you trying to create a textbox control from a string variable as the name? I don't quite understand what your trying to do, but would be curiuos to learn what dereferencing is.

-CJ
Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
M

Michael Maes

Thanks,

It's a 'workaround' but it will do the job.
Must I conclude that in VB this is not possible?

Michael

| * "Michael Maes" <[email protected]> scripsit:
| > How would I handle Dereferencing in vb.Net 2003.
| >
| > Something like:
| >
| > Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)
|
| Store (name, control) pairs in a 'Hashtable'.
|
| --
| Herfried K. Wagner [MVP]
| <http://www.mvps.org/dotnet>
 
H

Herfried K. Wagner [MVP]

* "Michael Maes said:
It's a 'workaround' but it will do the job.
Must I conclude that in VB this is not possible?

Yes, it's not possible "out of the box".
 
M

Michael Maes

Hi CJ,

Until a couple of years ago I was a Coldfusion developer (up to v4.5).
In CFML there was a function 'Evaluate(Expression)'.
Lets say you have a structure 'Language_Enu' and 'Language_Nl'
Both would have the same 'member variables', like eg 'SaveAndClose'

To retreive the value of 'SaveAndClose' you could then set eg a current_language_variable cLang (String) scoping the entire application. So you could fetch your var like:

Evaluate("Language_" & #current_language_variable# & ".SaveAndClose")

giving you the right value for the right language.

Since CFML (at that time) wasn't OOP, this only worked for Variables (I think -> long time ago!)

If I'm right, this type of operation is called Dereferencing.

Now I'd like to retreive properties of controls having practically the same name. Instead of writing numorous nearly identical lines of code, I thought I could use this principal.

If this is not possible in vb.Net, I will use Herfrieds' solution and create the Hashtable dynamically by scanning recursively through the forms-controlcollection.

Maybe you have got another idea / approach.

Kind regards,

Michael

Are you trying to create a textbox control from a string variable as the name? I don't quite understand what your trying to do, but would be curiuos to learn what dereferencing is.

-CJ
Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
M

Michael Maes

That's a pitty.
Maybe we'll see this functionality in Whidbey ?!?

Thanks Herfried


| * "Michael Maes" <[email protected]> scripsit:
| > It's a 'workaround' but it will do the job.
| > Must I conclude that in VB this is not possible?
|
| Yes, it's not possible "out of the box".
|
| --
| Herfried K. Wagner [MVP]
| <http://www.mvps.org/dotnet>
 
C

CJ Taylor

I gotcha... Same thing could be done in JavaScript with the eval statement I believe... I remember doing something like this a couple times, just didn't know it was called derefrencing. Thanks for the pointer though!

-CJ
Hi CJ,

Until a couple of years ago I was a Coldfusion developer (up to v4.5).
In CFML there was a function 'Evaluate(Expression)'.
Lets say you have a structure 'Language_Enu' and 'Language_Nl'
Both would have the same 'member variables', like eg 'SaveAndClose'

To retreive the value of 'SaveAndClose' you could then set eg a current_language_variable cLang (String) scoping the entire application. So you could fetch your var like:

Evaluate("Language_" & #current_language_variable# & ".SaveAndClose")

giving you the right value for the right language.

Since CFML (at that time) wasn't OOP, this only worked for Variables (I think -> long time ago!)

If I'm right, this type of operation is called Dereferencing.

Now I'd like to retreive properties of controls having practically the same name. Instead of writing numorous nearly identical lines of code, I thought I could use this principal.

If this is not possible in vb.Net, I will use Herfrieds' solution and create the Hashtable dynamically by scanning recursively through the forms-controlcollection.

Maybe you have got another idea / approach.

Kind regards,

Michael

Are you trying to create a textbox control from a string variable as the name? I don't quite understand what your trying to do, but would be curiuos to learn what dereferencing is.

-CJ
Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
M

Michael Maes

Yeah, ... we'll have it "Maybe in Whidbey" ?!?!?
I gotcha... Same thing could be done in JavaScript with the eval statement I believe... I remember doing something like this a couple times, just didn't know it was called derefrencing. Thanks for the pointer though!

-CJ
Hi CJ,

Until a couple of years ago I was a Coldfusion developer (up to v4.5).
In CFML there was a function 'Evaluate(Expression)'.
Lets say you have a structure 'Language_Enu' and 'Language_Nl'
Both would have the same 'member variables', like eg 'SaveAndClose'

To retreive the value of 'SaveAndClose' you could then set eg a current_language_variable cLang (String) scoping the entire application. So you could fetch your var like:

Evaluate("Language_" & #current_language_variable# & ".SaveAndClose")

giving you the right value for the right language.

Since CFML (at that time) wasn't OOP, this only worked for Variables (I think -> long time ago!)

If I'm right, this type of operation is called Dereferencing.

Now I'd like to retreive properties of controls having practically the same name. Instead of writing numorous nearly identical lines of code, I thought I could use this principal.

If this is not possible in vb.Net, I will use Herfrieds' solution and create the Hashtable dynamically by scanning recursively through the forms-controlcollection.

Maybe you have got another idea / approach.

Kind regards,

Michael

Are you trying to create a textbox control from a string variable as the name? I don't quite understand what your trying to do, but would be curiuos to learn what dereferencing is.

-CJ
Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
C

CJ Taylor

I believe that is the answer for every feature in .NET. I've looked at Whidbey... It's nice, sure... Has some new controls that are fun. But so far Whidbey, Longhorn, and Yukon are supposed to be the 3 greatest tools ever invented. =)

They said the same thing about XP, now is FX, my curiousity is, what will it be after FX, I can't even think of creative names to use anymore.

I think it should be called.. Herfried.


Yeah, ... we'll have it "Maybe in Whidbey" ?!?!?
I gotcha... Same thing could be done in JavaScript with the eval statement I believe... I remember doing something like this a couple times, just didn't know it was called derefrencing. Thanks for the pointer though!

-CJ
Hi CJ,

Until a couple of years ago I was a Coldfusion developer (up to v4.5).
In CFML there was a function 'Evaluate(Expression)'.
Lets say you have a structure 'Language_Enu' and 'Language_Nl'
Both would have the same 'member variables', like eg 'SaveAndClose'

To retreive the value of 'SaveAndClose' you could then set eg a current_language_variable cLang (String) scoping the entire application. So you could fetch your var like:

Evaluate("Language_" & #current_language_variable# & ".SaveAndClose")

giving you the right value for the right language.

Since CFML (at that time) wasn't OOP, this only worked for Variables (I think -> long time ago!)

If I'm right, this type of operation is called Dereferencing.

Now I'd like to retreive properties of controls having practically the same name. Instead of writing numorous nearly identical lines of code, I thought I could use this principal.

If this is not possible in vb.Net, I will use Herfrieds' solution and create the Hashtable dynamically by scanning recursively through the forms-controlcollection.

Maybe you have got another idea / approach.

Kind regards,

Michael

Are you trying to create a textbox control from a string variable as the name? I don't quite understand what your trying to do, but would be curiuos to learn what dereferencing is.

-CJ
Hi,

How would I handle Dereferencing in vb.Net 2003.

Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a textbox.
The "txt" + someStringVariable concatenation would represent a valid name of a textbox on the form.

Thanks for any ideas,

Michael
 
M

Michael Maes

do you mean the dutch word ;-)

|
|
| >I think it should be called.. Herfried.
|
| LOL
|
|
 
C

Cor

"> do you mean the dutch word ;-)

Yes that is a Dutch word, it means fun, or are you thinking on the almost
the same word with a U.

Certainly not, not for CJ and also not for Herfried greath guys,

:)

Cor
 
Z

Zanna

<q>
Something like:
Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)

This sadly won't work because a type of string cannot be casted to a
textbox.
The "txt" + someStringVariable concatenation would represent a valid name of
a textbox on the form.
</q>

You can do a loop on Me.Controls and searching for the Item.Name = "txt" +
someStringVariable.
Than you can cast the item found to TextBox.
 
C

Cor

Hi Michael,

Or maybe you are a Dutch speaker, because your name sounds very Belgian.

LOL means Laugh On Line but when you are a Dutch speaker, then you know that
is the same as the dutch lol.

Cor
 
M

Michael Maes

Yes Cor, indeed "very Belgian".
Sometimes I feel like a dinosaur (or an "old fart") in these High Tech Era:
I've been programming for years, but still I haven't got an ICQ, I don't use
(know) many of the acronyms (I that word is used in English) that many so
eloquently use, .... So now and then I "dare" to ask what it means.

CYA,

Michael

| Hi Michael,
|
| Or maybe you are a Dutch speaker, because your name sounds very Belgian.
|
| LOL means Laugh On Line but when you are a Dutch speaker, then you know
that
| is the same as the dutch lol.
|
| Cor
|
|
 
M

Michael Maes

That's an option too but, personaly, I prefer to use the Hashtable because
then you only have to loop the control-collection once for each
form-instance (filling the HT with all the controls).

Michael

|
| <q>
| Something like:
| Dim txt As TextBox = DirectCast("txt" & someStringVariable, TextBox)
|
| This sadly won't work because a type of string cannot be casted to a
| textbox.
| The "txt" + someStringVariable concatenation would represent a valid name
of
| a textbox on the form.
| </q>
|
| You can do a loop on Me.Controls and searching for the Item.Name = "txt" +
| someStringVariable.
| Than you can cast the item found to TextBox.
|
| --
| Math Parser : http://www.neodatatype.net
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top