Solved

C#: dimensional constant

Posted on 2008-10-16
12
262 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now