Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Inheriting Static/Shared Fields and Properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Craig - A Healtchare IT developer - 31 Aug 2004 12:25 GMT
Hi,

I'm x-training to .NET at the moment from Java. I have a question regarding
how to implement in .NET the following Java feature.

In Java interfaces it is possible to define static final fields (and assign
values to them) - these fields provide constants that can be exploited by all
classes implementing those interfaces.

Thus inheritance is used to provide static constants with wide (but
definable) scope.

In .NET i've found this is not directly possible ... (tho. feel free to
correct me on this)

Alternatives are just create a class with the constants (but then all
classes and client programmers can access them - maybe this is undesirable)
or create a class containing the constants - messy and can potentially run
into problems through mutiple inheritance (I want to extend X but would also
like to inherit the constants in Y)

I know .NET interfaces allow the signature of properties to be defined in
Interfaces, but assignation of a value to a Shared member in an interface
appears to be forbbiden.

I have found the above Java feature very useful, and would like to re-use
this approach if possible.

Thanks in advance for help.
Robert Jordan - 31 Aug 2004 12:35 GMT
Hi Craig,

> In Java interfaces it is possible to define static final fields (and assign
> values to them) - these fields provide constants that can be exploited by all
[quoted text clipped - 11 lines]
> into problems through mutiple inheritance (I want to extend X but would also
> like to inherit the constants in Y)

in Java these interface are mostly used like enums in C#/.NET.
You may try this pattern, if your consts are of integral type:

// the value doesn't care at all
public enum SomeName {
  Foo,
  Bar
}

or

// you need a special value, i.e. to pass them
// to an unmanaged resource, etc.
public enum SomeName {
  Foo = 17,
  Bar = 1
}

or

// values can be combined with the OR operator
[Flags]
public enum SomeName {
  Read  = 0,
  Write = 2,
  Execute = 4
}

if you need to deal with several type:

public sealed class Foo {
  public const string Bar = "whatever";
  public const int SomeInt = 1;
  ....

  private Foo() {
    // prohibit instances
  }
}

bye
rob

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.