Solved

C#: dimensional constant

Posted on 2008-10-16
12
266 Views
Last Modified: 2013-12-17
Hi,
I am looking for a way of storing information in a public constant. The information consist of three values, let say height, width and depth. I though about a delimited string but I want a more maintainable solution. Can some one help?
0
Comment
Question by:karakav
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 22729710
use collection or array to store multiple values,
u can also use List, Dictionary.
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22729788
Why dont you create a structure or class to store the data

  struct Position
    {
        int Height;
        int Width;
        int Depth;
    }

or

 class Position
    {
        public int Height{ get; set; }
        public int Width { get; set; }
        public int Depth { get; set; }
    }
0
 
LVL 6

Assisted Solution

by:openshac
openshac earned 100 total points
ID: 22729808
do something like the .Net implementation of Size...
[Serializable, StructLayout(LayoutKind.Sequential), TypeConverter(typeof(SizeConverter)), ComVisible(true)]
public struct Size
{
    public static readonly Size Empty;
    private int width;
    private int height;
    public Size(Point pt)
    {
        this.width = pt.X;
        this.height = pt.Y;
    }
 
    public Size(int width, int height)
    {
        this.width = width;
        this.height = height;
    }
 
 

Open in new window

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 6

Expert Comment

by:openshac
ID: 22729811
you probably don't need the first line
0
 
LVL 4

Author Comment

by:karakav
ID: 22729814
But I need a CONSTANT. Classes, structs and Arrays are discarded in this case.
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22729855
You cant have a struct as a const, one way would be..


public struct MyStruct
        {
            public readonly int depth;
            public MyStruct(int x)
            {
                this.depth = x;
            }

        }

        public static readonly MyStruct MyConstantStruct = new MyStruct(100);
0
 
LVL 13

Accepted Solution

by:
crazyman earned 200 total points
ID: 22729865

public struct MyStruct
        {
            private int depth;
            public int Depth
            {
                get { return depth; }
            }
            public MyStruct(int x)
            {
                this.depth = x;
            }
 
        }
 
        public static readonly MyStruct MyConstantStruct = new MyStruct(100);

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22729976
Anyway, I don't want to use Structures.
0
 
LVL 6

Expert Comment

by:openshac
ID: 22730025
you said you didn't want to use "a delimited string" and wanted a "more maintainable solution".

I suggest you use a struct and declare a constant of type say MySize
0
 
LVL 13

Expert Comment

by:crazyman
ID: 22730614
Well if you dont want a struct or a string why not use

public const int Height = 100;
public const int Width = 100;
public const int Depth = 100;
0
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 200 total points
ID: 22733183
You can use a class with GET ONLY properties, then it would behave as constant data which could be initialized only during instantiation


class Position
   {
        private int m_height;
        private int m_width;
        private int m_depth;

        public Position(int height, int width, int depth)
        {
            m_height = height;
            m_depth = depth;
            m_width = width;
        }
       public int Height
       {
           get{
               return m_height;
        }
       }
       public int Width
       {
           get
           {
               return m_width;
           }
       }
       public int Depth
       {
           get
           {
               return m_depth;
           }
       }
   }

0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31506668
I guess I don't have other choice than Structures after all. Thanks any way.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question