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 9136587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:20+00:00 2026-06-17T08:58:20+00:00

I need to declare a variable with a name that matches a string. This

  • 0

I need to declare a variable with a name that matches a string.

This is what I need to work, but in reality it compiles with a new expression instance with the name "s" (I need it to take the name that is stored in the string s)
How can I solve this?

Code:

foreach (string s in StringArray)
            {
                if (SomeDifferentArray.Contains(s) != true)
                {
                    Expression s = new Expression(); 
                }
            }

I need a new instance of expression for each string in StringArray, and each of those expressions needs to be named according to the literal that the string holds.

  • 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-17T08:58:21+00:00Added an answer on June 17, 2026 at 8:58 am

    This seems like a use case for a mapping collection. I’m not a C# user myself but I suspect the dictionary class would be the one to go to:

    http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

    C# is a compiled language, so you can’t create compile-time variables that are dependent on run-time data. However, you can use a map-style data structure to associate a unique key object with a single value object. In your case, you can use it to associate each string with a single Expression.

    Dictionary<string, Expression> myExpressionDict = new Dictionary<string, Expression>();
    
    foreach(string s in StringArray) {
        if(!SomeDifferentArray.Contains(s)) {
            myExpressionDict[s] = new Expression();
        }
    }
    

    After this loop finishes, for any string s in your original SomeArray, you can do myExpressionDict[s] to get its associated Expression object.

    In C# this structure is called a “dictionary.” This same data struture may also go by the names “map” or “hash,” depending on your programming language or library. For example, in the Java collections framework it’s referred to as Map, in Ruby they use the term hash, and in Python it goes by the name dictionary (or dict for short).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to declare 3d array variable but can't. int[][][] ary = new int[5][2][];
I have a clojure application that need some configuration variable, mainly string but also
Variable names in haskell need to be in small case, but How to declare
I want to declare static(or not static) variable inside Enum. I need this because
I have a C# function that look like this: bool func(string name, bool retry)
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have a vector declared as a global variable that I need to be
I need to declare an enum variable as a class member and need to
This is homework, but I need a nudge. I can't find how to sort
consider this # global variable i_am_global = {} def get_global(): global i_am_global # declare

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.