Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9310741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:11:27+00:00 2026-06-19T01:11:27+00:00

How can I set defined values for a parameter? I mean, for example, I

  • 0

How can I set defined values for a parameter? I mean, for example, I have the following function:

int functionA(string parameterA) {}

Now, when I call this function and input value for the parameterA, I want the value to be in the value set I have defined. For example, the defined value set is:

string[] definedParameterA = { "Hello World", "stackoverflow" };

the value of parameterA I input must be “Hello World” or “stackoverflow”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:11:29+00:00Added an answer on June 19, 2026 at 1:11 am

    If you really want it to be a string, you’d just validate it at the start of the method:

    private static readonly String[] ValidValues = {"Hello World", "stackoverflow"};
    
    public void DoSomething(string text)
    {
        if (!ValidValues.Contains(text))
        {
            throw new ArgumentException("Invalid value: " + text, "text");
        }
        // Carry on
    }
    

    However, you might also consider using an enum instead. You still need to validate that the enum value is defined, but it makes it simpler for the caller to get it right:

    public enum Message
    {
        HelloWorld,
        StackOverflow
    }
    
    public void DoSomething(Message message)
    {
        if (!Enum.IsDefined(typeof(Enum), message))
        {
            throw new ArgumentException("Invalid value: " + message, "message");
        }
        // Carry on
    }
    

    You can have a mapping from the enum value to string elsewhere if you want, of course.

    Note that the above code will box message on every call. You could avoid that (and make it more typesafe) using my Unconstrained Melody project if you really want, but it’s probably not worth it until you’ve proved that this boxing is actually an issue. The code would then look like this:

    public void DoSomething(Message message)
    {
        if (!message.IsNamedValue()) // Extension method
        {
            throw new ArgumentException("Invalid value: " + message, "message");
        }
        // Carry on
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if I can set a pre defined parameter value in PHP
How can I set attribute to MVC2 user control defined in single file with
When we define a property like public string Name {get; set;} dot net can
I can set the relationship between View Model and view through following DataContext syntax:
Is there any way to have a user defined parameter in a file and
Explanation I have a function that is passed the parameter options . This parameter
How I Can pass multiple values in a single parameter to a stored procedure
Can you please tell me how can I set a global #define in my
I am trying to define a set of functions where I can pass in
I can set the width and height of my canvas by setting the width

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.