can u convert this to vb.net please?

  • Thread starter Thread starter Tariq Ahmad
  • Start date Start date
T

Tariq Ahmad

got this code from codeproject.com, but cant convert it to vb.

can u help please? thnx.

t.

----------------------------------------------------------------------------
-------

Append a Description attribute to each enum entry:

private enum MyColors
{
[Description("yuk!")] LightGreen = 0x012020,
[Description("nice :-)")] VeryDeepPink = 0x123456,
[Description("so what")] InvisibleGray = 0x456730,
[Description("no comment")] DeepestRed = 0xfafafa,
[Description("I give up")] PitchBlack = 0xffffff,
}To access the description from code, the following function is called:

public static string GetDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
 
Tariq Ahmad said:
got this code from codeproject.com, but cant convert it to vb.

can u help please? thnx.

t.

-------------------------------------------------------------------------- --
-------

Append a Description attribute to each enum entry:

private enum MyColors
{
[Description("yuk!")] LightGreen = 0x012020,
[Description("nice :-)")] VeryDeepPink = 0x123456,
[Description("so what")] InvisibleGray = 0x456730,
[Description("no comment")] DeepestRed = 0xfafafa,
[Description("I give up")] PitchBlack = 0xffffff,
}To access the description from code, the following function is called:
Private Enum MyColors
<Description("yuk!")> LightGreen = &h012020 ''' I may be wrong in the
hex
....
End Enum


public static string GetDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
public shared function GetDescription (value as Enum)
dim fi as fieldinfo
Dim attributes() as descriptionattribute

fi = value.gettype().getfield(value.toString())
attributes = fi.getcustomattributes(gettype(descriptionattribute),
false)

return iif(attributes.length > 0, attributes(0).description,
value.tostring()
''' this part may be wrong, I'm just doing it on the fly but this should
get you going
End sub
 
You should enable option strict in Outlook ;-)

: public shared function GetDescription (value as Enum)

Public Shared Function GetDescription(ByVal value As Enum) As String
..
..
..

Return IIf(attributes.Length > 0, attributes(0).Description,
value.ToString).ToString

End Function

: End sub

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


:
: : > got this code from codeproject.com, but cant convert it to vb.
: >
: > can u help please? thnx.
: >
: > t.
: >
:
--------------------------------------------------------------------------
: --
: > -------
: >
: > Append a Description attribute to each enum entry:
: >
: > private enum MyColors
: > {
: > [Description("yuk!")] LightGreen = 0x012020,
: > [Description("nice :-)")] VeryDeepPink = 0x123456,
: > [Description("so what")] InvisibleGray = 0x456730,
: > [Description("no comment")] DeepestRed = 0xfafafa,
: > [Description("I give up")] PitchBlack = 0xffffff,
: > }To access the description from code, the following function is called:
: >
: Private Enum MyColors
: <Description("yuk!")> LightGreen = &h012020 ''' I may be wrong in the
: hex
: ....
: End Enum
:
:
:
: > public static string GetDescription(Enum value)
: > {
: > FieldInfo fi= value.GetType().GetField(value.ToString());
: > DescriptionAttribute[] attributes =
: > (DescriptionAttribute[])fi.GetCustomAttributes(
: > typeof(DescriptionAttribute), false);
: > return
: (attributes.Length>0)?attributes[0].Description:value.ToString();
: > }
: >
: public shared function GetDescription (value as Enum)
: dim fi as fieldinfo
: Dim attributes() as descriptionattribute
:
: fi = value.gettype().getfield(value.toString())
: attributes = fi.getcustomattributes(gettype(descriptionattribute),
: false)
:
: return iif(attributes.length > 0, attributes(0).description,
: value.tostring()
: ''' this part may be wrong, I'm just doing it on the fly but this should
: get you going
: End sub
:
: >
:
:
 
Is that under Tools -> Options -> Intellisesne -> Newsgroups?

=)

my bad. Thanks for fixing that though Tom.
 
: Is that under Tools -> Options -> Intellisesne -> Newsgroups?
Scottish: Aye
Geordie: Eee


--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


