Link to home
Avatar of QPR
QPRFlag for New Zealand

asked on

Create object in switch statement

Based on a case I need to call a given web service reference.
I'm having trouble working out how to create an object from a given type based on this.
If I try to create it in each case then I get that the local variable "client" already exists in this scope.
Another error is that when I refer to "client" outside of the switch it says it does not exist.

switch (appType)
            {
                case "bcapps":
                    ListName = "";
                    A_BC_WS_Lists.Lists client = new A_BC_WS_Lists.Lists();
                    break;
                case "ehlthapps":
                    ListName = "";
                    B_BC_WS_Lists.Lists client = new B_BC_WS_Lists.Lists();
                    break;
                case "luc":
                    ListName = "";
                    break;
                case "Sub":
                    ListName = "";
                    break;
            }

How can I dynamically create the object based on the value of the case.
How can I refer to this if it is created within the switch.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Do the classes share a common base class, or might they implement any common interfaces? Did you create these classes yourself, or were they auto-generated for you?
Avatar of QPR

ASKER

They are web service references (to call a web service)
So based on the value of a variable I need to create client like this

A_BC_WS_Lists.Lists client = new A_BC_WS_Lists.Lists();

The image shows my list of web service refs

User generated image
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of QPR

ASKER

Thanks ill digest and test this when back at work tomorrow
Avatar of QPR

ASKER

Thanks