BlackStone Posted August 5, 2004 Posted August 5, 2004 What is the C# equivilent to a VB property such as this one?? Public Property Numbers(ByVal Index As Integer) As Integer .... End Property Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Joe Mamma Posted August 5, 2004 Posted August 5, 2004 What is the C# equivilent to a VB property such as this one?? Public Property Numbers(ByVal Index As Integer) As Integer .... End Property public class IntClass { int [] _ints = new int[3] {1,2,3}; public int this[int index] { get { return _ints[index]; } set { _ints[index] = value; } } } IntClass ints = new IntClass(); int x = ints[1]; // x = 2 ints[1] =100; int x = ints[1]; // x = 100 Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
BlackStone Posted August 5, 2004 Author Posted August 5, 2004 The above is like a default property in VB. (Indexers in C#) But what do you do if you want to have many properties that act like arrays? Like this(just an example): (vb) Public Class Buffer Private mBytes() As Byte Public Property Bytes(ByVal Index As Integer) As Byte Get Return mBytes(Index) End Get Set(ByVal value As Byte) mBytes(Index) = value End Set End Property Public Property Chars(ByVal Index As Integer) As Char Get Return Chr(mBytes(Index)) End Get Set(ByVal value As Char) mBytes(Index) = Asc(value) End Set End Property End Class Is the only way to do this to use seperate procedures for set/get?? Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Arch4ngel Posted August 5, 2004 Posted August 5, 2004 I think it's something C# can't do. Correct me if I'm wrong. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
cyclonebri Posted August 5, 2004 Posted August 5, 2004 I think it's something C# can't do. Correct me if I'm wrong. I am not sure if I'm missing the question, but I think that anytime you want to set or get properties across a class you should use a property with get/set functionality. You could write a public function that would allow you to get or set though without having to use the property syntax... For instance, in your class you could have the following: [CS] private byte[] m_Bytes = new byte[10]; //this public property gives you the ability to //get/set the entire array //from outside of the class: public byte[] Bytes { get { return m_Bytes; } set { m_Bytes = value; } } //This public function allows you to retrieve a specific //char from within the array public char GetChar(int index) { return Convert.ToChar(m_Bytes[index]); } //This public function allows you to set a specific //char into the array from outside the class public void SetChar(int index, string newvalue) { m_Bytes[index] = Convert.ToByte(newvalue); } [/CS] I hope that helps with your question... Quote
*Experts* Nerseus Posted August 5, 2004 *Experts* Posted August 5, 2004 I don't think there's a way to do properties with params in C# directly. You would have to do a combination of JoeMamma's and cyclonebri solutions to get what you want. Joe showed how to use an indexer to expose a property like an array. cyclone showed how to expose a private member as a property. If you create a new class that has an indexer and expose your private member variable of that new class type, you'd end up with something like: classA.PropA["hi"] = 123; In this case, PropA is a property in ClassA (classA being the instance). It would likely expose a private member variable such as propA that was of type NewClass. NewClass would expose the indexer as a string ("hi") and return an int value. If you need a sample, let me know. Not that I'd write one up, but I'd think about it :) -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
mocella Posted August 5, 2004 Posted August 5, 2004 You can only have one indexed property per class (the "this", which is your indexer). I went through this headache early in the planning stages of my current project, but managed to design compeletly around them for most situations. Here's a pretty decent article I had bookmarked from my earlier searches: http://www.csharphelp.com/archives/archive140.html Quote
Joe Mamma Posted August 5, 2004 Posted August 5, 2004 You can only have one indexed property per class (the "this"' date=' which is your indexer). I went through this headache early in the planning stages of my current project, but managed to design compeletly around them for most situations.[/quote'] actually, it is limited by the index type. this means you can have various enums and indexers specific to enumerations which can be quite useful and descriptive - public class IntsAndStrings { public enum EnumInts { a = 1 , b = 2 , c = 3 } public enum EnumStrings { a = 1 , b = 2 , c = 3 } int [] _ints = new int[3] {1,2,3}; string [] _strings = new string[3]; public int this[EnumInts index] { get { return _ints[(int)index]; } set { _ints[(int)index] = value; } } public string this[EnumStrings index] { get { return _strings[(int)index]; } set { _strings[(int)index] = value; } } } IntsAndStrings ist = new IntsAndStrings(); int x = ist[intsAndStrings.EnumInts.a]; ist[intsAndStrings.EnumInts.a] = 1000; string s = ist[intsAndStrings.EnumStrings.a]; ist[intsAndStrings.EnumStrings.b] = "Byte Me!!!"; Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
BlackStone Posted August 5, 2004 Author Posted August 5, 2004 I made a dll to see what would happen if I had a vb class with properties + params. Apparently behind the seens seperater procedures are made for each, so in C#, it had get_bytes,set_bytes, etc.. Oh, well I guess Ill go with the sloppier Get/Set procedures. All the solutions above are very interesting, but they are too complex for the simple needs of a property+params. Quote "For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.