none
Write variable name as string

    Question

  • I want to write the names of a bunch of variables into a textfile using the streamwriter. Do you know of any possible way that one can convert variable names to strings?

    I'm looking for something like streamwriter.write(celciusTemperature.VariableNameToString()).

    Or if no built-in method exists, then any method! I can't figure out how to write a method to convert variable names to strings myself. If you can figure it out, please show me how it's done!

    Thanks!
    Wednesday, August 22, 2007 10:36 PM

Answers

  • One could apply an attribute to the the variables with the long name or any such text. When needed, simply reflect off of the attribute for the description and process it. I demonstrate such usage on my blog article entitled C# Using Extended Attribute Information on Objects.
    Thursday, August 23, 2007 1:30 AM
    Moderator
  • Convert variable name to string. Is it possible?
    Thursday, August 23, 2007 10:24 PM
  •  

    im not in the right frame of mind at this time of night so excuse the lazyness here.

     

    Why dont you make a custom class which has properties:

     

    variableName

    value

     

    so variableName will be "variable1" and value will be "3.1" or whatever....therefore you can right to the file the value of the variable property along with the value of the object via the value property

     

    This maybe one small workaround but not the best.

     

    so when you get your results from SQL, create an instance of the class and say, add it into an array or something (if this is what you want) then for each instance of that object, set the variableName property to "var1", "var2" etc... and the value property to the result of the field retrieved from SQL

     

    then you can go through the objects and write your statements to text file.

     

    Does this give you some ideas?

    Friday, August 24, 2007 2:13 AM
    Moderator

All replies

  • One could apply an attribute to the the variables with the long name or any such text. When needed, simply reflect off of the attribute for the description and process it. I demonstrate such usage on my blog article entitled C# Using Extended Attribute Information on Objects.
    Thursday, August 23, 2007 1:30 AM
    Moderator
  • Convert variable name to string. Is it possible?
    Thursday, August 23, 2007 10:24 PM
  • a variable name technically is a string

     

    can you explain further on what you want to do and any relevent code samples?

     

    do you mean:

     

    MyClass class1 = new MyClass();

     

    MessageBox.Show(class1.ToString());

     

    where you want to show "class1" instead of the Type of class as a string representable format?

     

    Thursday, August 23, 2007 10:31 PM
    Moderator
  • Yes, you have described it well. That is exactly what I want!
    Thursday, August 23, 2007 11:00 PM
  • I don't think that is possible I'm afraid

     

    why do you want to do this?

     

    Thursday, August 23, 2007 11:06 PM
    Moderator
  •  ahmedilyas wrote:

    I don't think that is possible I'm afraid

     

    why do you want to do this?

     




    I'll try to explain. Maybe you can even come up with an alternate solution.

    I'm preforming a bunch of queries on a database. The queries are done with different values to a set of variables. The results of these queries will be a long list of rules that I want to output to a text file. This set of rules will look like:

    if (variable1 == 3 AND variable2 = 4 AND variable3 = "ased")
    {
    //this applies
    }
    else if (variable1 == 3.1 AND variable2 = 4.1 AND variable3 = "asedf")
    {
    //this applies
    }
    ... and so on.

    I'm having trouble writing this to a file."if(", "else if(", ")", "{", "}", "==" and variable values are easy to write to a textfile. But I don't have a good solution for writing the variable names (variable1, variable2, variable3...).

    If you have any idea on how to solve this, please show me how! Thanks trying to help!
    Friday, August 24, 2007 2:06 AM
  •  

    im not in the right frame of mind at this time of night so excuse the lazyness here.

     

    Why dont you make a custom class which has properties:

     

    variableName

    value

     

    so variableName will be "variable1" and value will be "3.1" or whatever....therefore you can right to the file the value of the variable property along with the value of the object via the value property

     

    This maybe one small workaround but not the best.

     

    so when you get your results from SQL, create an instance of the class and say, add it into an array or something (if this is what you want) then for each instance of that object, set the variableName property to "var1", "var2" etc... and the value property to the result of the field retrieved from SQL

     

    then you can go through the objects and write your statements to text file.

     

    Does this give you some ideas?

    Friday, August 24, 2007 2:13 AM
    Moderator
  • Yes. That was a pretty easy solution. But actually pretty good as well :-) Much better than my current! I'll try it!

    Big thanks!
    Friday, August 24, 2007 9:58 AM
  •  RRRouter wrote:

    I'm looking for something like streamwriter.write(celciusTemperature.VariableNameToString()).

     

    Since your code (in your example) is hardcoded to what variable it uses you could just as well hardcode the name.

     

    Code Snippet
    streamwriter.Write(String.Format("celciusTemperature has value {0}", celciusTemperature));

     

     

     

    Saturday, August 25, 2007 8:39 PM
    Moderator