Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to set an image size mode, so I create enum with three modes, I need to choose the size mode then send it to property and set it, how can I do it?

I know this code is wrong but this is the idea I need.

What I have tried:

// in windows application
C#
enum sizeMode
    {
        Stretch,Normal,Fit
    };
 private void stretchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sameehViewer.SizeMode = sizeMode.Stretch;
        }

// in library control
C#
enum _sizeMode;
  public enum SizeMode
  {
      set
      {
          _sizeMode = value;
          SetSizeMode();
      }
      get
      {
          return _sizeMode;
      }
  }
Posted
Updated 23-Nov-20 3:19am
v3

1 solution

I suggest you define the Enum as
public enum SizeMode
{
    Stretch,Normal,Fit
}
Assuming 'sameehViewer is an instance of a Class defined in your library:

1) If you define the Enum in the app: the Library/Class is going to need a reference to the app the Enum is defined in: that creates an undesirable dependency, and exposes the app to possible unwanted changes.

2) This is very confused code:
enum _sizeMode;
public enum SizeMode // compile error here
{
  set
  {
      _sizeMode = value;
      SetSizeMode(); // logic error here: how will the method know the setting ?
  }
  get
  {
      return _sizeMode;
  }
}
3) Consider defining the Enum in the library with access set to public: then, when your app has a valid reference to an instance of the library, it can select one of the Enum values to pass.
namespace ViewerLibrary
{
    public enum SizeMode
    {
        Stretch, Normal, Fit
    }

    public class Viewer
    {
        public SizeMode viewSizeMode { set; get; } = SizeMode.Normal;

        public void SetSizeMode(SizeMode szMode)
        {
            viewSizeMode = szMode;
        }
    }
}
Note the use of a Method to set the Property value: having setters or getters in a Property cause side-effects is a bad code practice..

4)After you add a reference to the Viewer dll to the app: in the app main form you can then create an instance of the Viewer, and do whatever:
using ViewerLibrary;

namespace TheApp
{
    private Viewer viewer = new Viewer();
    
    private void Form1_Load(object sender, EventArgs e)
    {
        viewer.SetSizeMode(SizeMode.Stretch);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900