Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. converting variable into the a string

converting variable into the a string

Scheduled Pinned Locked Moved C#
data-structurestutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitmohanty
    wrote on last edited by
    #1

    Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says, >>a=[1,2,3;2,3,4]; My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards Amit

    G D 2 Replies Last reply
    0
    • A amitmohanty

      Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says, >>a=[1,2,3;2,3,4]; My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards Amit

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't create variables dynamically, so there is no point of getting the name of a variable as a string. You already know the names of all your variables. What did you intend to use this for? I imagine that something like a collection would be more useful for you. --- b { font-weight: normal; }

      A 1 Reply Last reply
      0
      • G Guffa

        You can't create variables dynamically, so there is no point of getting the name of a variable as a string. You already know the names of all your variables. What did you intend to use this for? I imagine that something like a collection would be more useful for you. --- b { font-weight: normal; }

        A Offline
        A Offline
        amitmohanty
        wrote on last edited by
        #3

        thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following >> a=[1,2;2,3] Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, Amit

        A G 2 Replies Last reply
        0
        • A amitmohanty

          thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following >> a=[1,2;2,3] Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, Amit

          A Offline
          A Offline
          afinnell
          wrote on last edited by
          #4

          MATLAB most likely created their own parser that can interpret their language. Depending on how far you wish to go you could do the same thing. You'll need to create a stack that contains all of the variables and there relevant values. If you want to just assign values to names you could create a Map of all the names to their values. You would then be able to access them like so: String myVal = (String)variables["a"]; This is a simplistic point of view on the whole matter of creating a parser though. Look up some articles on Abstract Syntax Trees (AST), and some parser generators such as Lex, Bison, SableCC, and ANTLR. Hope this helps. - Drew

          1 Reply Last reply
          0
          • A amitmohanty

            thanks for your answer. but, i was wondering how MATLAB creates the variable name dynamically. if you enter something like the following >> a=[1,2;2,3] Then it creates a variable of name a and stores its value in its workspace, which you can later access it. Regards, Amit

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            A variable in the MATLAB user environment does not exist as a variable in the MATLAB application itself. Both the name and the contents of the user variable is data to the application. The variable names and the data is stored in a collection or something similar. If you want the same functionality, use a collection. --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • A amitmohanty

              Hi, Can anyone tell me how to get the name of a variable as a string. Actually I want to store the values of the varibale( my varibale is a 2D array) in a text file named the same. Like if the name of the variable is myMatrix, then it should be able to store the values of the array in myMatrix.txt One more thing I want to do is that, I want to get user input as what should be given name of the variable. So, if user says, >>a=[1,2,3;2,3,4]; My program should be able to create a matrix with variable name a and initiate it with the values given by the user. Regards Amit

              D Offline
              D Offline
              Denevers
              wrote on last edited by
              #6

              you could probably model your user variable like this public class UserVariable { private string variableName; private object variableContent; public string VariableName { get { return variableName; } set { variableName = value; } } public string VariableContent { get { return variableContent; } set { variableContent = value; } } public UserVariable(string name,object content) { this.variableName = name; this.variableContent = content; } // automatically write the content of the variable when used in // a string.Format public override string ToString() { return string.Format("{0}",variableContent); } } this could be the head of a series of specialised Variable.. just my 2 cents Denevers

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups