What am I missing?
-
I am relatively new to .net. While I have dabbled in it, I haven't done anything serious. Now I'm trying to build a fairly complex dll/assembly and am running into a real frustration. I need to have a property that is both read/write and pulls/puts data into a collection based on a unique key. The problem is, I can't overload the set portion to allow for the key to be passed along with the value. Here are the examples of both uses: MyValue = Field(100) Field(100) = "Frank" I could do this very easily in VB6. I know this is more like a function than a property. Do I really need to write two functions with one overloaded? And if I do that, the second let syntax won't work. It would look like this: Field(100,"Frank") While that's useable, it's not very consistent or user-friendly for someone using this assembly. Not only that, it creates a real migration problem. Any suggestions would be greatly appreciated. CalmlyFrustrated
-
I am relatively new to .net. While I have dabbled in it, I haven't done anything serious. Now I'm trying to build a fairly complex dll/assembly and am running into a real frustration. I need to have a property that is both read/write and pulls/puts data into a collection based on a unique key. The problem is, I can't overload the set portion to allow for the key to be passed along with the value. Here are the examples of both uses: MyValue = Field(100) Field(100) = "Frank" I could do this very easily in VB6. I know this is more like a function than a property. Do I really need to write two functions with one overloaded? And if I do that, the second let syntax won't work. It would look like this: Field(100,"Frank") While that's useable, it's not very consistent or user-friendly for someone using this assembly. Not only that, it creates a real migration problem. Any suggestions would be greatly appreciated. CalmlyFrustrated
What you probably want is an indexor... read this. Indexers In C#[^]
topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search
-
What you probably want is an indexor... read this. Indexers In C#[^]
topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search
Thanks for the suggestion. This particular function/property doesn't necessarily have sequential numbering. It also has potentially 65535 indices. I don't really have a choice as to the structure (it's a file format of a third-party program). What I'm using in VB6 is perfect and I'm struggling with the idea that dot net can't do it. It feels like I'm going backwards porting things to dot net. Public Property Get Field(FldNo as Long) as String ..... code End Property Public Property Let Field(FldNo as Long, Value as String) ..... code End Property This allows the calling routine to address this property just like any other property. I originally wrote it as a method but the property approach is so much more convenient to code. CalmlyFrustrated
-
Thanks for the suggestion. This particular function/property doesn't necessarily have sequential numbering. It also has potentially 65535 indices. I don't really have a choice as to the structure (it's a file format of a third-party program). What I'm using in VB6 is perfect and I'm struggling with the idea that dot net can't do it. It feels like I'm going backwards porting things to dot net. Public Property Get Field(FldNo as Long) as String ..... code End Property Public Property Let Field(FldNo as Long, Value as String) ..... code End Property This allows the calling routine to address this property just like any other property. I originally wrote it as a method but the property approach is so much more convenient to code. CalmlyFrustrated
Indexors don't have to use sequential numbering, though that's probably what you saw in the example, you can do anything with the index and the value once you have them. The index doesn't even have to be number. But if you want another alternative you could declare a struct with a long and a string and have that be the Type of your property. No other options come to mind but good luck.
topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search