Re: Is this good use of Properties?



The property is completey self-contained, requires no instance data and
returns a constant or literal value. This makes it an ideal candidate for
static usage. Whether you refine your achitecture in that way is up to you.

Whether a class has one instance or a million, the code is not duplicated on
a per-instance basis, only the data, so essentitially your argument is
valid, the only thing that matters is whether you intend to create a
consistent and robust architecture or just fudge what your doing because it
seems to work at the time. Not marking the property as static implies to the
user that the property requires an instance of the class, which it doesn't

As to your other question, I'll just don my flame-proof trousers quickly and
say that I believe VB.NET was primarily expected to be code generated and so
is unnecessarily wordy and insists on some quite ridiculous langage
constructs. The insistence of the compiler that read-only is declared when
it can easily be inferred is just one example in a long list of things that
I find intensely annoying when using VB. Another is the stupidity of the
Overloads-Overrides construct because an overload isn't neccesarily an
override and vice-versa. Overloads can also be inferred by the compiler
simply by looking at the method signature so this introduces more redundant
waffle into the language.

To be dispassionate VB.NET does have some advantages. I've seen code
compiled with VB.NET that outperformed an exact equivalent program in C# on
a couple of occasions now. This is not to say that the advantages are
across-the-board however so I don't advocate VB as a performance enhancer.

I think that if you write a lot of code by hand, which I do, C# has the
leanest source code and the most sensible language structure. C# never makes
me pull my hair out in despair like VB.NET does.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"Brett" <no@xxxxxxxx> wrote in message
news:eK7Gk9JVFHA.3544@xxxxxxxxxxxxxxxxxxxxxxx
>I could declare it as static but only one instance of this class will ever
>exists. Given that scenario, isn't that basically the same as declaring
>it static?
>
> What are some of your reasons for saying the VB compiler is brain dead?
> I'm always interested in the detailed differences between C# and VB.NET
> and where one is better than the other.
>
> Thanks,
> Brett
>
> "Bob Powell [MVP]" <bob@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:OEAGOtJVFHA.3636@xxxxxxxxxxxxxxxxxxxxxxx
>> The C# compiler, unlike the brain-dead VB compiler, can figure out for
>> itself that if a property only has a get accessor then it's probably a
>> read-only property. It's one of my VB pet hates.
>>
>> Perhaps the type of property you mention would be better as a static
>> property similar to Pi in the Math class or "Red" in the Color class.
>>
>> --
>> Bob Powell [MVP]
>> Visual C#, System.Drawing
>>
>> Find great Windows Forms articles in Windows Forms Tips and Tricks
>> http://www.bobpowell.net/tipstricks.htm
>>
>> Answer those GDI+ questions with the GDI+ FAQ
>> http://www.bobpowell.net/faqmain.htm
>>
>> All new articles provide code in C# and VB.NET.
>> Subscribe to the RSS feeds provided and never miss a new article.
>>
>>
>>
>>
>>
>> "Brett" <no@xxxxxxxx> wrote in message
>> news:%23Cry0lJVFHA.2172@xxxxxxxxxxxxxxxxxxxxxxx
>>> If I do this without declaring a corresponding field, is it considered
>>> bad design? What are the advantages or disadvantages to either method?
>>> Notice there is not set.
>>>
>>> public string URL
>>> {
>>> get
>>> {
>>> return "www.somewhere.com/test.aspx";
>>> }
>>> }
>>>
>>> vs. a more common approach:
>>>
>>>
>>> private readonly string _URL = "www.somewhere.com/test.aspx";
>>>
>>> public string URL
>>> {
>>> get
>>> {
>>> return _URL;
>>> }
>>> }
>>>
>>> Also, if I only have a get accessor, is it necessary to declare the
>>> field as readonly?
>>>
>>> Thanks,
>>> Brett
>>>
>>
>>
>
>


.



Relevant Pages

  • Re: Is this good use of Properties?
    ... VB.NET that you wish the language would have handled it better. ... Overloads can also be inferred by the compiler ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: const variables
    ... Consts are, by definition, constant so it's the compiler that does the work here. ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... Answer those GDI+ questions with the GDI+ FAQ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is this good use of Properties?
    ... I could declare it as static but only one instance of this class will ever ... What are some of your reasons for saying the VB compiler is brain dead? ... > Find great Windows Forms articles in Windows Forms Tips and Tricks ... >> public string URL ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: Properties
    ... Is there any reason why this would not work and not simplify the ... Why should I have to declare any variable most of the time? ... The reason you explicitly declare fields used by a property is that the ... compiler needs to know what the code in the property does. ...
    (microsoft.public.dotnet.languages.csharp)

Loading