views:

184

answers:

5

Hi all,
I recently created a class which has a constructor taking 3 enumerations as arguments. These enumerations are defined in the object itself as ObjectEnum and AnotherObjectEnum in the example below.

LongObjectName pt = new LongObjectName(
            LongObjectName.ObjectEnum.EnumerationOne,
            LongObjectName.ObjectEnum.EnumerationTwo,
            LongObjectName.AnotherObjectEnum.EnumerationThree,
            0.0);

I have to initialize 8 of these objects and I'd like to condense this into a clearer format for future coders. I'd like to structure this code so that I can simplify the declaration of this object - something like a "with" from VB.NET. I would have to implement the IDisposable interface in order to use the "using" command.

ideally I'd like my code to look like this:

LongObjectName pt = new LongObjectName( 
                ObjectEnum.EnumerationOne,
                ObjectEnum.EnumerationTwo,
                AnotherObjectEnum.EnumerationThree,
                0.0);

Is there any simple way to clean up code like this? Thanks in advance - this is my first question so constructive criticism is appreciated.

+8  A: 

There is a simple way: Simply don't declare your enums within the class but in the same namespace of the class.

using won't help you here, by the way, as that does nothing else than wrap a try/finally around your code and call Dispose() in the finally block. It's by no means similar to VB's With.

ETA: Since you chose to accept this answer, even in the light of better ones:

You can use the using directive to create an alias for your enum:

using ObjectEnum = SomeNameSpace.LongObjectName.ObjectEnum;

Please give an upvote to SLaks and not me, since s?he was the first to come up with this idea here.

Joey
Thanks for the credit.
SLaks
do I then have to "downvote" you to be able to upvote Slaks? Both of your answers were helpful - his was "instructive", but yours also gave me the understanding of what I was doing wrong in order to correct it (declare your enums not in the class but within the namespace of the class).
CrimsonX
No. You can upvote as many answers as you like by clicking the up arrow. You can only accept (check mark) one answer, and you should accept whichever one you feel is most helpful.
SLaks
SLaks: You're welcome and would deserve the accepted answer more than me, imho.
Joey
+10  A: 

You are confusing the two uses of the using keyword.

You can use the using directive to make an alias for any type; this has nothing to do with IDisposable.

For example:

using ObjectEnum = YourNamespace.LongObjectName.ObjectEnum;

(Note that all types in using directives must be fully qualified with the namespace name.


The using statement has nothing to do with the using directive except the name, and is used to dispose an IDisposable.

For example:

using (TransactionScope.BeginTransaction())
using (dataSet)
using (new SqlCommand("SQL", connection) {
    //Do something useful
}
SLaks
+1  A: 

First things first you can do this:

var pt = new LongObjectName( 
                ObjectEnum.EnumerationOne,
                ObjectEnum.EnumerationTwo,
                AnotherObjectEnum.EnumerationThree,
                0.0);

Second, you can consider moving the enum out of the class definition or create an alias for the enum in your callers class file.

using ObjectEnum = LongObjectName.ObjectEnum ;
ChaosPandion
FYI - MSDN suggested that the var type should only be used if you are storing an anonymous type. Also, I believe your "using" doesn't work per the earlier poster's description of using (and the fact that my enumeration doesn't implement the iDisposable interface)
CrimsonX
@CrimsonX: You're confusing the two uses of the `using` keyword. See my first answer.
SLaks
+4  A: 

Constructive Critisicism

If you really want constructive criticism, run FxCop on your code, or Code Analysis if you have Visual Studio Team Suite.

It will generate lots of warnings for code that violates best practices. The warnings can be daunting for large projects, but if you approach them one set at a time, you can trim it down to a more reasonable number. FxCop can be quite useful; it has found several bugs in my own code that would otherwise have gone unnoticed.


In your case, avoid public nested types. Unless there is a compelling reason not to, you shouldn't put those enums inside the class.

SLaks
Thanks for the advice. I'll see if we can run this for the future and I have checked out the recommendation regarding public nested types. I appreciate it!
CrimsonX
+2  A: 
  1. Welcome to SO, CrimsonX.
  2. C#'s "using" statement is not analogous to VB's "With". The "using" statement sets the scope of the object before it is disposed, but it does not initialize object properties.
  3. You might consider creating static factory methods that create instances using common options, such as "Schedule.NewScheduleSevenDaysAWeek()" and "Schedule.NewScheduleWeekdaysOnly()".
  4. It looks to me like you are simply trying to save keystrokes. If that is the case, Johanness' suggestion of moving the enums out of the class will help. In general, however, I think you will find C# more verbose than VB. For example, C#'s object initializer is intended to be a cleaner way of instantiating objects and setting properties, but it is not shorter. Cleaner != shorter.
flipdoubt