Is it possible to name a variable using a method in c#?










-4














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.










share|improve this question

















  • 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 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
















-4














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.










share|improve this question

















  • 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 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














-4












-4








-4







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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













  • 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 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








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













1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer






















  • 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










  • 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










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
);



);













draft saved

draft discarded


















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









0














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.






share|improve this answer






















  • 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










  • 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















0














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.






share|improve this answer






















  • 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










  • 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













0












0








0






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 '18 at 22:52

























answered Nov 11 '18 at 22:22









Scott Chamberlain

97.8k24179318




97.8k24179318











  • 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










  • 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










  • @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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo