0
Answer

Something between a class and a struct

Ask a question
Hi,

Consider this:

Dim Male as new Gender(EnumGender.Male)
Person1.Gender=Male
Person2.Gender=Male
Person2.Gender.Name="Female"

If Gender was an object, Person1 would have turned female together with Person2. What I wanted to say was: "I do have a GenderCode for Person2, and you, Gender, should go figure out what exact Enumerator value should be attached to that". I've hacked this but it doesn't feel right. The correct way would be:

Person2.enumGender = GenderName2Enum("Female")

which is much work (is there a function like that? where is it?) and just feels old-fashioned. I'd love some syntactic sugar somehow that let's you say "I'm not changing a complete object, just a value of the object it belongs too, but I do it in a luxurious way. Maybe like

Person2.Gender!Name="Female"

Saying: I'm changing a property of the Gender, but I only mean it for Person2 so if some other object happens to have the exact same Gender object please clone it before this one is changed by me. (remotely similar to the way strings are handled)

The Enum declaration could have some property-like functions like below:

Public Enum Gender
  Male=0
  Female=1

  Public Set Name(Value as String)
  if Value="Male" then return Male
  if Value="Female" then return Female
  Throw Exception("What?")
  End Set

  Public Get Name
  if me=Male then return "Male"
  if me=Female then return "Female"
  End Set

  Public Set Code(Value as Integer)
  if Value=1 then return Male
  if Value=2 then return Female
  Throw Exception("What?")
  End Set

End Enum

In my case I have 1, Man, Male all meaning Gender.Male. And I did write code that wouldn't turn Person1 in the first example to be female. But as people using my objects wouldn't expect that behaviour, I'd like to see a nice way to put this in the next version of VB.