2

Hi in Actionscript I may refer to a variable within an object thusly

objectName["variableName"] = "Some value";

How would I do the equivalent in c#

thanks

2
  • The variable names match the column names in the db so instead of matching each up I just want to loop the column names and fill the appropriate variable Commented Jul 22, 2010 at 6:15
  • Seems you have something in mind to accomplish. What's your C# code so far? Commented Jul 22, 2010 at 6:45

6 Answers 6

1

Dictionary is not part of the language & is provided in the framework.
Use Dictionary<Key, Value>.

e.g.

Dictionary<string, string> myData = new Dictionary<string, string>();

myData.Add("first", "stack");
myData.Add("second", "overflow");

Console.WriteLine(myData["first"]);
Sign up to request clarification or add additional context in comments.

Comments

1

It depends.

If you genuinely mean an object you want:

objectName.variableName = "Some value"; 

If you want a Map (also known as an associative array) your best solution is a Dictionary:

Dictionary<string, string> d = new Dictionary<string, string>()
d["variableName"] = "Some value";

Comments

1

You could use a dictionary...

using System.Collections.Generic;

public class x{
  public method1() {
    var objectName = new Dictionary<string, object>();
    objectName["variableName"] = "Some value";
  }
}

Or, use strongly-typed properties (safer and fast running), recommended where you know variable names at compile-time.

public class Person{
  public string Name {get;set;}
  public int Age {get;set;}
}

// and use it as follows in your functions

var person1 = new Person() {
  Name = "Fred",
  Age = 21,
};

// again, to demonstrate different syntax to do same thing
var person2 = new Person();
person2.Name = "Danny";
person2.Age = 2;
person2.Age = "x";  // won't compile - expects int, hence safer

Comments

0

What type of variable? It sounds like a dictionary/hashtable is most suited. These have index operators [], so the syntax will be identical.

Comments

0

C# doesn't have "objects with variables". C# objects have properties which are accessed like MyObject.PropertyName.

2 Comments

Er... yes it does; they're called fields, but that is what they are: object-level variables.
I've never heard anyone reference a field as an "object variable"
0

What you're referring to is a key-value collection. A Dictionary is C# implementation of this. Note that a dictionary only allows for unique keys. You can use it generically so you can use it like this.

Dictionary<string, string> myValues = new Dictionary<string, string>();
myValues.Add("variableName", "variableValue");

or

Dictionary<string, int> familySizes = new Dictionary<string, int>();
familySizes.Add("simpsons", 5);

As you can see you can choose what datatypes you use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.