• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

Inherit the Integer Class

I have a custom class which converts any information written to it to an integer. Seeing as this is the base state for the data it seems to make sense that I should Inherit the Integer/Int32 class and simply tack my custom methods and properties on.

I can't inherit integer like this:
************************
Public Class NewClass()
Inherits Integer

End Class
************************

What should I do or am I asking the impossible here?

I prefer VB.net but would be prepared to accept an answer in any .net compatable language.

Thanks
0
davidogdenblue
Asked:
davidogdenblue
  • 2
  • 2
  • 2
  • +3
1 Solution
 
anyoneisCommented:
"You can use Inherits only in a class or interface. This means the declaration context for an inheritance cannot be a source file, namespace, structure, module, procedure, or block."

No can do!

David

0
 
Bob LearnedCommented:
There are a variety of methods to convert things to an integer, so what value are you gaining with this custom class?

Bob
0
 
Arthur_WoodCommented:
Integer is not a CLASS, in any language that I am familiar with.  It is a TYPE, which is not the same thing.

You cannot Inherit from Integer.  

You may need to rethink your approach to this problem.

AW
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
aaronfengCommented:
Not sure where you are going with this, but if you want to manipulate an integer you could wrap the integer in a data class and call a method on it to return the approriate format based on the method.  So in this case you can use composition instead of inheritance.

The class you are trying to inherit from integer, what concept does it represent in the domain?  I.e Money etc..

Cheers,

Aaron
0
 
gena17Commented:
Integer is definitely class in .NET
But this class is sealed, it means you can't inherit from it.
0
 
davidogdenblueAuthor Commented:
Currently my existing class has numerous operator overloads to allow me to treat it like an integer (add, subtract, divide etc) I saw inheriting the Integer class as doing this automatically. In addition I would be able to use the '= assign' operator, I don't seem to be able to overload this operator only the '= compare'.

Aaron: Would that be effectively writing my own Integer Class? Can you elaborate on you idea?

The current class holds timecode information. All timecode information boils down to a number of frames. I have properties which accept many different formats of timecode but all of these are converted down to an integer once inside the class.
0
 
aaronfengCommented:
Hi davidogdenblue,

Basically in the end you want to be able to input something then output it as integer right?

public class TimeFrame {
  private Format f;

  public TimeFrame(Format f) {
    this.f = f;
  }

  public int OutputTimeFrameAsInteger() {
    int i = 0;
    // this class will ask the Format class for its format
   //  so it would know how to convert into proper
   // integer format

   return i;
  }
}

You can expand on that idea, I just started with the basic out line.  Does that help?

Cheers,

Aaron
0
 
anyoneisCommented:
Write your own class that composes an integer - "Favor aggregation over inheritance" (Gang of Four).

David
0
 
davidogdenblueAuthor Commented:
I have found that I can modify my existing class to give me the functionality I need by overloading the Ctype operator. Like so:

********************************************************
    Public Shared Widening Operator CType(ByVal n1 As Timecode) As Integer

        Return n1.TimeAsFrames

    End Operator

    Public Shared Widening Operator CType(ByVal n1 As Integer) As Timecode

        Dim ReturnTC As New EBUTimecode(FrameRate.PalFPS)

        ReturnTC.TimeAsFrames = n1

        Return ReturnTC

    End Operator
********************************************************

This is just about satifactory for my needs although inheriting the integer class would have been preferable.

I am inclined to award the points to gena17 if there are no complaints...

Thanks
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now