I have a basic question thats been niggling me, but I never found time to
look at it before.
If I have an enumeration such as this
Fiend Enum TravelDirection
North
South
East
West
End Enum
and
Direction = TravelDirection
Now I want to set the Direction Randomly to one of the TravelDirection enum
values
If I know that my enum is between 1 and 4 I can easily get a new random
number and assign it, however, As I add to the enumeration, I wont want to
worry about coding all the changes, so how could I easily do this I know I
can do it, and I have but Im looking for the most simple solution. There
doesent seem to be a Max property for enumerations.
If this sounds dumb, then I appologise
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
-- 38 2257
Hi Terry,
Is there a reason that it seems that you do it for me difficult? I think I
miss something.
Two times the last position of environment tickcount + 5 divided by 5 should
in my opinion do the trick.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> I have a basic question thats been niggling me, but I never found time to look at it before.
If I have an enumeration such as this
Fiend Enum TravelDirection North South East West End Enum
and
Direction = TravelDirection
Now I want to set the Direction Randomly to one of the TravelDirection enum values
If I know that my enum is between 1 and 4 I can easily get a new random number and assign it, however, As I add to the enumeration, I wont want to worry about coding all the changes, so how could I easily do this I know I can do it, and I have but Im looking for the most simple solution. There doesent seem to be a Max property for enumerations.
If this sounds dumb, then I appologise -- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> schrieb: If I have an enumeration such as this
Fiend Enum TravelDirection North South East West End Enum
and
Direction = TravelDirection
Now I want to set the Direction Randomly to one of the TravelDirection enum values
If I know that my enum is between 1 and 4 I can easily get a new random number and assign it, however, As I add to the enumeration, I wont want to worry about coding all the changes, so how could I easily do this I know I can do it, and I have but Im looking for the most simple solution.
\\\
Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles))
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
///
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote I have a basic question thats been niggling me, but I never found time to look at it before.
If I have an enumeration such as this
Fiend Enum TravelDirection North South East West End Enum
and
Direction = TravelDirection
That should be:
Direction = TravelDirection.East
Or some other specific value. You wouldn't be able to assign the
entire enum to a variable.
Now I want to set the Direction Randomly to one of the TravelDirection enum values
If I know that my enum is between 1 and 4 I can easily get a new random number and assign it, however, As I add to the enumeration, I wont want to worry about coding all the changes, so how could I easily do this I know I can do it, and I have but Im looking for the most simple solution. There doesent seem to be a Max property for enumerations.
Why not use one of the values?
East West
MaxDirection = West End Enum
LFS
Sorry, I think I F*c(ed up here.
What I meant was how to set the variable to a Random number somwhere in the
range of the enumeration.
So Direction = ( Some Random Number In the Range )
I dont want to explicitly set it like this
Direction = RandomNumber( 0, 10)
I was looking for something more like
Direction = RandomNumber.next( 0, MyEnumeration.Max )
See what I mean, I mat have missed something here
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl... "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote I have a basic question thats been niggling me, but I never found time to look at it before.
If I have an enumeration such as this
Fiend Enum TravelDirection North South East West End Enum
and
Direction = TravelDirection
That should be:
Direction = TravelDirection.East
Or some other specific value. You wouldn't be able to assign the entire enum to a variable.
Now I want to set the Direction Randomly to one of the TravelDirection enum values
If I know that my enum is between 1 and 4 I can easily get a new random number and assign it, however, As I add to the enumeration, I wont want to worry about coding all the changes, so how could I easily do this I know I can do it, and I have but Im looking for the most simple solution. There doesent seem to be a Max property for enumerations.
Why not use one of the values?
East West MaxDirection = West End Enum
LFS
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
The maxValue of Next needs to be one greater than the last value desired:
Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles())
Dim r As New Random
MsgBox(Values(r.Next(0, Values.Length)).ToString())
LFS
"Larry Serflaten" <se*******@usinternet.com> schrieb: Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
The maxValue of Next needs to be one greater than the last value desired:
Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
"Mr Newbie" <NO********@THISADDRESS.COM> wrote What I meant was how to set the variable to a Random number somwhere in the range of the enumeration.
So Direction = ( Some Random Number In the Range )
As Herfried shows, the best solution is to add the members to an array, and
pick some random value out of the array. It may be that the values in the Enum
are NOT sequential, so that you can't just pick some number from 0 to some
max value. With the actual Enum values in an array, you can be sure picking
one element of the array will give you some value that is actually in the Enum.
Make sense?
BTW, another route would be something like this:
Dim pick As Integer() = New Integer() {TravelDirection.North, TravelDirection.South, _
TravelDirection.East, TravelDirection.West}
test = pick(Rnd.Next(0, 4))
;-)
LFS
Thanks 2 both, i've used something similar in the past but its just a shame
this was not built in to an enum class.
Cheers
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uT*************@TK2MSFTNGP11.phx.gbl... "Larry Serflaten" <se*******@usinternet.com> schrieb: Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
The maxValue of Next needs to be one greater than the last value desired:
Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
Cheers Cor
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl... Hi Terry,
Is there a reason that it seems that you do it for me difficult? I think I miss something.
Two times the last position of environment tickcount + 5 divided by 5 should in my opinion do the trick.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com>
I have a basic question thats been niggling me, but I never found time to look at it before.
If I have an enumeration such as this
Fiend Enum TravelDirection North South East West End Enum
and
Direction = TravelDirection
Now I want to set the Direction Randomly to one of the TravelDirection enum values
If I know that my enum is between 1 and 4 I can easily get a new random number and assign it, however, As I add to the enumeration, I wont want to worry about coding all the changes, so how could I easily do this I know I can do it, and I have but Im looking for the most simple solution. There doesent seem to be a Max property for enumerations.
If this sounds dumb, then I appologise -- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
Very true, I didnt think of that, yes they may not be sequentional good
point. However, it would however, be nice if this was built into the enum
class.
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... "Mr Newbie" <NO********@THISADDRESS.COM> wrote
What I meant was how to set the variable to a Random number somwhere in the range of the enumeration.
So Direction = ( Some Random Number In the Range )
As Herfried shows, the best solution is to add the members to an array, and pick some random value out of the array. It may be that the values in the Enum are NOT sequential, so that you can't just pick some number from 0 to some max value. With the actual Enum values in an array, you can be sure picking one element of the array will give you some value that is actually in the Enum.
Make sense?
BTW, another route would be something like this:
Dim pick As Integer() = New Integer() {TravelDirection.North, TravelDirection.South, _ TravelDirection.East, TravelDirection.West}
test = pick(Rnd.Next(0, 4))
;-) LFS
OHM,
??
You want a method on the Enum class that randomly returns one of the Enum's
values?
IMHO Such a function would suggest that the Enum class is trying to do too
much!
I would use code similar to Herfried's in a context appropriate class,
probably the class that was consuming the random enum value.
Hope this helps
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:uM**************@TK2MSFTNGP15.phx.gbl... Thanks 2 both, i've used something similar in the past but its just a shame this was not built in to an enum class.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:uT*************@TK2MSFTNGP11.phx.gbl... "Larry Serflaten" <se*******@usinternet.com> schrieb: Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
The maxValue of Next needs to be one greater than the last value desired:
Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
OK, why do you think its too much to ask of an Enum Class to be able to
report the number of elements it contains ?
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uM**************@TK2MSFTNGP15.phx.gbl... Thanks 2 both, i've used something similar in the past but its just a shame this was not built in to an enum class.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:uT*************@TK2MSFTNGP11.phx.gbl... "Larry Serflaten" <se*******@usinternet.com> schrieb: > Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) > Dim r As New Random > MsgBox(Values(r.Next(0, Values.Length - 1)).ToString())
The maxValue of Next needs to be one greater than the last value desired:
Dim Values() As AnchorStyles = CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) Dim r As New Random MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
Terry,
I hope you do not mind that I answer,
In my opinion is an Enum the best used when it has the values
1
2
4
8
Than you can do an OR or whatever with it, and has it no sence to know the
max value (in this case 15) or F
However just my opinion.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uM**************@TK2MSFTNGP15.phx.gbl... Thanks 2 both, i've used something similar in the past but its just a shame this was not built in to an enum class.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:uT*************@TK2MSFTNGP11.phx.gbl... "Larry Serflaten" <se*******@usinternet.com> schrieb: >> Dim Values() As AnchorStyles = >> [Enum].GetValues(GetType(AnchorStyles)) >> Dim r As New Random >> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) > > The maxValue of Next needs to be one greater than the last value > desired: > > Dim Values() As AnchorStyles = > CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) > Dim r As New Random > MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
Hi Cor,
Im not sure if you understood my origional post. What I wanted
was to be able to choose randomly any element from the Enum members, in
order to do this you need to know the number of elements.
Herfried's code will do this quite well.
I went on to say that It would be nice if the Enum class would be able to
choose one element at Random and Jay said he thought this was to much and it
probably is, however, it would be nice if the Enum could report the number
of elements it has to simplify choosing one from its return element values
array.
Unfortunately you dont seem to be able to inherit an Enum or I would have
wrapped it myself.
Cheers
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl... Terry,
I hope you do not mind that I answer,
In my opinion is an Enum the best used when it has the values 1 2 4 8 Than you can do an OR or whatever with it, and has it no sence to know the max value (in this case 15) or F
However just my opinion.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com>
OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uM**************@TK2MSFTNGP15.phx.gbl... Thanks 2 both, i've used something similar in the past but its just a shame this was not built in to an enum class.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:uT*************@TK2MSFTNGP11.phx.gbl... > "Larry Serflaten" <se*******@usinternet.com> schrieb: >>> Dim Values() As AnchorStyles = >>> [Enum].GetValues(GetType(AnchorStyles)) >>> Dim r As New Random >>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >> >> The maxValue of Next needs to be one greater than the last value >> desired: >> >> Dim Values() As AnchorStyles = >> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >> Dim r As New Random >> MsgBox(Values(r.Next(0, Values.Length)).ToString()) > > ACK, I should have read the documentation... ;-). > > -- > Herfried K. Wagner [MVP] > <URL:http://dotnet.mvps.org/>
Terry,
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> schrieb: I went on to say that It would be nice if the Enum class would be able to choose one element at Random and Jay said he thought this was to much and it probably is
In this particular case, I agree with Jay. Picking a random enumeration
constant doesn't have anything to do with enums and thus should not be
added.
however, it would be nice if the Enum could report the number of elements it has
I agree up to this point that this would be a nice addition to enumerations.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote it would be nice if the Enum could report the number of elements it has to simplify choosing one from its return element values array.
But Herfried has already shown how to do that. When you put the elements in
an array, the array itself will tell you how many there are. Or, you can get the
value from the Enum class:
Dim cnt As Integer = [Enum].GetValues(GetType(MyEnum)).Length
Debug.WriteLine(cnt)
I don't see how they could add anything else. What if you wanted to have
a member called Length? If it was used as a shared member of your enum,
you couldn't use it:
cnt = MyEnum.Length ' ???
Do you see how they really shouldn't add anything to your enum except what
you list as members of the enum?
LFS
OHM, OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
That's not what I stated! :-|
I think having the Enum Class report the number of elements an enum contains
might be a good thing. Although you can easily get the number from
Enum.GetNames or Enum.GetValues. I say "might be a good thing", as it could
be too easy to introduce problems as Enum values are not required to be
contiguous, nor start with zero.
What I said would be a bad thing, and I attempted to be very specific, is
for System.Enum to have a function that would return a random value (ala
Herfried's code). Yes, your code needs it, however is Returning a random
value really a realistic behavior that all Enums should behave? I don't see
that it is, hence I don't think Enum should possess that ability.
Now I do agree it would be nice to add our own functions to specific enums,
such as your TravelDirection enum might have the ability to return a random
value. Unfortunately the designers at MS decided not to allow that.
Hope this helps
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl... OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
<<snip>>
Larry,
I could see a Enum.GetLength function: Dim cnt As Integer = [Enum].GetLength(GetType(MyEnum))
That would not need to actually create & return the array of values.
However!!! This could cause problems, as Enums are not required to start
with zero nor are they required to be contiguous.
In other words, code such as:
Public Enum MyEnum
None = 0
Value1 = 55
Value2 = 65
Value3 = 75
End Enum
'For value As MyEnum = 0 to [Enum].GetLength(GetType(MyEnum))
For value As MyEnum = 0 to [Enum].GetValues(GetType(MyEnum)).Length
Debug.WriteLine(value, value.ToString())
Next
Would cause all sorts of mischief.
Where as this one does not:
For Each value As MyEnum In [Enum].GetValues(GetType(MyEnum))
Debug.WriteLine(value, value.ToString())
Next
Hope this helps
Jay
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl... "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote it would be nice if the Enum could report the number of elements it has to simplify choosing one from its return element values array.
But Herfried has already shown how to do that. When you put the elements in an array, the array itself will tell you how many there are. Or, you can get the value from the Enum class:
Dim cnt As Integer = [Enum].GetValues(GetType(MyEnum)).Length Debug.WriteLine(cnt)
I don't see how they could add anything else. What if you wanted to have a member called Length? If it was used as a shared member of your enum, you couldn't use it:
cnt = MyEnum.Length ' ???
Do you see how they really shouldn't add anything to your enum except what you list as members of the enum?
LFS
For one, enum per CLI specification cannot have any
methods\properties\events of its own. It can't even have any instance fields
except one, and thats used for defining the underlying type of the enum. So
forget about extending it.
Could it add a shared method Count that excepts an enum type and returns the
number of fields? Maybe, but I'm not sure it would be a good design
decision.
Lets say it has the shared Count\Length method, Count\Length denotes
arrays\collections\containers, and enum is not any of these. Matter of fact
an enum does not contain anything except the one instance variable as
perviously mentioned.
Could it represent the size of the enum? No because an enum defines named
literals and thats it.
Could it represent the number of valid values of the enum? Not really
because I can assign any value to an enum field whether its defined by the
enum type or not. Ex.
Public Enum Fruit
Apple
Orange
End Enum
Dim F As Fruit
F = Ctype(10,Fruit)
What it actually represents is the number of named literals defined by the
enum type. If this is true then what good is this value when used against
the enum itself? What does it tell you about the enum and how is it useful?
I can't really think of anything useful off the top of my head.
Where it does become useful is when you ask for the name or value arrays,
and arrays already of the property length, and it properly describes the
array not the enum
Anyways thinking out loud and just my two cents
JD
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl... OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do
too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uM**************@TK2MSFTNGP15.phx.gbl... Thanks 2 both, i've used something similar in the past but its just a shame this was not built in to an enum class.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:uT*************@TK2MSFTNGP11.phx.gbl... "Larry Serflaten" <se*******@usinternet.com> schrieb: >> Dim Values() As AnchorStyles =
[Enum].GetValues(GetType(AnchorStyles))>> Dim r As New Random >> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) > > The maxValue of Next needs to be one greater than the last value > desired: > > Dim Values() As AnchorStyles = > CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) > Dim r As New Random > MsgBox(Values(r.Next(0, Values.Length)).ToString())
ACK, I should have read the documentation... ;-).
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
Hi Terry,
As I said in my first post I do not know what you want to do, an Enum is in
my opinion to get a value using a keyword in a program. Something as
Direction = TravelDirection.North OR TravelDirection.West
(What would give as the Enum values where 1 2 4 8 using the normal situation
give 3 and than I could use that to go NorthWest in my screen)
However when I want to know what my randomnumber 4 would be I would just use
a hashtable, where it would give a value "East" (accoording to your table).
So I am curious in what situation you want to use this.
Cor
When I would get a value
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> schreef in
bericht news:%2****************@TK2MSFTNGP12.phx.gbl... Hi Cor, Im not sure if you understood my origional post. What I wanted was to be able to choose randomly any element from the Enum members, in order to do this you need to know the number of elements.
Herfried's code will do this quite well.
I went on to say that It would be nice if the Enum class would be able to choose one element at Random and Jay said he thought this was to much and it probably is, however, it would be nice if the Enum could report the number of elements it has to simplify choosing one from its return element values array.
Unfortunately you dont seem to be able to inherit an Enum or I would have wrapped it myself.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Cor Ligthert" <no************@planet.nl> wrote in message news:OB**************@TK2MSFTNGP12.phx.gbl... Terry,
I hope you do not mind that I answer,
In my opinion is an Enum the best used when it has the values 1 2 4 8 Than you can do an OR or whatever with it, and has it no sence to know the max value (in this case 15) or F
However just my opinion.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com>
OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uM**************@TK2MSFTNGP15.phx.gbl... > Thanks 2 both, i've used something similar in the past but its just a > shame this was not built in to an enum class. > > Cheers > > -- > OHM ( Terry Burns ) * Use the following to email me * > > Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() > For i As Int32 = 0 To ch.Length - 1 > ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) > Next > Process.Start("mailto:" & New String(ch)) > -- > > > "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message > news:uT*************@TK2MSFTNGP11.phx.gbl... >> "Larry Serflaten" <se*******@usinternet.com> schrieb: >>>> Dim Values() As AnchorStyles = >>>> [Enum].GetValues(GetType(AnchorStyles)) >>>> Dim r As New Random >>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >>> >>> The maxValue of Next needs to be one greater than the last value >>> desired: >>> >>> Dim Values() As AnchorStyles = >>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >>> Dim r As New Random >>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >> >> ACK, I should have read the documentation... ;-). >> >> -- >> Herfried K. Wagner [MVP] >> <URL:http://dotnet.mvps.org/> > >
Yes I understand that. However, it would be an easy thing to have
incorporated into the Class and would have made coding just slightly
simpler. However, this is fairly academic I know.
Regards
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl... "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote it would be nice if the Enum could report the number of elements it has to simplify choosing one from its return element values array.
But Herfried has already shown how to do that. When you put the elements in an array, the array itself will tell you how many there are. Or, you can get the value from the Enum class:
Dim cnt As Integer = [Enum].GetValues(GetType(MyEnum)).Length Debug.WriteLine(cnt)
I don't see how they could add anything else. What if you wanted to have a member called Length? If it was used as a shared member of your enum, you couldn't use it:
cnt = MyEnum.Length ' ???
Do you see how they really shouldn't add anything to your enum except what you list as members of the enum?
LFS
OOPS, Ive upset Jay
Sorry
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl... OHM, OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
That's not what I stated! :-|
I think having the Enum Class report the number of elements an enum contains might be a good thing. Although you can easily get the number from Enum.GetNames or Enum.GetValues. I say "might be a good thing", as it could be too easy to introduce problems as Enum values are not required to be contiguous, nor start with zero.
What I said would be a bad thing, and I attempted to be very specific, is for System.Enum to have a function that would return a random value (ala Herfried's code). Yes, your code needs it, however is Returning a random value really a realistic behavior that all Enums should behave? I don't see that it is, hence I don't think Enum should possess that ability.
Now I do agree it would be nice to add our own functions to specific enums, such as your TravelDirection enum might have the ability to return a random value. Unfortunately the designers at MS decided not to allow that.
Hope this helps Jay "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:O8**************@TK2MSFTNGP11.phx.gbl... OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... OHM, ??
You want a method on the Enum class that randomly returns one of the Enum's values?
IMHO Such a function would suggest that the Enum class is trying to do too much!
I would use code similar to Herfried's in a context appropriate class, probably the class that was consuming the random enum value.
Hope this helps Jay <<snip>>
This is more of an academic point really. As H has shows this is already
possible with a few lines of code, but I was hoping to have done this with
one simple single line of code.
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:u1**************@TK2MSFTNGP12.phx.gbl... Hi Terry,
As I said in my first post I do not know what you want to do, an Enum is in my opinion to get a value using a keyword in a program. Something as Direction = TravelDirection.North OR TravelDirection.West (What would give as the Enum values where 1 2 4 8 using the normal situation give 3 and than I could use that to go NorthWest in my screen)
However when I want to know what my randomnumber 4 would be I would just use a hashtable, where it would give a value "East" (accoording to your table).
So I am curious in what situation you want to use this.
Cor When I would get a value "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> schreef in bericht news:%2****************@TK2MSFTNGP12.phx.gbl... Hi Cor, Im not sure if you understood my origional post. What I wanted was to be able to choose randomly any element from the Enum members, in order to do this you need to know the number of elements.
Herfried's code will do this quite well.
I went on to say that It would be nice if the Enum class would be able to choose one element at Random and Jay said he thought this was to much and it probably is, however, it would be nice if the Enum could report the number of elements it has to simplify choosing one from its return element values array.
Unfortunately you dont seem to be able to inherit an Enum or I would have wrapped it myself.
Cheers
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Cor Ligthert" <no************@planet.nl> wrote in message news:OB**************@TK2MSFTNGP12.phx.gbl... Terry,
I hope you do not mind that I answer,
In my opinion is an Enum the best used when it has the values 1 2 4 8 Than you can do an OR or whatever with it, and has it no sence to know the max value (in this case 15) or F
However just my opinion.
Cor
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com>
OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... > OHM, > ?? > > You want a method on the Enum class that randomly returns one of the > Enum's values? > > IMHO Such a function would suggest that the Enum class is trying to do > too much! > > I would use code similar to Herfried's in a context appropriate class, > probably the class that was consuming the random enum value. > > Hope this helps > Jay > > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in > message news:uM**************@TK2MSFTNGP15.phx.gbl... >> Thanks 2 both, i've used something similar in the past but its just a >> shame this was not built in to an enum class. >> >> Cheers >> >> -- >> OHM ( Terry Burns ) * Use the following to email me * >> >> Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> For i As Int32 = 0 To ch.Length - 1 >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> Next >> Process.Start("mailto:" & New String(ch)) >> -- >> >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in >> message news:uT*************@TK2MSFTNGP11.phx.gbl... >>> "Larry Serflaten" <se*******@usinternet.com> schrieb: >>>>> Dim Values() As AnchorStyles = >>>>> [Enum].GetValues(GetType(AnchorStyles)) >>>>> Dim r As New Random >>>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >>>> >>>> The maxValue of Next needs to be one greater than the last value >>>> desired: >>>> >>>> Dim Values() As AnchorStyles = >>>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >>>> Dim r As New Random >>>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >>> >>> ACK, I should have read the documentation... ;-). >>> >>> -- >>> Herfried K. Wagner [MVP] >>> <URL:http://dotnet.mvps.org/> >> >> > >
Yes your last point is the one I am making
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"JD" <no@spam.com> wrote in message news:SXUgd.26314$HA.22935@attbi_s01... For one, enum per CLI specification cannot have any methods\properties\events of its own. It can't even have any instance fields except one, and thats used for defining the underlying type of the enum. So forget about extending it.
Could it add a shared method Count that excepts an enum type and returns the number of fields? Maybe, but I'm not sure it would be a good design decision.
Lets say it has the shared Count\Length method, Count\Length denotes arrays\collections\containers, and enum is not any of these. Matter of fact an enum does not contain anything except the one instance variable as perviously mentioned.
Could it represent the size of the enum? No because an enum defines named literals and thats it.
Could it represent the number of valid values of the enum? Not really because I can assign any value to an enum field whether its defined by the enum type or not. Ex. Public Enum Fruit Apple Orange End Enum Dim F As Fruit F = Ctype(10,Fruit)
What it actually represents is the number of named literals defined by the enum type. If this is true then what good is this value when used against the enum itself? What does it tell you about the enum and how is it useful? I can't really think of anything useful off the top of my head.
Where it does become useful is when you ask for the name or value arrays, and arrays already of the property length, and it properly describes the array not the enum
Anyways thinking out loud and just my two cents
JD "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:O8**************@TK2MSFTNGP11.phx.gbl... OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... > OHM, > ?? > > You want a method on the Enum class that randomly returns one of the > Enum's values? > > IMHO Such a function would suggest that the Enum class is trying to do too > much! > > I would use code similar to Herfried's in a context appropriate class, > probably the class that was consuming the random enum value. > > Hope this helps > Jay > > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in > message news:uM**************@TK2MSFTNGP15.phx.gbl... >> Thanks 2 both, i've used something similar in the past but its just a >> shame this was not built in to an enum class. >> >> Cheers >> >> -- >> OHM ( Terry Burns ) * Use the following to email me * >> >> Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> For i As Int32 = 0 To ch.Length - 1 >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> Next >> Process.Start("mailto:" & New String(ch)) >> -- >> >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message >> news:uT*************@TK2MSFTNGP11.phx.gbl... >>> "Larry Serflaten" <se*******@usinternet.com> schrieb: >>>>> Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) >>>>> Dim r As New Random >>>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >>>> >>>> The maxValue of Next needs to be one greater than the last value >>>> desired: >>>> >>>> Dim Values() As AnchorStyles = >>>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >>>> Dim r As New Random >>>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >>> >>> ACK, I should have read the documentation... ;-). >>> >>> -- >>> Herfried K. Wagner [MVP] >>> <URL:http://dotnet.mvps.org/> >> >> > >
Well then, since its only applicable when you are using those arrays, the
Length property of those arrays are what you should be using, and it should
not be a shared method of the enum.
In this exercise, you are asking the enum to act like an array, enums don't
contain anything, they have literal fields. Matter of fact if you look at
the IL of the CLR enum class or the rotor source code, enum itself has to
either use reflection or an internal call to inspect the metadata to
determine the number of fields, their name and values. This would be true of
all other class unless the developer or compiler hardcodes the "number of
fields" property.
When building a library like .NET's, the exposed interface available to the
programmer to use must be as clear and simple as possible. Not to mention
enums are quirky little entities in the CLR to begin with. I really don't
think having a shared method that returns number of fields for one
type(enums) and not the others would be a good thing, especially when you
can get at it through reflection anyway.
JD
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:uK**************@TK2MSFTNGP12.phx.gbl... Yes your last point is the one I am making
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"JD" <no@spam.com> wrote in message news:SXUgd.26314$HA.22935@attbi_s01... For one, enum per CLI specification cannot have any methods\properties\events of its own. It can't even have any instance fields except one, and thats used for defining the underlying type of the enum. So forget about extending it.
Could it add a shared method Count that excepts an enum type and returns the number of fields? Maybe, but I'm not sure it would be a good design decision.
Lets say it has the shared Count\Length method, Count\Length denotes arrays\collections\containers, and enum is not any of these. Matter of fact an enum does not contain anything except the one instance variable as perviously mentioned.
Could it represent the size of the enum? No because an enum defines
named literals and thats it.
Could it represent the number of valid values of the enum? Not really because I can assign any value to an enum field whether its defined by
the enum type or not. Ex. Public Enum Fruit Apple Orange End Enum Dim F As Fruit F = Ctype(10,Fruit)
What it actually represents is the number of named literals defined by
the enum type. If this is true then what good is this value when used
against the enum itself? What does it tell you about the enum and how is it useful? I can't really think of anything useful off the top of my head.
Where it does become useful is when you ask for the name or value
arrays, and arrays already of the property length, and it properly describes the array not the enum
Anyways thinking out loud and just my two cents
JD "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:O8**************@TK2MSFTNGP11.phx.gbl... OK, why do you think its too much to ask of an Enum Class to be able to report the number of elements it contains ?
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl... > OHM, > ?? > > You want a method on the Enum class that randomly returns one of the > Enum's values? > > IMHO Such a function would suggest that the Enum class is trying to
do too > much! > > I would use code similar to Herfried's in a context appropriate
class, > probably the class that was consuming the random enum value. > > Hope this helps > Jay > > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in > message news:uM**************@TK2MSFTNGP15.phx.gbl... >> Thanks 2 both, i've used something similar in the past but its just
a >> shame this was not built in to an enum class. >> >> Cheers >> >> -- >> OHM ( Terry Burns ) * Use the following to email me * >> >> Dim ch() As Char =
"ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> For i As Int32 = 0 To ch.Length - 1 >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> Next >> Process.Start("mailto:" & New String(ch)) >> -- >> >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in
message >> news:uT*************@TK2MSFTNGP11.phx.gbl... >>> "Larry Serflaten" <se*******@usinternet.com> schrieb: >>>>> Dim Values() As AnchorStyles = [Enum].GetValues(GetType(AnchorStyles)) >>>>> Dim r As New Random >>>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >>>> >>>> The maxValue of Next needs to be one greater than the last value >>>> desired: >>>> >>>> Dim Values() As AnchorStyles = >>>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >>>> Dim r As New Random >>>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >>> >>> ACK, I should have read the documentation... ;-). >>> >>> -- >>> Herfried K. Wagner [MVP] >>> <URL:http://dotnet.mvps.org/> >> >> > >
Despite your explaination, I dont think it would be that difficult or wrong
to have provided a property to return the number of elements, but that's my
opinion. I for one would have welcomed that.
Thanks for your views anyway.
Regards - OHM
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"JD" <no@spam.com> wrote in message news:yC5hd.39100$R05.2136@attbi_s53... Well then, since its only applicable when you are using those arrays, the Length property of those arrays are what you should be using, and it should not be a shared method of the enum.
In this exercise, you are asking the enum to act like an array, enums don't contain anything, they have literal fields. Matter of fact if you look at the IL of the CLR enum class or the rotor source code, enum itself has to either use reflection or an internal call to inspect the metadata to determine the number of fields, their name and values. This would be true of all other class unless the developer or compiler hardcodes the "number of fields" property.
When building a library like .NET's, the exposed interface available to the programmer to use must be as clear and simple as possible. Not to mention enums are quirky little entities in the CLR to begin with. I really don't think having a shared method that returns number of fields for one type(enums) and not the others would be a good thing, especially when you can get at it through reflection anyway.
JD
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... Yes your last point is the one I am making
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"JD" <no@spam.com> wrote in message news:SXUgd.26314$HA.22935@attbi_s01... > For one, enum per CLI specification cannot have any > methods\properties\events of its own. It can't even have any instance > fields > except one, and thats used for defining the underlying type of the > enum. > So > forget about extending it. > > Could it add a shared method Count that excepts an enum type and > returns > the > number of fields? Maybe, but I'm not sure it would be a good design > decision. > > Lets say it has the shared Count\Length method, Count\Length denotes > arrays\collections\containers, and enum is not any of these. Matter of > fact > an enum does not contain anything except the one instance variable as > perviously mentioned. > > Could it represent the size of the enum? No because an enum defines named > literals and thats it. > > Could it represent the number of valid values of the enum? Not really > because I can assign any value to an enum field whether its defined by the > enum type or not. Ex. > Public Enum Fruit > Apple > Orange > End Enum > Dim F As Fruit > F = Ctype(10,Fruit) > > What it actually represents is the number of named literals defined by the > enum type. If this is true then what good is this value when used against > the enum itself? What does it tell you about the enum and how is it > useful? > I can't really think of anything useful off the top of my head. > > Where it does become useful is when you ask for the name or value arrays, > and arrays already of the property length, and it properly describes > the > array not the enum > > Anyways thinking out loud and just my two cents > > JD > > > > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in > message > news:O8**************@TK2MSFTNGP11.phx.gbl... >> OK, why do you think its too much to ask of an Enum Class to be able >> to >> report the number of elements it contains ? >> >> -- >> OHM ( Terry Burns ) * Use the following to email me * >> >> Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> For i As Int32 = 0 To ch.Length - 1 >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> Next >> Process.Start("mailto:" & New String(ch)) >> -- >> >> >> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message >> news:%2****************@TK2MSFTNGP09.phx.gbl... >> > OHM, >> > ?? >> > >> > You want a method on the Enum class that randomly returns one of the >> > Enum's values? >> > >> > IMHO Such a function would suggest that the Enum class is trying to do > too >> > much! >> > >> > I would use code similar to Herfried's in a context appropriate class, >> > probably the class that was consuming the random enum value. >> > >> > Hope this helps >> > Jay >> > >> > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in >> > message news:uM**************@TK2MSFTNGP15.phx.gbl... >> >> Thanks 2 both, i've used something similar in the past but its just a >> >> shame this was not built in to an enum class. >> >> >> >> Cheers >> >> >> >> -- >> >> OHM ( Terry Burns ) * Use the following to email me * >> >> >> >> Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> >> For i As Int32 = 0 To ch.Length - 1 >> >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> >> Next >> >> Process.Start("mailto:" & New String(ch)) >> >> -- >> >> >> >> >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message >> >> news:uT*************@TK2MSFTNGP11.phx.gbl... >> >>> "Larry Serflaten" <se*******@usinternet.com> schrieb: >> >>>>> Dim Values() As AnchorStyles = > [Enum].GetValues(GetType(AnchorStyles)) >> >>>>> Dim r As New Random >> >>>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >> >>>> >> >>>> The maxValue of Next needs to be one greater than the last value >> >>>> desired: >> >>>> >> >>>> Dim Values() As AnchorStyles = >> >>>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >> >>>> Dim r As New Random >> >>>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >> >>> >> >>> ACK, I should have read the documentation... ;-). >> >>> >> >>> -- >> >>> Herfried K. Wagner [MVP] >> >>> <URL:http://dotnet.mvps.org/> >> >> >> >> >> > >> > >> >> > >
Terry,
(you didn't upset me per se).
I agree the Framework designers and/or the language designers could have
included the feature, however should they have? As JD has explained they did
not.
Should it be a property of a specific Enum
Fiend Enum TravelDirection
North
South
East
West
End Enum
Dim count As Integer = TravelDirection.Length
Or a shared function of all Enums?
Dim count As Integer = [Enum].GetLength(GetType(TravelDirection))
Or maybe even just a VB.NET specific keyword?
Dim count As Integer = Len(TravelDirection)
The benefit of the specific property is it is coupled to a specific Enum,
although it may hide a value of the Enum... Of course you can approximate
this with adding another value to the Enum:
Fiend Enum TravelDirection
North
South
East
West
Length ' represents the 4 "real" elements in this Enum
End Enum
The danger of TravelDirection.Length is it will show up on Enum.GetNames &
Enum.GetValues, Enum.Parse, and Enum.IsDefined!
The benefit of the Shared Function is it is consistent with Enum.GetNames &
Enum.GetValues.
The benefit of the keyword is that there would be no changes to the
Framework itself. Of course you can approximate the keyword with Herfried's
function.
Public Function Len(ByVal enumType As Type) As Integer
Return [Enum].GetValues(enumType).Length
End Function
Dim count As Integer = Len(GetType(TravelDirection))
However!!! My concerns of such a member could cause problems still stands,
as Enums are not required to start with zero nor are they required to be
contiguous. In other words if I have a Flags enum, what does Enum.Length
really represent?
<Flags> _
Friend Enum TravelDirection
None = 0
North = 1
South = 2
East = 4
West = 8
NorthWest = North Or West
...
All = North Or South Or East Or West
End Enum
Should All or NorthWest be included in the Length or not?
I hope you realize I am not disagreeing as much as I am being overly
cautious of such a member. I'm not sure if the designers had these same
feelings when they left the member out of Enum or not...
Hope this helps
Jay
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl... Despite your explaination, I dont think it would be that difficult or wrong to have provided a property to return the number of elements, but that's my opinion. I for one would have welcomed that.
Thanks for your views anyway.
Regards - OHM
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote I hope you realize I am not disagreeing as much as I am being overly cautious of such a member. I'm not sure if the designers had these same feelings when they left the member out of Enum or not...
I would think it is a matter of utilization. How often does a person need to
know how many members are present? 1:100, 1:1000, or maybe 1:10000?
If they did Enums, structures and classes should also include such capablilities
should they not? Again, how often would that be needed?
I'm sure we've all seen posts similar to "MS should have provided an easy way to ___"
But they can't anticipate every need, and the more details they try to inlcude, the
more complicated the language becomes. The original (Dartmouth) Basic had
about 15 commands....
Functions or methods that are not provided but are often used, should be put into
a library and included in those projects that need them. This is how the language
is extendable, such that developers can add any functions they want and treat them
just like part of the language. The trouble with that idea however, is that the entire
library needs to be shipped, even if just one method is used from it.
What should have been considered, is some means to provide static linking
instead of assuming 'dynamic linking' is right for all cases. If static linking was
available then developers certainly could build large libraries of methods and
functions to be used over and over again in thier various projects, without
worrying that the libraries are getting too large, or that they include the entire
library even when only one method is used. With static linking, only those
methods that are actually used get included in the exe.
If I were to add my voice to the "There should be a way to" crowd, that is
what I would have been asking for all these many years. And, since .NET
is still fairly new I am going to double my efforts in seeing that they add
that capability so that developers can reference their own libraries and call
on their own custom functions, and know that only the functions they use
are compiled into the (MSIL) exe image.
LFS
Larry, What should have been considered, is some means to provide static linking instead of assuming 'dynamic linking' is right for all cases.
Have you looked at .NET Modules (a .netmodule file) (not to be confused with
VB.NET's Module keyword).
They are similar to C++ static linking, as they allow you to package one or
more functions in a Module, then the module is included with a multi-file
Assembly, as opposed to the single-file Assembly we are use to seeing.
A .NET Module is probably closer to a C++ .OBJ file, rather then a C++ .LIB
file.
Unfortunately VS.NET does not support Modules, however vbc.exe (the actual
Visual Basic compiler) does support Modules.
For an example see: http://msdn.microsoft.com/library/de...blyexample.asp
For vbc.exe support see: http://msdn.microsoft.com/library/de...ToAssembly.asp http://msdn.microsoft.com/library/de...fAddmodule.asp
Hope this helps
Jay
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote I hope you realize I am not disagreeing as much as I am being overly cautious of such a member. I'm not sure if the designers had these same feelings when they left the member out of Enum or not...
I would think it is a matter of utilization. How often does a person need to know how many members are present? 1:100, 1:1000, or maybe 1:10000? If they did Enums, structures and classes should also include such capablilities should they not? Again, how often would that be needed?
I'm sure we've all seen posts similar to "MS should have provided an easy way to ___" But they can't anticipate every need, and the more details they try to inlcude, the more complicated the language becomes. The original (Dartmouth) Basic had about 15 commands....
Functions or methods that are not provided but are often used, should be put into a library and included in those projects that need them. This is how the language is extendable, such that developers can add any functions they want and treat them just like part of the language. The trouble with that idea however, is that the entire library needs to be shipped, even if just one method is used from it.
What should have been considered, is some means to provide static linking instead of assuming 'dynamic linking' is right for all cases. If static linking was available then developers certainly could build large libraries of methods and functions to be used over and over again in thier various projects, without worrying that the libraries are getting too large, or that they include the entire library even when only one method is used. With static linking, only those methods that are actually used get included in the exe.
If I were to add my voice to the "There should be a way to" crowd, that is what I would have been asking for all these many years. And, since .NET is still fairly new I am going to double my efforts in seeing that they add that capability so that developers can reference their own libraries and call on their own custom functions, and know that only the functions they use are compiled into the (MSIL) exe image.
LFS
Its not about difficulty, its about clean interface design. I spent 2 years
writing a framework for developers and if the interface is not clean,
consistent and simple then it causes confusion. Its really, really hard.
What we pushed out was far from perfect and the library really didn't
express itself clearly. One of things we had trouble with was, when should
we implement something, and when should we let the users build their own
utility code or policies of their own that they could use in conjunction
with our library. If you try to satisfy all usage scenarios in your base
class library then your class library is going to suck.
Asking a type to return the number of fields it contains, so you can treat
the type's fields like an array, when the type is not an array, would cause
confusion. The Enum type is not an array of values, its a type with fields
that have literal values. There is a difference. If you want to reflect over
the type to get the fields array, and iterate over that, thats more
expressive and clear. Or in the Enum's case you can get the values and names
and iterate over them. Pushing any piece of that abstraction up into the
Enum itself, in my opinion, would be confusing and would muttle the Enum
interface, especially when people have enough trouble with the Enum type.
I know I probably won't convince you, but just throwing a method into a type
might seem simple and harmless but the reprecussions could be be negative.
I've seen it in action. This may be one of cases where you can write utility
code to express the specific view you are shooting for, and viola, you have
a class library that sits on top of the enum type. I've included some sample
code below, its far from perfect, but hopefully it'll give you an idea.
Again just my 2 cents.
JD
Imports System
Imports System.Reflection
Module Module1
Public Class EnumArray
Private _Fields() as FieldInfo
Private Sub New()
End Sub
Public Sub New (EnumType As Type)
_Fields = EnumType.GetFields(BindingFlags.Static Or
BindingFlags.[Public])
End Sub
Public ReadOnly Property Length As Integer
Get
return _Fields.Length
End Get
End Property
Public ReadOnly Property Name(Indx As Integer) As String
Get
If Indx >= length Then
Throw new ArgumentException("Out of range")
End If
return _Fields(Indx).Name
End Get
End Property
Public ReadOnly Property Value(Indx As Integer) As Integer
Get
If Indx >= length Then
Throw new ArgumentException("Out of range")
End If
'Would have to check for all possible types that enums are
'allowed. Int32 is default though.
return Ctype(_Fields(Indx).GetValue(Nothing),Integer)
End Get
End Property
Public Default ReadOnly Property Item(Indx As Integer) As EnumPair
Get
If Indx >= length Then
Throw new ArgumentException("Out of range")
End If
Dim EP As New EnumPair()
EP._Name = Name(Indx)
EP._Value = Value(Indx)
Return EP
End Get
End Property
Public Class EnumPair
Friend _Name As String
Friend _Value as Integer
Friend Sub New()
End Sub
Public ReadOnly Property Name As String
Get
return _Name
End Get
End Property
Public ReadOnly Property Value As Integer
Get
return _Value
End Get
End Property
End Class
End Class
Public Enum Fruit
Apple = 5
Orange = 10
Comquat = 20
End Enum
Sub Main()
Dim F As Fruit
Dim FruitArray As New EnumArray(F.GetType())
For I As Integer = 0 To FruitArray.Length - 1
Console.WriteLine("Name:{0} Value:{1}",FruitArray(i).Name, _
FruitArray(i).Value)
Next
Console.ReadLine
End Sub
End Module
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:eu**************@TK2MSFTNGP12.phx.gbl... Despite your explaination, I dont think it would be that difficult or
wrong to have provided a property to return the number of elements, but that's
my opinion. I for one would have welcomed that.
Thanks for your views anyway.
Regards - OHM
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"JD" <no@spam.com> wrote in message news:yC5hd.39100$R05.2136@attbi_s53... Well then, since its only applicable when you are using those arrays,
the Length property of those arrays are what you should be using, and it should not be a shared method of the enum.
In this exercise, you are asking the enum to act like an array, enums don't contain anything, they have literal fields. Matter of fact if you look
at the IL of the CLR enum class or the rotor source code, enum itself has
to either use reflection or an internal call to inspect the metadata to determine the number of fields, their name and values. This would be
true of all other class unless the developer or compiler hardcodes the "number
of fields" property.
When building a library like .NET's, the exposed interface available to the programmer to use must be as clear and simple as possible. Not to
mention enums are quirky little entities in the CLR to begin with. I really
don't think having a shared method that returns number of fields for one type(enums) and not the others would be a good thing, especially when
you can get at it through reflection anyway.
JD
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... Yes your last point is the one I am making
-- OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() For i As Int32 = 0 To ch.Length - 1 ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) Next Process.Start("mailto:" & New String(ch)) --
"JD" <no@spam.com> wrote in message news:SXUgd.26314$HA.22935@attbi_s01... > For one, enum per CLI specification cannot have any > methods\properties\events of its own. It can't even have any instance > fields > except one, and thats used for defining the underlying type of the > enum. > So > forget about extending it. > > Could it add a shared method Count that excepts an enum type and > returns > the > number of fields? Maybe, but I'm not sure it would be a good design > decision. > > Lets say it has the shared Count\Length method, Count\Length denotes > arrays\collections\containers, and enum is not any of these. Matter
of > fact > an enum does not contain anything except the one instance variable as > perviously mentioned. > > Could it represent the size of the enum? No because an enum defines named > literals and thats it. > > Could it represent the number of valid values of the enum? Not really > because I can assign any value to an enum field whether its defined
by the > enum type or not. Ex. > Public Enum Fruit > Apple > Orange > End Enum > Dim F As Fruit > F = Ctype(10,Fruit) > > What it actually represents is the number of named literals defined
by the > enum type. If this is true then what good is this value when used against > the enum itself? What does it tell you about the enum and how is it > useful? > I can't really think of anything useful off the top of my head. > > Where it does become useful is when you ask for the name or value arrays, > and arrays already of the property length, and it properly describes > the > array not the enum > > Anyways thinking out loud and just my two cents > > JD > > > > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in > message > news:O8**************@TK2MSFTNGP11.phx.gbl... >> OK, why do you think its too much to ask of an Enum Class to be able >> to >> report the number of elements it contains ? >> >> -- >> OHM ( Terry Burns ) * Use the following to email me * >> >> Dim ch() As Char =
"ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> For i As Int32 = 0 To ch.Length - 1 >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> Next >> Process.Start("mailto:" & New String(ch)) >> -- >> >> >> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message >> news:%2****************@TK2MSFTNGP09.phx.gbl... >> > OHM, >> > ?? >> > >> > You want a method on the Enum class that randomly returns one of
the >> > Enum's values? >> > >> > IMHO Such a function would suggest that the Enum class is trying
to do > too >> > much! >> > >> > I would use code similar to Herfried's in a context appropriate class, >> > probably the class that was consuming the random enum value. >> > >> > Hope this helps >> > Jay >> > >> > "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote
in >> > message news:uM**************@TK2MSFTNGP15.phx.gbl... >> >> Thanks 2 both, i've used something similar in the past but its
just a >> >> shame this was not built in to an enum class. >> >> >> >> Cheers >> >> >> >> -- >> >> OHM ( Terry Burns ) * Use the following to email me * >> >> >> >> Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray() >> >> For i As Int32 = 0 To ch.Length - 1 >> >> ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1) >> >> Next >> >> Process.Start("mailto:" & New String(ch)) >> >> -- >> >> >> >> >> >> "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message >> >> news:uT*************@TK2MSFTNGP11.phx.gbl... >> >>> "Larry Serflaten" <se*******@usinternet.com> schrieb: >> >>>>> Dim Values() As AnchorStyles = > [Enum].GetValues(GetType(AnchorStyles)) >> >>>>> Dim r As New Random >> >>>>> MsgBox(Values(r.Next(0, Values.Length - 1)).ToString()) >> >>>> >> >>>> The maxValue of Next needs to be one greater than the last
value >> >>>> desired: >> >>>> >> >>>> Dim Values() As AnchorStyles = >> >>>> CType([Enum].GetValues(GetType(AnchorStyles)), AnchorStyles()) >> >>>> Dim r As New Random >> >>>> MsgBox(Values(r.Next(0, Values.Length)).ToString()) >> >>> >> >>> ACK, I should have read the documentation... ;-). >> >>> >> >>> -- >> >>> Herfried K. Wagner [MVP] >> >>> <URL:http://dotnet.mvps.org/> >> >> >> >> >> > >> > >> >> > >
"JD" <no@spam.com> schrieb: Asking a type to return the number of fields it contains, so you can treat the type's fields like an array, when the type is not an array, would cause confusion. The Enum type is not an array of values, its a type with fields that have literal values. There is a difference. If you want to reflect over the type to get the fields array, and iterate over that, thats more expressive and clear. Or in the Enum's case you can get the values and names and iterate over them.
Mhm... With a similar argument it would be possible to say that getting the
enum's possible values is "bad design" because an enum can take combinations
of the values too, and the method may thus be misleading.
To make a conclusion: I agree that there is no need to add this property.
There was a need to add methods for getting the names of an enumeration's
constants, and their values, respectively. By providing these two ways to
get information about the enum, it's not directly required to add a property
that will return the number of constants defined in the enum, because this
would be simple "wrapper" code, as posted in previous messages in this
thread.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote What should have been considered, is some means to provide static linking instead of assuming 'dynamic linking' is right for all cases.
Have you looked at .NET Modules (a .netmodule file) (not to be confused with VB.NET's Module keyword).
No, I hadn't (I'm not too keen on compiling from the command line ;-)
That does not seem to be picking and choosing only the used functions out
of the modules. It apparently requires that the entire module be present at
runtime, which defeats the purpose....
Imagine, for example, I create my own Math class, and in one project I only
use ABS, SIN and COS. Those are the only functions I want included in the
EXE because there may be a hundred or more functions in that class that I am
not using. I might have my own Math class, File access module, Dialog module
and a number of others all neatly organized into namespaces and classes, that I
want to use part of, but I don't want to ship all of them in their entirety when I
am only using a handful of methods from each of them. http://msdn.microsoft.com/library/de...fAddmodule.asp
From that page:
--------
All modules added with /addmodule must be in the same directory
as the output file at run time. That is, you can specify a module
in any directory at compile time, but the module must be in the
application directory at run time. If it is not, you get a
System.TypeLoadException error.
---------
So that seems to be saying the entire module must be shipped with the
assembly....
LFS
OK, I'm convinced, after listening to all viewpoints on this, I think I
finally agree that the Enum class is positioned about where it should be.
Thanks to all for an interesting discussion.
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl... "JD" <no@spam.com> schrieb: Asking a type to return the number of fields it contains, so you can treat the type's fields like an array, when the type is not an array, would cause confusion. The Enum type is not an array of values, its a type with fields that have literal values. There is a difference. If you want to reflect over the type to get the fields array, and iterate over that, thats more expressive and clear. Or in the Enum's case you can get the values and names and iterate over them.
Mhm... With a similar argument it would be possible to say that getting the enum's possible values is "bad design" because an enum can take combinations of the values too, and the method may thus be misleading.
To make a conclusion: I agree that there is no need to add this property. There was a need to add methods for getting the names of an enumeration's constants, and their values, respectively. By providing these two ways to get information about the enum, it's not directly required to add a property that will return the number of constants defined in the enum, because this would be simple "wrapper" code, as posted in previous messages in this thread.
-- Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/>
Larry, That does not seem to be picking and choosing only the used functions out of the modules. It apparently requires that the entire module be present at runtime, which defeats the purpose....
It really depends on the granularity of your net modules. The more granular
your net modules the more choosing you will have...
Those are the only functions I want included in the EXE because there may be a hundred or more functions in that class that I am not using.
Due to OOP, I would expect the entire class to be included, as you defined a
class to contain all those functions! The class *is* the Abstraction! not
the 3 or 4 functions that you want to use.
If only "part" of a class with hundreds of members is needed, to me it would
suggest that the class may be doing "too much" and might need to be split
into smaller more specialized classes.
If I took the net module approach, at the very least each "class" would be
its own net module.
However! IMHO part of the reason for using .NET is to simplify the
developers job, I find Dynamically linked assemblies to significantly
simplify my job as a developer. Using static linking I found normally
complicated my job, especially when one of the statically linked routines
needed maintenance. Even considering net modules, sounds like its
complicating the job...
Yes there are tools that allegedly allow you to statically link only the
parts of the Framework that your application needs, however I have not tried
them... Nor do I expect I will, as I see more complications in them then
simplifications...
So that seems to be saying the entire module must be shipped with the assembly....
Yes, as I stated, you would be creating a multi-file assembly!
Hope this helps
Jay
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote
> What should have been considered, is some means to provide static > linking > instead of assuming 'dynamic linking' is right for all cases.
Have you looked at .NET Modules (a .netmodule file) (not to be confused with VB.NET's Module keyword).
No, I hadn't (I'm not too keen on compiling from the command line ;-)
That does not seem to be picking and choosing only the used functions out of the modules. It apparently requires that the entire module be present at runtime, which defeats the purpose....
Imagine, for example, I create my own Math class, and in one project I only use ABS, SIN and COS. Those are the only functions I want included in the EXE because there may be a hundred or more functions in that class that I am not using. I might have my own Math class, File access module, Dialog module and a number of others all neatly organized into namespaces and classes, that I want to use part of, but I don't want to ship all of them in their entirety when I am only using a handful of methods from each of them.
http://msdn.microsoft.com/library/de...fAddmodule.asp
From that page: -------- All modules added with /addmodule must be in the same directory as the output file at run time. That is, you can specify a module in any directory at compile time, but the module must be in the application directory at run time. If it is not, you get a System.TypeLoadException error. ---------
So that seems to be saying the entire module must be shipped with the assembly....
LFS
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote That does not seem to be picking and choosing only the used functions out of the modules. It apparently requires that the entire module be present at runtime, which defeats the purpose....
It really depends on the granularity of your net modules. The more granular your net modules the more choosing you will have...
But the purpose of having a library is to gather related code into categories.
Look at .Net's Math class, for example. It has like 35 members, yet only
a few might be used in any single project. How can anyone know which
to group into a smaller module? They are all related, having to Math, and
some may be inter-related. So they all belong in the grouping, even if only
a few are used at a time.
Due to OOP, I would expect the entire class to be included, as you defined a class to contain all those functions! The class *is* the Abstraction! not the 3 or 4 functions that you want to use.
Then don't call it a class, call it a Library, or whatever else. As I said, a person
might group Math functions in one namespace, File access in another, an
assortment of dialogs in another, and so on. The advantage to having them
all in the group is that you only need to reference one library to get access to
all the functions in that grouping. If they were in separate modules, you'd have
to hunt and peck for a module that had the right combination, or create your
own for that project, which could be a tedious round of cut and paste coding....
In the end, that is about what it would amount to. Static linking would be a
convenient means to do cut and paste coding, just by adding a reference
pointing to the DLL and calling on those routines that are needed.
However! IMHO part of the reason for using .NET is to simplify the developers job, I find Dynamically linked assemblies to significantly simplify my job as a developer. Using static linking I found normally complicated my job, especially when one of the statically linked routines needed maintenance. Even considering net modules, sounds like its complicating the job...
I can see what you're saying about maintenance causing a bit of a problem.
But maybe thats a good idea, that the old code stays with the old version,
and to fix the problem you have to ship a new assembly that has the
fixes baked in. After all, you can simply delete the old file, and copy in
the replacement. Not so?
With side-by-side deployment, if you come out with a new DLL, then
you've got two DLLs out there, some assemblies using the new, and some
the old.
Yes there are tools that allegedly allow you to statically link only the parts of the Framework that your application needs, however I have not tried them... Nor do I expect I will, as I see more complications in them then simplifications...
That doesn't sound like what I'd want. As I said, it could be made an option,
an attribute on an assembly, to allow lifting code out of the assembly, or not.
I wouldn't want to lift every FCL routine I need, just a few out of my own
libraries....
LFS
Larry,
It seems we are talking oranges & apples here. :-|
I offered net modules, as that is the closest you are going to get currently
to static linking in .NET, unfortunately it is not static linking! I
apologize if I did not convey that to you in my initial response. I don't
believe static linking is planned for VS.NET 2005 (aka Whidbey, due out
later in 2005) or beyond. For information on VS.NET 2005 see http://lab.msdn.microsoft.com/vs2005/, you can submit requests for VS.NET
2005 & later (such as this) in the Product Feedback Center on the above
page. If MS sees a big enough request for it, it may be supported, likewise
if they see a big enough dissention it might not be supported...
I practice Object Oriented Thinking (OOT) which includes Object Oriented
Analysis (OOA) & Object Oriented Programming (OOP). Which means I don't see
a big need to include part of an abstraction (a class, such as Math) in an
assembly. In fact I see including part of the abstraction as very much anti
OOT. In other words in OOT I would expect the entire abstraction to be
included, whether that abstraction is a Class, Module, Enum, Delegate, or
Interface. Namespaces as you know simply allow you to organize the
previously listed abstractions, they can exist in multiple assemblies, which
means if static linking were allowed, parts of a namespace could be
included...
You seem to be thinking in terms of function libraries, which really isn't
OOP. I've done function libraries, how do you say it "been there, done
that". I find OOT to be significantly easier (analysis, design, coding), for
reasons as identified by Ian Graham in his book "Object Oriented Methods -
Principles & Practice - Third Edition" from Addison Wesley. Of course you
can apply OOT to languages such as C that only support function libraries.
In other words: If static linking were supported, I would expect the
*ENTIRE* abstraction to be included!
You do realize that you can use Visual SourceSafe to share source members
between projects? Which may be closer to what you want then net modules. In the end, that is about what it would amount to. Static linking would be a convenient means to do cut and paste coding, just by adding a reference pointing to the DLL and calling on those routines that are needed.
Cut & paste coding itself is a bad "code smell", as it introduces
duplication! See Martin Fowler's "Refactoring" (http://www.refactoring.com)
and Andrew Hunt's & David Thomas' "The Pragmatic Programmer"
(http://www.pragmaticprogrammer.com/) both from Addison Wesley for more
information on Code Smells & duplication.
Thinking about it I suppose I am seeing static linking as a form of
duplication also, as the (executable) code is being duplicated in different
assemblies.
With side-by-side deployment, if you come out with a new DLL, then you've got two DLLs out there, some assemblies using the new, and some the old.
I don't have specific links, however you can force the old assemblies to use
the new DLL, post if you would like links. Which means you can deploy your
class library once, possibly to the GAC, and avoid any duplication...
Hope this helps
Jay
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:O%***************@TK2MSFTNGP10.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote
> That does not seem to be picking and choosing only the used functions > out > of the modules. It apparently requires that the entire module be > present at > runtime, which defeats the purpose....
It really depends on the granularity of your net modules. The more granular your net modules the more choosing you will have...
But the purpose of having a library is to gather related code into categories. Look at .Net's Math class, for example. It has like 35 members, yet only a few might be used in any single project. How can anyone know which to group into a smaller module? They are all related, having to Math, and some may be inter-related. So they all belong in the grouping, even if only a few are used at a time.
Due to OOP, I would expect the entire class to be included, as you defined a class to contain all those functions! The class *is* the Abstraction! not the 3 or 4 functions that you want to use.
Then don't call it a class, call it a Library, or whatever else. As I said, a person might group Math functions in one namespace, File access in another, an assortment of dialogs in another, and so on. The advantage to having them all in the group is that you only need to reference one library to get access to all the functions in that grouping. If they were in separate modules, you'd have to hunt and peck for a module that had the right combination, or create your own for that project, which could be a tedious round of cut and paste coding....
In the end, that is about what it would amount to. Static linking would be a convenient means to do cut and paste coding, just by adding a reference pointing to the DLL and calling on those routines that are needed.
However! IMHO part of the reason for using .NET is to simplify the developers job, I find Dynamically linked assemblies to significantly simplify my job as a developer. Using static linking I found normally complicated my job, especially when one of the statically linked routines needed maintenance. Even considering net modules, sounds like its complicating the job...
I can see what you're saying about maintenance causing a bit of a problem. But maybe thats a good idea, that the old code stays with the old version, and to fix the problem you have to ship a new assembly that has the fixes baked in. After all, you can simply delete the old file, and copy in the replacement. Not so?
With side-by-side deployment, if you come out with a new DLL, then you've got two DLLs out there, some assemblies using the new, and some the old.
Yes there are tools that allegedly allow you to statically link only the parts of the Framework that your application needs, however I have not tried them... Nor do I expect I will, as I see more complications in them then simplifications...
That doesn't sound like what I'd want. As I said, it could be made an option, an attribute on an assembly, to allow lifting code out of the assembly, or not. I wouldn't want to lift every FCL routine I need, just a few out of my own libraries....
LFS
Larry,
When I get from all those inline answers the right idea, I have the idea
that you and me are completly inline with our thinking about the Enum.
However I get the idea that you want to split up applications in more
distributable parts, what I did like in the time of computers with low
amounts of memory.
Now that changed for me, a little bit more in a load can only give
performance benefit in my opinion, while more distributed parts can give
errors, by instance because a user did remove a part accedentitcly or loaded
an older version over that.
That means for me as well for the tool we are using, I even do not like it
that I sometimes have to set references what can confuse in the beginning.
(Before somebody starts to explain that I understand why and we are not
endusers).
Just some thoughts of me in this discussion. And when I got the ideas wrong,
correct me.
Cor
"Larry Serflaten" <se*******@usinternet.com> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote
> That does not seem to be picking and choosing only the used functions > out > of the modules. It apparently requires that the entire module be > present at > runtime, which defeats the purpose....
It really depends on the granularity of your net modules. The more granular your net modules the more choosing you will have...
But the purpose of having a library is to gather related code into categories. Look at .Net's Math class, for example. It has like 35 members, yet only a few might be used in any single project. How can anyone know which to group into a smaller module? They are all related, having to Math, and some may be inter-related. So they all belong in the grouping, even if only a few are used at a time.
Due to OOP, I would expect the entire class to be included, as you defined a class to contain all those functions! The class *is* the Abstraction! not the 3 or 4 functions that you want to use.
Then don't call it a class, call it a Library, or whatever else. As I said, a person might group Math functions in one namespace, File access in another, an assortment of dialogs in another, and so on. The advantage to having them all in the group is that you only need to reference one library to get access to all the functions in that grouping. If they were in separate modules, you'd have to hunt and peck for a module that had the right combination, or create your own for that project, which could be a tedious round of cut and paste coding....
In the end, that is about what it would amount to. Static linking would be a convenient means to do cut and paste coding, just by adding a reference pointing to the DLL and calling on those routines that are needed.
However! IMHO part of the reason for using .NET is to simplify the developers job, I find Dynamically linked assemblies to significantly simplify my job as a developer. Using static linking I found normally complicated my job, especially when one of the statically linked routines needed maintenance. Even considering net modules, sounds like its complicating the job...
I can see what you're saying about maintenance causing a bit of a problem. But maybe thats a good idea, that the old code stays with the old version, and to fix the problem you have to ship a new assembly that has the fixes baked in. After all, you can simply delete the old file, and copy in the replacement. Not so?
With side-by-side deployment, if you come out with a new DLL, then you've got two DLLs out there, some assemblies using the new, and some the old.
Yes there are tools that allegedly allow you to statically link only the parts of the Framework that your application needs, however I have not tried them... Nor do I expect I will, as I see more complications in them then simplifications...
That doesn't sound like what I'd want. As I said, it could be made an option, an attribute on an assembly, to allow lifting code out of the assembly, or not. I wouldn't want to lift every FCL routine I need, just a few out of my own libraries....
LFS
Isn't it funny how sometimes, the simplest posts on a simple subject (
supposedly ) result in huge threads.
--
OHM ( Terry Burns ) * Use the following to email me *
Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ub****************@TK2MSFTNGP09.phx.gbl... Larry,
When I get from all those inline answers the right idea, I have the idea that you and me are completly inline with our thinking about the Enum.
However I get the idea that you want to split up applications in more distributable parts, what I did like in the time of computers with low amounts of memory.
Now that changed for me, a little bit more in a load can only give performance benefit in my opinion, while more distributed parts can give errors, by instance because a user did remove a part accedentitcly or loaded an older version over that.
That means for me as well for the tool we are using, I even do not like it that I sometimes have to set references what can confuse in the beginning. (Before somebody starts to explain that I understand why and we are not endusers).
Just some thoughts of me in this discussion. And when I got the ideas wrong, correct me.
Cor
"Larry Serflaten" <se*******@usinternet.com> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote
> That does not seem to be picking and choosing only the used functions > out > of the modules. It apparently requires that the entire module be > present at > runtime, which defeats the purpose....
It really depends on the granularity of your net modules. The more granular your net modules the more choosing you will have...
But the purpose of having a library is to gather related code into categories. Look at .Net's Math class, for example. It has like 35 members, yet only a few might be used in any single project. How can anyone know which to group into a smaller module? They are all related, having to Math, and some may be inter-related. So they all belong in the grouping, even if only a few are used at a time.
Due to OOP, I would expect the entire class to be included, as you defined a class to contain all those functions! The class *is* the Abstraction! not the 3 or 4 functions that you want to use.
Then don't call it a class, call it a Library, or whatever else. As I said, a person might group Math functions in one namespace, File access in another, an assortment of dialogs in another, and so on. The advantage to having them all in the group is that you only need to reference one library to get access to all the functions in that grouping. If they were in separate modules, you'd have to hunt and peck for a module that had the right combination, or create your own for that project, which could be a tedious round of cut and paste coding....
In the end, that is about what it would amount to. Static linking would be a convenient means to do cut and paste coding, just by adding a reference pointing to the DLL and calling on those routines that are needed.
However! IMHO part of the reason for using .NET is to simplify the developers job, I find Dynamically linked assemblies to significantly simplify my job as a developer. Using static linking I found normally complicated my job, especially when one of the statically linked routines needed maintenance. Even considering net modules, sounds like its complicating the job...
I can see what you're saying about maintenance causing a bit of a problem. But maybe thats a good idea, that the old code stays with the old version, and to fix the problem you have to ship a new assembly that has the fixes baked in. After all, you can simply delete the old file, and copy in the replacement. Not so?
With side-by-side deployment, if you come out with a new DLL, then you've got two DLLs out there, some assemblies using the new, and some the old.
Yes there are tools that allegedly allow you to statically link only the parts of the Framework that your application needs, however I have not tried them... Nor do I expect I will, as I see more complications in them then simplifications...
That doesn't sound like what I'd want. As I said, it could be made an option, an attribute on an assembly, to allow lifting code out of the assembly, or not. I wouldn't want to lift every FCL routine I need, just a few out of my own libraries....
LFS
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc Dansereau |
last post by:
Hi all
I am new to this forum and to the c programming language.
If I understand, the random() function in C return numbers that follow a
uniform distribution U(0,1). Can somebody know how to...
|
by: ChrisB |
last post by:
Hello:
I will be creating 50+ enumerations related to a large number of classes
that span a number of namespaces. I was wondering if there are any "best
practices" when defining enumerations.
...
|
by: Ben Finney |
last post by:
Antoon Pardon wrote:
> I just downloaded your enum module for python
> and played a bit with it. IMO some of the behaviour makes it less
> usefull.
Feedback is appreciated. I'm hoping to...
|
by: fieldfallow |
last post by:
Hello all,
Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
Assuming there isn't, as it appears from the docs that I have, is there
a...
|
by: Ben Finney |
last post by:
Howdy all,
PEP 354: Enumerations in Python has been accepted as a draft PEP. The
current version can be viewed online:
<URL:http://www.python.org/peps/pep-0354.html>
Here is the...
|
by: Jim Michaels |
last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or
mysql.
I have not been able to do either. I get the same number back several times
from PHP's mt_rand() and from mysql's...
|
by: Peter Oliphant |
last post by:
I would like to be able to create a random number generator that produces
evenly distributed random numbers up to given number.
For example, I would like to pick a random number less than 100000,...
|
by: badcrusher10 |
last post by:
Hello.
I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work?
THIS IS WHAT I NEED TO DO:
Professor Snoop wants a program...
|
by: pereges |
last post by:
I need to generate two uniform random numbers between 0 and 1 in C ?
How to do it ?
I looked into rand function where you need to #define RAND_MAX as 1
but will this rand function give me ...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |