question regarding C#.

  • Thread starter Thread starter Julia
  • Start date Start date
J

Julia

Hi

I have class named Property:

enum PropertyType
{
text,
numeric
}

class Property
{
string stringValue;
long numericValue;
PropertyType type;
}

and a collection of properties,in the example the collection hold two
properties
a Url which is string type,and Size which is long type

I would like to be able to do something like the following

string Value1=Properties["Url"].value //this should return a string type
value
long Value2=Properties["Size"].value //this should return a long type
value

can't it be done?


thanks.
 
Sure, but you need to adjust your code.

Here is what you need:
1) A property called Properties which points to something like a Hashtable
or dictionary.
2) A structure that holds all the properties such as Value.
3) The Hashtable would then contain references to the structure.

Keep in mind what your code is doing. First it needs to dereference the
Properties property. Next it sees the indexer so what gets returned from
Properties must have an indexer (like a collection or hashtable). Finally,
the item found in the collection must have a Value property.
 
Julia,

You got your answer here, however it took some time.
Did you know that one of the three most active Microsoft develloper
newsgroups is

microsoft.public.dotnet.languages.csharp

Maybe for the next time

Cor

"Julia"
 
Julia said:
I have class named Property:

enum PropertyType
{
text,
numeric
}

class Property
{
string stringValue;
long numericValue;
PropertyType type;
}

and a collection of properties,in the example the collection hold two
properties
a Url which is string type,and Size which is long type

I would like to be able to do something like the following

string Value1=Properties["Url"].value //this should return a string type
value
long Value2=Properties["Size"].value //this should return a long type
value

can't it be done?

No. The compiler can't know that Properties["Url"].Value is going to be
a string but Properties["Size"].Value is going to be a long. What you
*can* do is use a Hashtable or something similar and cast the result:

string value1 = (string) Properties["Url"].Value;
long value2 = (long) Properties["Size"].Value;

(Use a straight Hashtable and you don't need to use the .Value bit in
the first place.)
 
Back
Top