?
Solved

Global variable passed as method parameter

Posted on 2009-12-20
4
Medium Priority
?
211 Views
Last Modified: 2012-05-08
Hello Experts :-)

I am newbie to C#, I have Delphi background so my approach is kind of Delphi style and maybe this is wrong for C#...

Anyway, here comes my question:

I want to create my own enum variable to be used across the whole application.

I created new static class, inside this class I created my enum variable.

I can use this variable inside other classes which is fine but I would also like to use it as a method parameter.

Compiler does not allow me to do it saying that this is a field not a variable.

How can I use my global field as variable then?

Thank you

panJacek
0
Comment
Question by:panJacek
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Reza Rad
ID: 26090257
bring sample code of what you want to do here
0
 
LVL 11

Accepted Solution

by:
lenordiste earned 1000 total points
ID: 26090306
all you really need is to have a public enum, so don't need to have a static class or anything similar.

Here is all you need to be able to use your enum everywhere (as well as passing it as a parameter):
namespace MyAppNamespace
{  
 public enum Fruit : int
    {
        apple=1,
        pear=2,
        orange=3
    }
}

you can then use your enum like so:
namespace MyAppNamespace
{

    public  class Example
    {

        public string EnumAsParamExample(Fruit aFruit)
        {
            return aFruit.ToString();
        }
        private string DoSomethingWithEnum()
        {
           return EnumAsParamExample(Fruit.apple);
        }

    }
}

Hope this helps!

0
 

Author Comment

by:panJacek
ID: 26090309


public static class GlobalVariables
{
  public enum XYZ
  {
    x, y, z
  }
}

public class AnotherClass
{
  public AnotherClass(XYZ aParam)
  {
  }
}
   

It will not compile.

panJacek
0
 
LVL 30

Assisted Solution

by:Reza Rad
Reza Rad earned 1000 total points
ID: 26090316
you don't need to define your enum in a class at all, enum is a structure itself
use it like this:

public enum XYZ
{
x,y,z
}

public class AnotherClass
{
   public AnotherClass(XYZ aparam)
   {
   }
}


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

809 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