Assign reference to list properly c# - c#

I'm trying to add strings to a List<string> so I can print them with a loop in a certain point of time, being more specific here is part of my code:
public class Foo{
public string propertyA;
public string propertyB;
public string propertyC;
public List<string> list;
Public Foo(){
list = new List<string>();
list.Add(propertyA);
list.Add(propertyB);
list.Add(propertyC);
}
}
In later code, after assigning propertyA and the other variables and trying to iterate over the List I get empty strings. I require the properties to be in the list. My questions is which would be the best way to achieve this?

Looks like you are getting empty strings because when you are adding to the list the values in your properties have not been set at the time that the Foo() constructor is called...
Try passing values and setting them in the Foo constructor as follows:
public class Foo{
public string propertyA;
public string propertyB;
public string propertyC;
public List<string> list;
Public Foo(string propA, string propB, string propC){
propertyA = propA;
propertyB = propB;
propertyC = propC;
list = new List<string>();
list.Add(propertyA);
list.Add(propertyB);
list.Add(propertyC);
}
}
Alternatively you could add the values to the list at a later time when the properties are actually set and not in the constructor e.g.
public string PropertyA
{
//set the person name
set { propertyA = value;
list.Add(value);
}
//get the person name
get { return propertyA; }
}
...

What you're seeing is expected behavior. Updating "propertyA", etc later on won't update the strings that have already been added to the collection.
You could consider using a Dictionary instead of your own class, and then adding and updating elements is easier: (and you don't have to keep updating your class with new property names)
var properties = new Dictionary<string, string>();
properties.Add("propertyA", "some value of property A");
properties["propertyA"] = "some new value";
And when you want to display the values later:
MessageBox.Show(string.Join(Environment.NewLine, properties));
Alternatively, if you want a class and the option of adding properties to it, then maybe extending the Dictionary class like this will at least make things easier to maintain, so you can add more properties that'll stay in sync with the underlying Dictionary, with a minimum of fuss.
public class PropertyCollection : Dictionary<string, string>
{
public string PropertyA
{
get { return GetValue(); }
set { StoreValue(value); }
}
public string PropertyB
{
get { return GetValue(); }
set { StoreValue(value); }
}
protected string GetValue([CallerMemberName] string propName = "")
{
if (ContainsKey(propName))
return this[propName];
return "";
}
protected void StoreValue(string propValue, [CallerMemberName] string propName = "")
{
if (ContainsKey(propName))
this[propName] = propValue;
else
Add(propName, propValue);
}
}

If you want to assign propertyA, B, C after an instance of Foo is created and enumerate them, you could try something like this:
public class Foo
{
public string propertyA { get { return list[0]; } set { list[0] = value; } }
public string propertyB { get { return list[1]; } set { list[1] = value; } }
public string propertyC { get { return list[2]; } set { list[2] = value; } }
public List<string> list = new List<string>() {"", "", ""};
}
For the reasons why the code behaves in a way you might not expect, see How are strings passed in .NET?

Related

How can I iterate through a list with multiple objects from class

I am coding an application for my study, but i'm stuck at this point. I made a class called 'Neighborhood'. This class has a string and an int. In my main code I give the class a value and put it in a list. I now want to loop through my list and get the int out of it (put it in a listbox, or do a calculation). How do I get the int out of the list?
class Wijk
{
private string wijken;
private int tijd;
public string Wijken
{
get { return wijken; }
set { wijken = value; }
}
public int Tijd
{
get { return tijd; }
set { tijd = value; }
}
}
Created the list and the instance of the class.
List<object> Uden = new List<object>();
Wijk Wijkeninput = new Wijk();
Now I value the string and int with a combobox and textbox.
private void wijkAanmaken()
{
Wijkeninput.Wijken = Convert.ToString(cbWijken);
Wijkeninput.Tijd = Convert.ToInt16(tbSnelheid.Text);
Uden.Add(Wijkeninput);
}
For this, instead of having an object list, you can have list containing class objects like
List<Wjik> Uden = new List<Wjik>();
then you can access int as follows:
foreach (Wjik obj in listProgram)
{
int tij = Convert.ToInt32(obj.tijd);
}
First the List can be declared like this:
List<Wijk> Uden = new List<Wijk>();
To iterate over it:
foreach(var item in Uden)
{
var myInt = item.Tijd;
var myString = item.Wijken;
//here do whatever you want with the values
}

Is it possible to easily get variable names in a method as well as their values

Following my Is it possible to have a Function that takes any number of variables of any type?
I have the function that gets any number of any type of variables and it works perfectly
public string funcVars(params object[] paths)
{
string strVars = String.Join(", ", paths.Select(x => x.ToString()));
return strVars;
}
To call it I'd simply need to
string someString ="asd"; int someInt = 123; bool someBool=false;
funcVars(someString,someInt,someBool);
And the output would be
asd,123,false
is there any simple way I can also get the variable names as well as their values, so the output would be
asd,123,false,someString,someInt,someBool //(or any other similar form)
Or do I need to hardcode the names every time I call my method ?
funcVars("someString","someInt","someBool",someString,someInt,someBool);
What you really should be doing is creating a class to hold your variables:
internal class MyValues
{
internal string SomeString { get; set; }
internal int SomeInt { get; set; }
internal bool SomeBool { get; set; }
}
Then you can pass an instance of your class:
var mv = new MyValues() { SomeString = "asd", SomeInt = 123, SomeBool = false };
funcVars(mv);
Here is funcVars:
public string funcVars(MyValues values)
{
string strVars =
String.Join(", ", new[] { values.SomeString,
values.SomeInt.ToString(), values.SomeBool.ToString() });
return strVars;
}
Straight up stealing roy.ap's code and adding the "nameof()" method since getting the name of the property seemed to be apart of the question.
class Program
{
internal class MyValues
{
internal string SomeString { get; set; }
internal int SomeInt { get; set; }
internal bool SomeBool { get; set; }
}
static void Main(string[] args)
{
var mv = new MyValues() { SomeString = "asd", SomeInt = 123, SomeBool = false };
Console.WriteLine(funcVars(mv));
Console.ReadLine();
}
public static string funcVars(MyValues values)
{
string strVars =
String.Join(", ", new[]
{
nameof(values.SomeString), values.SomeString,
nameof(values.SomeInt), values.SomeInt.ToString(),
nameof(values.SomeBool), values.SomeBool.ToString()
});
return strVars;
}
}
There really isn't a way to get the variable names via the the function itself because the scope changes once you're in the method. That is even if you pass an array of objects, if you perform a foreach to go through each object you will give the individual objects a new scope specific name.
No, because the variables are not actually passed
No it is not possible, because the variables themselves are not actually passed. Their values are passed.
Consider this code:
string someString ="asd"; int someInt = 123; bool someBool=false;
funcVars(someString,someInt,someBool);
In your call to funcVars, all the parameters are passed by value. All three variables are copied, and copy of them is put on the stack. These stack variables are identified by completely different symbols-- (e.g. paths[0],paths[1], etc.)
After all, what would happen if you called it like this?
funcVars("Hello",245+25,test != null);
Obviously those values do not have variable names. There is no way your function can possibly retrieve what doesn't exist.
Use ExpandoObject instead
The System.Dynamic.ExpandoObject seems like a really good fit for this problem.
var args = new System.Dynamic.ExpandoObject();
args.SomeString = "hello";
args.SomeInt = 32;
args.SomeBool = false;
funcVars(args);
public static string funcVars(ExpandoObject inputs)
{
var sb = new StringBuilder();
foreach (KeyValuePair<string, object> kvp in inputs)
{
sb.Append(String.Format("{0} = {1}", kvp.Key, kvp.Value);
}
return sb.ToString();
}

How to maintain instance number in list of objects

I have a class baseClass, and a list of objects of the baseClass. What i want to achieve is that i have to dynamically assign the instance number to each object in the list. for that what am doing is that use a constructor to do this.
Following is the class definition:
public class baseClass
{
private int _InstanceNumber;
private int _MyIntVal;
private string _MyString;
public string MyString
{
get { return _MyString; }
set { _MyString = value; }
}
public int MyIntVal
{
get { return _MyIntVal; }
set { _MyIntVal = value; }
}
public int MyProperty
{
get { return _InstanceNumber; }
}
public baseClass(int instance)
{
_InstanceNumber = instance;
}
}
The creation of the List of objects is as follows:
int instanceNumber = 0;
List<baseClass> classList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
I know it is not the actual way for creating this. it does not give the index number actually. how can i calculate the instance number?
Consider the following scenario, that am creating another list of objects then it hard to maintain the instance number. or if i create another object(this also be an instance) external to the list.
int instanceNumber = 0;
List<baseClass> anotherClassList = new List<baseClass>();
classList.Add(new baseClass(instanceNumber++) { MyString = "sample1", MyIntVal = 10 });
classList.Add(new baseClass(instanceNumber++) { MyString = "sample2", MyIntVal = 11 });
Updates:
This is my temporary solution for this. i need proper way/ method to maintain instance number
If you want to find the index of item in the list, you should ask it from the list, not the item like:
var index = list.IndexOf(item);
But it seems that you expect the item to be aware of its position in the list. In order to do this, you should pass the list to the item so it can use it to find its own place in it:
public class Item
{
private List<Item> _containerList;
public Item(List<Item> containerList)
{
_containerList = containerList;
}
public int InstanceNumber
{
get { return _containerList.IndexOf(this); }
}
}
and change your code to:
List<Item> classList = new List<Item>();
classList.Add(new Item(classList ) { ... });
classList.Add(new Item(classList ) { ... });

Use Property to get value from List<string>

private List<string> _S3 = new List<string>();
public string S3[int index]
{
get
{
return _S3[index];
}
}
Only problem is I get 13 errors. I want to call string temp = S3[0]; and get the string value from the list with the particular index.
You can't do that in C# - you can't have named indexers like that in C#. You can either have a named property, with no parameters, or you can have an indexer with parameters but no name.
Of course you can have a property with a name which returns a value with an indexer. For example, for a read-only view, you could use:
private readonly List<string> _S3 = new List<string>();
// You'll need to initialize this in your constructor, as
// _S3View = new ReadOnlyCollection<string>(_S3);
private readonly ReadOnlyCollection<string> _S3View;
// TODO: Document that this is read-only, and the circumstances under
// which the underlying collection will change
public IList<string> S3
{
get { return _S3View; }
}
That way the underlying collection is still read-only from the public point of view, but you can access an element using:
string name = foo.S3[10];
You could create a new ReadOnlyCollection<string> on each access to S3, but that seems a little pointless.
C# cannot have parameters for their properties. (Side note: VB.Net can though.)
You can try using a function instead:
public string GetS3Value(int index) {
return _S3[index];
}
You have to use this notation
public class Foo
{
public int this[int index]
{
get
{
return 0;
}
set
{
// use index and value to set the value somewhere.
}
}
}
_S3[i] should automatically return the string at position i
So just do:
string temp = _S3[0];
Try this
private List<string> _S3 = new List<string>();
public List<string> S3
{
get
{
return _S3;
}
}
I would just go with
class S3: List<string>{}

XML string array deserialize as different type name

I have the following C# class property:
private List<string> _accountTypes;
[XmlArray(ElementName = "accountTypes")]
public List<string> AccountTypes
{
get { return _accountTypes; }
set { _accountTypes = value; }
}
which is initialized like this in the class constructor:
_accountTypes = new List<string>( new string[] { "OHGEE", "OHMY", "GOLLY", "GOLLYGEE" });
When deserialized I get this:
<accountTypes>
<string>OHGEE</string>
<string>OHMY</string>
<string>GOLLY</string>
<string>GOLLYGEE</string>
</accountTypes>
I should like it if I could get this:
<accountTypes>
<accountType>OHGEE</accountType>
<accountType>OHMY</accountType>
<accountType>GOLLY</accountType>
<accountType>GOLLYGEE</accountType>
</accountTypes>
Without creating a subclass of type "accountType", how can this be done? Are there any XML attribute properties that can be used to get what I need?
I think you are searching for the [XmlArrayItem] Attribute.
Try this:
[XmlArray(ElementName = "accountTypes")]
[XmlArrayItem("accountType")]
public List<string> AccountTypes
{
get { return _accountTypes; }
set { _accountTypes = value; }
}

Categories

Resources