: Is that under Tools -> Options -> Intellisesne -> Newsgroups?
:
: =)
:
: my bad. Thanks for fixing that though Tom.
:
:
: : > You should enable option strict in Outlook ;-)
: >
: > : public shared function GetDescription (value as Enum)
: >
: > Public Shared Function GetDescription(ByVal value As Enum) As String
: > .
: > .
: > .
: >
: > Return IIf(attributes.Length > 0, attributes(0).Description,
: > value.ToString).ToString
: >
: > End Function
: >
: > : End sub
: >
: > --
: > HTH,
: > -- Tom Spink, Über Geek
: >
: > Please respond to the newsgroup,
: > so all can benefit
: >
: > "Chaos, Panic, Disorder, my work here is done"
: >
: >
: > : > :
: > : : > : > got this code from codeproject.com, but cant convert it to vb.
: > : >
: > : > can u help please? thnx.
: > : >
: > : > t.
: > : >
: > :
: >
:
--------------------------------------------------------------------------
: > : --
: > : > -------
: > : >
: > : > Append a Description attribute to each enum entry:
: > : >
: > : > private enum MyColors
: > : > {
: > : > [Description("yuk!")] LightGreen = 0x012020,
: > : > [Description("nice :-)")] VeryDeepPink = 0x123456,
: > : > [Description("so what")] InvisibleGray = 0x456730,
: > : > [Description("no comment")] DeepestRed = 0xfafafa,
: > : > [Description("I give up")] PitchBlack = 0xffffff,
: > : > }To access the description from code, the following function is
: called:
: > : >
: > : Private Enum MyColors
: > : <Description("yuk!")> LightGreen = &h012020 ''' I may be wrong in
: the
: > : hex
: > : ....
: > : End Enum
: > :
: > :
: > :
: > : > public static string GetDescription(Enum value)
: > : > {
: > : > FieldInfo fi= value.GetType().GetField(value.ToString());
: > : > DescriptionAttribute[] attributes =
: > : > (DescriptionAttribute[])fi.GetCustomAttributes(
: > : > typeof(DescriptionAttribute), false);
: > : > return
: > : (attributes.Length>0)?attributes[0].Description:value.ToString();
: > : > }
: > : >
: > : public shared function GetDescription (value as Enum)
: > : dim fi as fieldinfo
: > : Dim attributes() as descriptionattribute
: > :
: > : fi = value.gettype().getfield(value.toString())
: > : attributes = fi.getcustomattributes(gettype(descriptionattribute),
: > : false)
: > :
: > : return iif(attributes.length > 0, attributes(0).description,
: > : value.tostring()
: > : ''' this part may be wrong, I'm just doing it on the fly but this
: should
: > : get you going
: > : End sub
: > :
: > : >
: > :
: > :
: >
: >
:
:
 
You probably don't want to use IIf. The conditional operator(?:) in C# is
short-circuited but IIf isn't, so

will choke when attributes.Length = 0.

So you'll need to go with:

If attributes.Length > 0 Then
Return attributes(0).Description
Else
Return value.ToString
End If

CJ Taylor said:
Tariq Ahmad said:
got this code from codeproject.com, but cant convert it to vb.

can u help please? thnx.

t.

--------------------------------------------------------------------------
--
-------

Append a Description attribute to each enum entry:

private enum MyColors
{
[Description("yuk!")] LightGreen = 0x012020,
[Description("nice :-)")] VeryDeepPink = 0x123456,
[Description("so what")] InvisibleGray = 0x456730,
[Description("no comment")] DeepestRed = 0xfafafa,
[Description("I give up")] PitchBlack = 0xffffff,
}To access the description from code, the following function is called:
Private Enum MyColors
<Description("yuk!")> LightGreen = &h012020 ''' I may be wrong in the
hex
....
End Enum


public static string GetDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
public shared function GetDescription (value as Enum)
dim fi as fieldinfo
Dim attributes() as descriptionattribute

fi = value.gettype().getfield(value.toString())
attributes = fi.getcustomattributes(gettype(descriptionattribute),
false)

return iif(attributes.length > 0, attributes(0).description,
value.tostring()
''' this part may be wrong, I'm just doing it on the fly but this should
get you going
End sub
 
Back
Top