HELP pls!!!!
-
I would like to create a class that could be used to return values. I stored those values in a property so at times I will just retrieve it. My class looks like this:
Class1 { private string _name; public Class1(){} public string Name { get { return _name; } set { _name = value; } } }
My problem now, lies here: I would like to call those property in a way that "name" variable is an array-like? Meaning if i will use this class, I could have code like this:Class1 c1 = new Class1(); string name1 = c1.Name[0]; string name2 = c1.Name[1]; ... ... ...
Could anyone tell me if it's possible to have a property which is array-like? If yes, how is it done? Thank you very much!!!:):):) -
I would like to create a class that could be used to return values. I stored those values in a property so at times I will just retrieve it. My class looks like this:
Class1 { private string _name; public Class1(){} public string Name { get { return _name; } set { _name = value; } } }
My problem now, lies here: I would like to call those property in a way that "name" variable is an array-like? Meaning if i will use this class, I could have code like this:Class1 c1 = new Class1(); string name1 = c1.Name[0]; string name2 = c1.Name[1]; ... ... ...
Could anyone tell me if it's possible to have a property which is array-like? If yes, how is it done? Thank you very much!!!:):):)clasClass1 { private string _name; //declare the string in array. public Class1(){} public string[] Name { get { return _name; } set { _name = value; } } } Hope this helps, I`m not sure how it`s done. Tell me if it works
Work hard and a bit of luck is the key to success.
You don`t need to be genius, to be rich.
-
clasClass1 { private string _name; //declare the string in array. public Class1(){} public string[] Name { get { return _name; } set { _name = value; } } } Hope this helps, I`m not sure how it`s done. Tell me if it works
Work hard and a bit of luck is the key to success.
You don`t need to be genius, to be rich.
A slight error in one line. It should look like:
private string[] _name;
-
I would like to create a class that could be used to return values. I stored those values in a property so at times I will just retrieve it. My class looks like this:
Class1 { private string _name; public Class1(){} public string Name { get { return _name; } set { _name = value; } } }
My problem now, lies here: I would like to call those property in a way that "name" variable is an array-like? Meaning if i will use this class, I could have code like this:Class1 c1 = new Class1(); string name1 = c1.Name[0]; string name2 = c1.Name[1]; ... ... ...
Could anyone tell me if it's possible to have a property which is array-like? If yes, how is it done? Thank you very much!!!:):):)Maybe use an ArrayList (from namespace System.Collections) intead of strings... using System.Collections Class1 { private ArrayList _name=new ArrayList(); public Class1(){} public string Name { get { return _name; } set { _name=value; } } } in this way this code Class1 myClass=new Class1(); Console.WriteLine(myClass.Name[0].ToString()); will write on screen the first object stored converted to string... In this way you can store different types of data (or objects), since ArrayList class is supposed to store "object" objects :P Obviusly you need to know what kind of data you are going to retrieve from the ArrayList... ^^
-
I would like to create a class that could be used to return values. I stored those values in a property so at times I will just retrieve it. My class looks like this:
Class1 { private string _name; public Class1(){} public string Name { get { return _name; } set { _name = value; } } }
My problem now, lies here: I would like to call those property in a way that "name" variable is an array-like? Meaning if i will use this class, I could have code like this:Class1 c1 = new Class1(); string name1 = c1.Name[0]; string name2 = c1.Name[1]; ... ... ...
Could anyone tell me if it's possible to have a property which is array-like? If yes, how is it done? Thank you very much!!!:):):)Why don't you try property indexers. For eg,
class Class1 { StringArrayClass cs; public StringArrayClass Name { return cs; } class StringArrayClass { public string this[int index] { return array[index]; } }
-
Maybe use an ArrayList (from namespace System.Collections) intead of strings... using System.Collections Class1 { private ArrayList _name=new ArrayList(); public Class1(){} public string Name { get { return _name; } set { _name=value; } } } in this way this code Class1 myClass=new Class1(); Console.WriteLine(myClass.Name[0].ToString()); will write on screen the first object stored converted to string... In this way you can store different types of data (or objects), since ArrayList class is supposed to store "object" objects :P Obviusly you need to know what kind of data you are going to retrieve from the ArrayList... ^^
that didn't work, i've tried it... thanks anyway :)
-
Why don't you try property indexers. For eg,
class Class1 { StringArrayClass cs; public StringArrayClass Name { return cs; } class StringArrayClass { public string this[int index] { return array[index]; } }
Yes! that's exactly how i did it.. I look on a couple of samples in msdn.. thanks!!! :)