Is it possible to name a variable using a method in c#?
I'm working on a project inspired by Python's argparse module that basically enables you to parse command-line arguments like the python module does. But i need to name a variable using a function for example:
NameVariable(name: "testVariable", type: string, value: "I am a string!");
Console.WriteLine(testVariable);
Output would be: I am a string!
Is there a way to do this if so
please let me know thanks.
c# variables
add a comment |
I'm working on a project inspired by Python's argparse module that basically enables you to parse command-line arguments like the python module does. But i need to name a variable using a function for example:
NameVariable(name: "testVariable", type: string, value: "I am a string!");
Console.WriteLine(testVariable);
Output would be: I am a string!
Is there a way to do this if so
please let me know thanks.
c# variables
3
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
You could also easily create aVariable<T>
class, where T would represent the return type, and you could also have astring
field for the "name". However, I'm curious as to why you want to do this.
– Frontear
Nov 11 '18 at 22:30
add a comment |
I'm working on a project inspired by Python's argparse module that basically enables you to parse command-line arguments like the python module does. But i need to name a variable using a function for example:
NameVariable(name: "testVariable", type: string, value: "I am a string!");
Console.WriteLine(testVariable);
Output would be: I am a string!
Is there a way to do this if so
please let me know thanks.
c# variables
I'm working on a project inspired by Python's argparse module that basically enables you to parse command-line arguments like the python module does. But i need to name a variable using a function for example:
NameVariable(name: "testVariable", type: string, value: "I am a string!");
Console.WriteLine(testVariable);
Output would be: I am a string!
Is there a way to do this if so
please let me know thanks.
c# variables
c# variables
asked Nov 11 '18 at 22:15
RanOutOfQuestions
22
22
3
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
You could also easily create aVariable<T>
class, where T would represent the return type, and you could also have astring
field for the "name". However, I'm curious as to why you want to do this.
– Frontear
Nov 11 '18 at 22:30
add a comment |
3
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
You could also easily create aVariable<T>
class, where T would represent the return type, and you could also have astring
field for the "name". However, I'm curious as to why you want to do this.
– Frontear
Nov 11 '18 at 22:30
3
3
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
You could also easily create a
Variable<T>
class, where T would represent the return type, and you could also have a string
field for the "name". However, I'm curious as to why you want to do this.– Frontear
Nov 11 '18 at 22:30
You could also easily create a
Variable<T>
class, where T would represent the return type, and you could also have a string
field for the "name". However, I'm curious as to why you want to do this.– Frontear
Nov 11 '18 at 22:30
add a comment |
1 Answer
1
active
oldest
votes
What you will need to use to do this is the System.Dynamic.ExpandoObject class.
using System;
using System.Dynamic;
using System.Collections.Generic;
public class Program
public static void Main()
var args = "--Bar --Foo MyStuff".Split();
var parsedArgs = ParseArgs(args);
Console.WriteLine(parsedArgs.Foo); //Writes "MyStuff"
Console.WriteLine(parsedArgs.Bar); //Writes true;
Console.WriteLine(parsedArgs.NotDefined); //Throws run time exception.
public static dynamic ParseArgs(string args)
IDictionary<string,object> result = new ExpandoObject();
//Very basic implementation
for(int i = 0; i < args.Length; i++)
if(args[i].StartsWith("--"))
if(i+1 < args.Length && !args[i+1].StartsWith("--"))
result.Add(args[i].Substring(2), args[i+1]);
else
result.Add(args[i].Substring(2), true);
return result;
Run Example
Also here is a tutorial on dynamic objects, it is about making .net work with python code which you may find interesting.
Then evenExpandoObject
can as well be used I believe
– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253783%2fis-it-possible-to-name-a-variable-using-a-method-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you will need to use to do this is the System.Dynamic.ExpandoObject class.
using System;
using System.Dynamic;
using System.Collections.Generic;
public class Program
public static void Main()
var args = "--Bar --Foo MyStuff".Split();
var parsedArgs = ParseArgs(args);
Console.WriteLine(parsedArgs.Foo); //Writes "MyStuff"
Console.WriteLine(parsedArgs.Bar); //Writes true;
Console.WriteLine(parsedArgs.NotDefined); //Throws run time exception.
public static dynamic ParseArgs(string args)
IDictionary<string,object> result = new ExpandoObject();
//Very basic implementation
for(int i = 0; i < args.Length; i++)
if(args[i].StartsWith("--"))
if(i+1 < args.Length && !args[i+1].StartsWith("--"))
result.Add(args[i].Substring(2), args[i+1]);
else
result.Add(args[i].Substring(2), true);
return result;
Run Example
Also here is a tutorial on dynamic objects, it is about making .net work with python code which you may find interesting.
Then evenExpandoObject
can as well be used I believe
– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
|
show 2 more comments
What you will need to use to do this is the System.Dynamic.ExpandoObject class.
using System;
using System.Dynamic;
using System.Collections.Generic;
public class Program
public static void Main()
var args = "--Bar --Foo MyStuff".Split();
var parsedArgs = ParseArgs(args);
Console.WriteLine(parsedArgs.Foo); //Writes "MyStuff"
Console.WriteLine(parsedArgs.Bar); //Writes true;
Console.WriteLine(parsedArgs.NotDefined); //Throws run time exception.
public static dynamic ParseArgs(string args)
IDictionary<string,object> result = new ExpandoObject();
//Very basic implementation
for(int i = 0; i < args.Length; i++)
if(args[i].StartsWith("--"))
if(i+1 < args.Length && !args[i+1].StartsWith("--"))
result.Add(args[i].Substring(2), args[i+1]);
else
result.Add(args[i].Substring(2), true);
return result;
Run Example
Also here is a tutorial on dynamic objects, it is about making .net work with python code which you may find interesting.
Then evenExpandoObject
can as well be used I believe
– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
|
show 2 more comments
What you will need to use to do this is the System.Dynamic.ExpandoObject class.
using System;
using System.Dynamic;
using System.Collections.Generic;
public class Program
public static void Main()
var args = "--Bar --Foo MyStuff".Split();
var parsedArgs = ParseArgs(args);
Console.WriteLine(parsedArgs.Foo); //Writes "MyStuff"
Console.WriteLine(parsedArgs.Bar); //Writes true;
Console.WriteLine(parsedArgs.NotDefined); //Throws run time exception.
public static dynamic ParseArgs(string args)
IDictionary<string,object> result = new ExpandoObject();
//Very basic implementation
for(int i = 0; i < args.Length; i++)
if(args[i].StartsWith("--"))
if(i+1 < args.Length && !args[i+1].StartsWith("--"))
result.Add(args[i].Substring(2), args[i+1]);
else
result.Add(args[i].Substring(2), true);
return result;
Run Example
Also here is a tutorial on dynamic objects, it is about making .net work with python code which you may find interesting.
What you will need to use to do this is the System.Dynamic.ExpandoObject class.
using System;
using System.Dynamic;
using System.Collections.Generic;
public class Program
public static void Main()
var args = "--Bar --Foo MyStuff".Split();
var parsedArgs = ParseArgs(args);
Console.WriteLine(parsedArgs.Foo); //Writes "MyStuff"
Console.WriteLine(parsedArgs.Bar); //Writes true;
Console.WriteLine(parsedArgs.NotDefined); //Throws run time exception.
public static dynamic ParseArgs(string args)
IDictionary<string,object> result = new ExpandoObject();
//Very basic implementation
for(int i = 0; i < args.Length; i++)
if(args[i].StartsWith("--"))
if(i+1 < args.Length && !args[i+1].StartsWith("--"))
result.Add(args[i].Substring(2), args[i+1]);
else
result.Add(args[i].Substring(2), true);
return result;
Run Example
Also here is a tutorial on dynamic objects, it is about making .net work with python code which you may find interesting.
edited Nov 11 '18 at 22:52
answered Nov 11 '18 at 22:22
Scott Chamberlain
97.8k24179318
97.8k24179318
Then evenExpandoObject
can as well be used I believe
– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
|
show 2 more comments
Then evenExpandoObject
can as well be used I believe
– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
Then even
ExpandoObject
can as well be used I believe– Rahul
Nov 11 '18 at 22:33
Then even
ExpandoObject
can as well be used I believe– Rahul
Nov 11 '18 at 22:33
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
@Rahul Good point, changed my answer over to use that.
– Scott Chamberlain
Nov 11 '18 at 22:52
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Thank you sooooo much, i really needed this, appreciate it.
– RanOutOfQuestions
Nov 12 '18 at 8:22
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
Why so many down votes?? ;(
– RanOutOfQuestions
Nov 12 '18 at 9:26
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
@RanOutOfQuestions It seems like a classic XY question: you're asking for help with your solution rather than asking how to solve your problem. If you describe your problem, you may find a better way to solve it.
– Gabriel Luci
Nov 13 '18 at 16:24
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253783%2fis-it-possible-to-name-a-variable-using-a-method-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Why? What problem are you trying to solve with this method?
– Gabriel Luci
Nov 11 '18 at 22:29
You could also easily create a
Variable<T>
class, where T would represent the return type, and you could also have astring
field for the "name". However, I'm curious as to why you want to do this.– Frontear
Nov 11 '18 at 22:30