?
Solved

C#: dimensional constant

Posted on 2008-10-16
12
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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
Stressed Out?

Watch some penguins on the livecam!

 
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 600 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 600 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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