Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
Would be very grateful if you help me. I have class named config that have private string variable named param.

I need to get from config class param variable sometimes as int type sometimes as bool type or string.

As I understand I need create 3 properties in config class,each property have to convert type, as follow:

the first property converts string to int, the second converts string to bool, the third property gets me the string value.

The class should look something like this.
C#
class Config
{
    private string param; 

    public int
    {
       get 
      { 
        return int.parse(param); 
      }
    }

    public bool
    {
       get 
      { 
        return bool.tryparse(param); 
      }
    }

   public string
    {
       get 
      { 
         return param; 
      }
    }
}

But I dont know how can those properties be used in accordance to the variable type that I want to get out of class.

I hope you can help me!! Thank you in advance!
Posted
Comments
pradiprenushe 10-Jul-12 6:08am    
Why you are using single variable. It will make you tightly coupled.
Mich_90 10-Jul-12 10:09am    
Because the class initialized with one value from DB.:)

1 solution

Even if the request is a kind of strange, this should be the solution (even if it is conceptually wrong):

C#
public class Config
{
    private string param;

    public bool MyBool
    {
        get { return bool.Parse(param); }
    }

    public int MyInt
    {
        get { return int.Parse(param); }
    }

    public string My
    {
        get { return param; }
    }
}


Then use it simply as:

C#
Config c = new Config();

int i = c.MyInt;


Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900