Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C#: dimensional constant

Posted on 2008-10-16
12
Medium Priority
?
275 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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