-1

Hello and forgive me for my very bad English first of all...

I'm not used to working with structures or enum...

I have many class in my project and i'm trying to organize.

My Class Duree Will contain Multiple enum or struct.

I would like to be able to call different enum or struct.

And build the request on the associated structure.

RepairList.vb

Public Class RepairList

    Dim Tool As New Tools
    Dim uuu As String = Tool.Find("C:\Data\1\" & Form1.TextBox2.Text & ".txt", "ProductType")
    ' Now uuu contains iPhone7,2
    uuu = uuu.Replace(",", "")
    uuu = uuu.Replace("i", "I")
    uuu = uuu.Replace(" ", "")
    uuu = uuu.Replace("P", "p")
    ' Now uuu contain Iphone72
    Dim p As Devices.Device  ' Class Devices  --> Structure --> Device
    Dim info As Reflection.FieldInfo = p.GetType().GetField(uuu)
    Dim az As Object = info.GetValue(p)
    ' Now az contains Iphone 6
    Dim toto As Object
    toto = az
    toto = toto.Replace(" ", "")
    ' Now toto contain Iphone6 the Name of the enum i woul catch in Duree
    Dim test As String = myCheckbox.Name   ' now test contains AntenneWifi
    Dim value As Integer
    value = CInt([Enum].Parse(GetType(Duree.Iphone6), test))

 End Class

Devices.vb

 Public Class Devices

 Public Structure Device

    Const Iphone41 = "Iphone 4S"
    Const Iphone51 = "Iphone 5"
    Const Iphone52 = "Iphone 5"
    Const Iphone53 = "Iphone 5C"
    Const Iphone54 = "Iphone 5C"
    Const Iphone61 = "Ihpone 5S"
    Const Iphone62 = "Iphone 5S"
    Const Iphone71 = "Iphone 6 Plus"
    Const Iphone72 = "Iphone 6"
    Const Iphone81 = "Iphone 6S"
    Const Iphone82 = "Iphone 6S Plus"
    Const Iphone84 = "Iphone SE"
    Const Iphone91 = "Iphone 7"
    Const Iphone92 = "Iphone 7 Plus"
    Const Iphone93 = "Iphone 7"
    Const Iphone94 = "Iphone 7 Plus"
End Structure

End Class

Duree.vb

Public Class Duree

Public Enum Iphone5
    'Define the Repair Time For each action.
    AntenneWifi = 35
    Batterie = 15
    BtnVolDown = 45
    BtnVolUp = 45
    BtnHome = 20
    BtnPower = 35
    CableWifiBth = 35
    CamAr = 30
    MotherBoard = 30
    FullScreen = 20
    ScreenOnly = 40
    HPExt = 35
    HPInt = 25
    SimLevier = 35
    NappeHomeLong = 30
    NappeBtnPower = 45
    NappeFacetime = 30
    NappeVolVibr = 40
    PlaqueProtectLCD = 30
    PriseJack = 40
    SimTray = 2
    Vibreur = 25
End Enum

Public Enum Iphone6
    'Define the Repair Time For each action.
    AntenneWifi = 35
    Batterie = 15
    BtnVolDown = 45
    BtnVolUp = 45
    BtnHome = 20
    BtnPower = 35
    CableWifiBth = 35
    CamAr = 30
    MotherBoard = 30
    FullScreen = 20
    ScreenOnly = 40
    HPExt = 35
    HPInt = 25
    SimLevier = 35
    NappeHomeLong = 30
    NappeBtnPower = 45
    NappeFacetime = 30
    NappeVolVibr = 40
    PlaqueProtectLCD = 30
    PriseJack = 40
    SimTray = 2
    Vibreur = 25
End Enum
End Class

I put here only 2 enum but i have more in the Duree class

So in RepairList.vb I would Replace Duree.Iphone6 in the following line

 value = CInt([Enum].Parse(GetType(Durée.Iphone6), test))

By something like

value = CInt([Enum].Parse[GetType("Duree+" & toto), test))

But I can't find the right syntax.

Here is an overview of what I have looked at before

Enum's and interfaces

Search for a string in Enum and return the Enum

and many others ..

Thank's a lot if you can help me

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
moi
  • 11
  • 1
  • I really dont understand what you are trying here. You get from a variable as a string the name of the enum and then you try to set the integer associated to an enum value or what are you trying to do? – Rafa Gomez Jul 16 '17 at 15:18
  • Sorry I have to edit the main post to try to be more accurate. – moi Jul 16 '17 at 15:37
  • I guess he meant to find an Enum based by string. By the way, why do you want it? Enum is the best way to prevent any mistype if your program needs a choice... – Karuntos Jul 16 '17 at 15:38
  • Yes it seem to be what i need but in another class. I' dont find how to build the request to Durée – moi Jul 16 '17 at 15:52
  • Your question doesn't make any sense. I don't know if it is the langague or inexperience, but it makes no sense. – jmoreno Jul 16 '17 at 16:24
  • If I look at the class name Durée, this 'é' makes me tend to inexperience...but everyone was a beginner sometime. Have written an answer, but I don`t know if that's what he's looking for... – Rafa Gomez Jul 16 '17 at 16:48
  • Allright in my code i dont have the "é" but for sure i'm a beginner... – moi Jul 16 '17 at 17:49

2 Answers2

0

So, although I am sure, what you are doing is not the best approach, for whatever you are trying to do, here a way you get what you are looking for:

Dim a As Assembly = GetType(Duree).Assembly
Dim t As Type = a.GetType("YOUR_PROJECT_NAME.Duree+" & toto)

If Duree is part of a namespace then the string becomes "YOUR_PROJECT_NAME.NAMESPACE.Duree+" & toto.

Now t contains the type of the enum called like the text of the toto string (if there is one, if not will be Nothing).

Maybe there is a better way to achieve this, but I dont know it, since I never needed it. Maybe if you explain a little better what you are trying to do, we can help with the design, so you don't need sth like this, since it makes not a lot of sense, and that's not the way to work with enums.

PD: Don't use special chars for names. Write Duree and not Durée.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Rafa Gomez
  • 690
  • 8
  • 24
0

Instead of trying to encode this information in the type system via Enums and Constants, I think you'll find it works better if you put this it in database tables you can load when the app starts, possibly into Dictionary objects. Once you do that you can streamline the ugly reflection code into simple lookups, and also make things much easier on yourself as new phone devices are released.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794