Solved

C#: dimensional constant

Posted on 2008-10-16
12
265 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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