Click here to Skip to main content
15,896,154 members
Home / Discussions / C#
   

C#

 
QuestionHow to get an Not Empty Instance back ? Pin
Abdalla Ben Omran26-Apr-19 2:42
Abdalla Ben Omran26-Apr-19 2:42 
AnswerRe: How to get an Not Empty Instance back ? Pin
Richard Deeming26-Apr-19 3:14
mveRichard Deeming26-Apr-19 3:14 
QuestionWhat is Out in C# ? Pin
Abdalla Ben Omran25-Apr-19 23:13
Abdalla Ben Omran25-Apr-19 23:13 
AnswerRe: What is Out in C# ? Pin
OriginalGriff25-Apr-19 23:37
mveOriginalGriff25-Apr-19 23:37 
GeneralRe: What is Out in C# ? Pin
Abdalla Ben Omran25-Apr-19 23:50
Abdalla Ben Omran25-Apr-19 23:50 
GeneralRe: What is Out in C# ? Pin
OriginalGriff26-Apr-19 0:03
mveOriginalGriff26-Apr-19 0:03 
QuestionI want to generate/make my predefined variable name in the C# code Pin
Member 1330748425-Apr-19 5:19
Member 1330748425-Apr-19 5:19 
AnswerRe: I want to generate/make my predefined variable name in the C# code Pin
Richard Deeming25-Apr-19 5:36
mveRichard Deeming25-Apr-19 5:36 
You can't do that for local variables. The variables names are thrown away when you compile your code, so there's no way to reference them at run-time.

For fields or properties, you could use reflection[^]. It won't be pretty, and it won't be fast, but it should work.
C#
class Foo
{
    private string x1 = "Hello";
    private string x27 = "Hi";
    private string x99 = "Hey";
    
    public string GetValue(int i)
    {
        string name = "x" + i;
        FieldInfo field = typeof(Foo).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
        if (field == null) throw new ArgumentException("Unknown index: " + i, nameof(i));
        return (string)field.GetValue(this);
    }
}

Alternatively, put your values into a Dictionary<TKey,TValue>[^]:
C#
Dictionary<int, string> x = new Dictionary<int, string>
{
    [1] = "Hello",
    [27] = "Hi",
    [99] = "Hey",
};

int i = 27;
string value = x[i];




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: I want to generate/make my predefined variable name in the C# code Pin
#realJSOP25-Apr-19 9:18
professional#realJSOP25-Apr-19 9:18 
AnswerRe: I want to generate/make my predefined variable name in the C# code Pin
OriginalGriff25-Apr-19 6:12
mveOriginalGriff25-Apr-19 6:12 
AnswerRe: I want to generate/make my predefined variable name in the C# code Pin
#realJSOP25-Apr-19 9:23
professional#realJSOP25-Apr-19 9:23 
GeneralRe: I want to generate/make my predefined variable name in the C# code Pin
Richard Deeming25-Apr-19 11:24
mveRichard Deeming25-Apr-19 11:24 
GeneralRe: I want to generate/make my predefined variable name in the C# code Pin
#realJSOP25-Apr-19 23:40
professional#realJSOP25-Apr-19 23:40 
QuestionAdventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218425-Apr-19 3:55
Member 1412218425-Apr-19 3:55 
AnswerRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Ron Nicholson25-Apr-19 5:34
professionalRon Nicholson25-Apr-19 5:34 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218425-Apr-19 10:36
Member 1412218425-Apr-19 10:36 
AnswerRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
OriginalGriff25-Apr-19 6:17
mveOriginalGriff25-Apr-19 6:17 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218425-Apr-19 10:44
Member 1412218425-Apr-19 10:44 
AnswerRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
V.25-Apr-19 19:02
professionalV.25-Apr-19 19:02 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218425-Apr-19 21:16
Member 1412218425-Apr-19 21:16 
AnswerRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Ron Nicholson26-Apr-19 5:13
professionalRon Nicholson26-Apr-19 5:13 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218426-Apr-19 21:43
Member 1412218426-Apr-19 21:43 
AnswerRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
BillWoodruff29-Apr-19 8:23
professionalBillWoodruff29-Apr-19 8:23 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
Member 1412218429-Apr-19 10:40
Member 1412218429-Apr-19 10:40 
GeneralRe: Adventure Game & Interactive Fiction - Coding for handling doors. Pin
BillWoodruff29-Apr-19 22:43
professionalBillWoodruff29-Apr-19 22:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.