Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Below I am writing to a Serilog logger text file a user name variable, transaction name variable and a dictionary with 10 elements. This work perfectly as you can see from the logger output below.


var array1 ={"User name:", "Transaction:" ,"Transaction values:"};
var array2 = new object[] { MyUser.userId, MyTransSeq.transactionId, MyVarValueDict };
for(int j=0; j< array1.Length; j++)
{
    text = text + list1[j] + " {"+j+"} ";
}
MyGlobals.logger.Information(text, array2.ToArray());


Output from above code
2015-01-29 21:20:57.935 +00:00 [Information] User name: "pclarkeirl" Transaction: "initmenu01" Transaction values: [("<&option>": " "), ("<&envname>": "\" \""), ("<&test>": "MYTESTVALU"), ("<&envcount>": "3"), ("<&companyid>": "strykerlmk"), ("<&envdesc1>": " "), ("<&envdesc2>": " "), ("<&envcolour>": " "), ("<&count>": " "), ("<&selopt>": " ")]


I now want to change this so that rather than hard code the variables, the user can select which variables are to be logged. So the user can type in the list of fields as a comma delimited field such as “MyUser.userId, MyTransSeq.transactionId, MyVarValueDict” and this is written to array2. The contents of array2 now contain:
[0]=“MyUser.userId”,
[1]=”MyTransSeq.transactionId”,
[2]=“MyVarValueDict”

The problem is that they are now Text values in quotes. So my question is How can I turn a text array value such as “MyUser.userId” back into an object. Below is some code I have tried without success ?


string[] userSelArray={"MyUser.userId", "MyTransSeq.thisTransId", "MyVarValuesDict" }; // This is simulating the array values received from user.

string[] array1 = { "User name:", "Transaction:", "Transaction values:" };
object[] array2 = new object[array1.Count()];
string text = "";
for (int j = 0; j < array1.Length; j++)
{
  text = text + array1[j] + " {" + j + "} ";
  array2[j] = userSelArray[j];
}
MyGlobals.logger.Information("NEXT" + text, array2.ToArray());


Output from above code
2015-01-29 22:01:44.502 +00:00 [Information] NEXTUser name: "MyUser.userId" Transaction: "MyTransSeq.thisTransId" Transaction values: "MyVarValuesDict"

Above you can see that the variable names treated as text and are not being translated
Posted
Updated 29-Jan-15 18:02pm
v2
Comments
Kenneth Haugland 29-Jan-15 17:36pm    
HAve you thought about an XMLSerializer?
Sergey Alexandrovich Kryukov 29-Jan-15 17:40pm    
System.String is already System.Object. And you should avoid "convert", this would be abuse.
Don't create those strings in first place. Try to work with data, not strings representing data.
—SA

C# is object-oriented. You need to create classes representing the things in your problem/application. The variables hold the attributes/values and the methods enable you to access and manipulate the variables.
You are hard-coding every value. You need to design the program to work for all cases.
 
Share this answer
 
I am hard coding this to provide an example only. So maybe concentrate on providing an answer to the question

regards
Pat
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900