locked
passing parameter to a constructor that takes 1 or x values RRS feed

  • Question

  • Hi i want to create an object that will hold a different value depending on what parameter is passed to it, I have thought of the following ways is there any better solutions

    solution 1) simply use an int - problem will have to know what each value means which is not self explaned.

    solution 2) create a seperate class which have static methods which return different versions of that class. problem - seems overly complicated for this problem plus the solution itself contains this very problem as the statically constructed class will need to have an identity e.g. enum

    so basically all I want if for my created object to hold a value which identifies what created it (there are so far 3 methods to create it) I could also just use a string but feel that this isnt perfect do you have any advice on what would be best? most elegent? MS seems to do something similar to solution 2 e.g. with a message box the icon is defined by MessageBoxIcon.Exclamation for eample.
    Monday, August 24, 2009 11:17 AM

Answers

  • Hi,

    You could create a class as follows :-

    	public class MyClass
    	{
    		private ConstructionTypes _currentConstructionType = ConstructionTypes.Type1;
    
    		public enum ConstructionTypes : int
    		{
    			Type1,
    			Type2,
    			Type3,
    		}
    
    		public ConstructionTypes CurrentConstructionType
    		{
    			get
    			{
    				return _currentConstructionType;
    			}
    		}
    
    		public MyClass(ConstructionTypes currentConstructionType)
    		{
    			_currentConstructionType = currentConstructionType;
    		}
    	}

    And use it like this (this code was being used from another class) :-

    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type1);
    
    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type2);
    
    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type3);
    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by badasschris Monday, August 24, 2009 11:42 AM
    Monday, August 24, 2009 11:40 AM

All replies

  • Hi,

    Could you create an emuration of types and pass the enumeration value into the constructor? Store this enumeration value in a variable of the class?

    Thanks

    Paul Diston


    http://www.smartmobiledevice.co.uk/
    Monday, August 24, 2009 11:26 AM
  • That is what I wanted to do but wasnt exaclty sure how to go about doing this, would I have to declare the enumeration in every class that uses this object and within the object itself or is there a way to make the enumeration global?
    • Edited by badasschris Monday, August 24, 2009 11:32 AM
    Monday, August 24, 2009 11:31 AM
  • Hi,

    You could create a class as follows :-

    	public class MyClass
    	{
    		private ConstructionTypes _currentConstructionType = ConstructionTypes.Type1;
    
    		public enum ConstructionTypes : int
    		{
    			Type1,
    			Type2,
    			Type3,
    		}
    
    		public ConstructionTypes CurrentConstructionType
    		{
    			get
    			{
    				return _currentConstructionType;
    			}
    		}
    
    		public MyClass(ConstructionTypes currentConstructionType)
    		{
    			_currentConstructionType = currentConstructionType;
    		}
    	}

    And use it like this (this code was being used from another class) :-

    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type1);
    
    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type2);
    
    MyClass myNewClass = new MyClass(MyClass.ConstructionTypes.Type3);
    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    • Marked as answer by badasschris Monday, August 24, 2009 11:42 AM
    Monday, August 24, 2009 11:40 AM
  • Thats absolutely perfect thanks again Paul, if only I had you hovering over my shoulder I would get this program done in half the time.
    Monday, August 24, 2009 11:42 AM