0

I have an existing method which essentially hardcodes an Enum value into it.

What I want to do, however, is to pass an Enum value to the method. Then, substitute the passed in value for the hardcoded field. The value that I want to substitute is: ImpactType.Item3ModerateLimited (at the end of the code).

Way method is right now:

strRemedyTktResponse = IssueRemedyTicket(sb1.ToString());

private string IssueRemedyTicket(string webServiceErrInfo)
        {
            string strResponse = string.Empty;

            IncidentService.HPD_IncidentInterface_Create_WSService webService = new IncidentService.HPD_IncidentInterface_Create_WSService();

            try
            {
                webService.AuthenticationInfoValue = new IncidentService.AuthenticationInfo();
                webService.AuthenticationInfoValue.userName = "smocustomer";
                webService.AuthenticationInfoValue.password = "ryder123";
                webService.Timeout = 1000 * 60;

                strResponse = webService.HelpDesk_Submit_Service(
                     new[] { ConfigurationManager.AppSettings["AssignedGroup"].ToString() }  //Assigned_Group
                    , "" //Assigned_Group_Shift_Name
                    , "Ryder System, Inc." //Assigned_Support_Company
                    , "FMS" //Assigned_Support_Organization
                    , "" //Assignee
                    , "Software" //Categorization_Tier_1
                    , "Handheld Computer" //Categorization_Tier_2
                    , "Webservice Failure" //Categorization_Tier_3
                    , "" //CI_Name
                    , "" //Closure_Manufacturer
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier3
                    , "" //Product_Model_Version
                    , "" //Closure_Product_Name
                    , "" //Department
                    , "SMO" //First_Name
                    , ImpactType.Item3ModerateLimited

Definition of ImpactType field:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:HPD_IncidentInterface_Create_WS")]
    public enum ImpactType {

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("1-Extensive/Widespread")]
        Item1ExtensiveWidespread,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("2-Significant/Large")]
        Item2SignificantLarge,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("3-Moderate/Limited")]
        Item3ModerateLimited,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("4-Minor/Localized")]
        Item4MinorLocalized,
    }

*I was thinking about doing something like below, but am getting the following error on the "Name" property.

Cannot convert from 'string' to 'ImpactType' *

private string IssueRemedyTicket(string webServiceErrInfo, int impactType)
        {
            string strResponse = string.Empty;

            IncidentService.HPD_IncidentInterface_Create_WSService webService = new IncidentService.HPD_IncidentInterface_Create_WSService();

            try
            {
                webService.AuthenticationInfoValue = new IncidentService.AuthenticationInfo();
                webService.AuthenticationInfoValue.userName = "smocustomer";
                webService.AuthenticationInfoValue.password = "ryder123";
                webService.Timeout = 1000 * 60;

                var value = "";
                if (impactType == 2)
                    value = ImpactType.Item2SignificantLarge.ToString();
                else
                    value = ImpactType.Item3ModerateLimited.ToString();

                strResponse = webService.HelpDesk_Submit_Service(
                     new[] { ConfigurationManager.AppSettings["AssignedGroup"].ToString() }  //Assigned_Group
                    , "" //Assigned_Group_Shift_Name
                    , "Ryder System, Inc." //Assigned_Support_Company
                    , "FMS" //Assigned_Support_Organization
                    , "" //Assignee
                    , "Software" //Categorization_Tier_1
                    , "Handheld Computer" //Categorization_Tier_2
                    , "Webservice Failure" //Categorization_Tier_3
                    , "" //CI_Name
                    , "" //Closure_Manufacturer
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier1
                    , "" //Closure_Product_Category_Tier3
                    , "" //Product_Model_Version
                    , "" //Closure_Product_Name
                    , "" //Department
                    , "SMO" //First_Name
                    ,  ((XmlEnumAttribute)typeof(ImpactType)
                        .GetMember(value.ToString())[0]
                        .GetCustomAttributes(typeof(XmlEnumAttribute), false)[0]).Name
sagesky36
  • 4,542
  • 19
  • 82
  • 130
  • Maybe I'm missing something, but it isn't as simple as treating it like a normal parameter? That is: `private string IssueRemedyTicket(string webServiceErrInfo, ImpactType impactType)` and passing it into the `webService.HelpDesk_Submit_Service` call like: `, impactType`? – Chris Sinclair Jul 27 '15 at 18:54
  • 1
    If I understand correctly, are you trying to get the `XmlEnumAttribute` value? If so, you can use reflection as shown here http://stackoverflow.com/questions/18737950/get-xmlenumattribute-from-enum – keyboardP Jul 27 '15 at 18:55
  • @keyboard, I'm getting the following error when I write use that code: – sagesky36 Jul 27 '15 at 19:12
  • Arugment 18: cannot convert from 'string' to 'ImpactType' – sagesky36 Jul 27 '15 at 19:13
  • Also, is 0 the index of the item you want to get. If I want to use the 'SignificantLarge' type, I would use the index of 1? – sagesky36 Jul 27 '15 at 19:14
  • this error occurs when using the .Name property at the end of the code I used. – sagesky36 Jul 27 '15 at 19:15
  • @sagesky36 Could you edit your question and post that new piece of code please – keyboardP Jul 27 '15 at 19:16
  • @keyboard, I just edited the code. See Edit #1. – sagesky36 Jul 27 '15 at 19:19
  • @keyboard, see Edit #2 for latest..... – sagesky36 Jul 27 '15 at 19:26
  • @sagesky36 you've added "Edit" blocks with more of the same code but without any indication of what you have done differently or why. Instead, why not just have one block of the code that reflects what you have tried and what you are asking your question about? – crashmstr Jul 27 '15 at 19:28
  • @crashmstr & keyboardP, I just have one code block now for the issue. Please take a look and let me know what the simple conversion error is that I'm missing...... – sagesky36 Jul 27 '15 at 19:38
  • @keyboardP, I just have one code block now for the issue. Please take a look and let me know what the simple conversion error is that I'm missing.... – sagesky36 Jul 27 '15 at 19:38

1 Answers1

1

See https://stackoverflow.com/a/16039343/380384

you want to convert an int into an enum of ImpactType. All you need is this:

ImpactType type = (ImpactType)impactType;

or

, ...
, "SMO" //First_Name
, (ImpactType)impactType
Community
  • 1
  • 1
John Alexiou
  • 28,472
  • 11
  • 77
  • 133