Problem with objects and automatic numbering in VB.NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johndevlon@hotmail.com

    Problem with objects and automatic numbering in VB.NET

    Hi everyone,

    I'm having a small problem using objects in VB.NET.

    I've created a class "Shop" and one of its properties is a number which
    I've named "intNumber" (Integer).

    I've created several objects of the class Shop and everythink works
    fine. What I'm trying to do is to modify my class in such a manner that
    when a create a new object, the property "intNumber" has already a
    value. Using a constructor you can do this but I would like to give
    each object a different number, starting at 1 or 0, increasing 1 each
    time a new object has been created...

    The first object has automatically number 1 assigned to "intNumber" ,
    the second number 2, etc...

    I know this is possible but have forgotten how to do this ...

    Many thanks

    John

  • Patrik Löwendahl [C# MVP]

    #2
    Re: Problem with objects and automatic numbering in VB.NET

    Hello john

    Use a static variable to hold the last number:


    public class Shop {
    private static int nextNumber = 0;
    private int number;

    public int Number {
    get { retun this.number; }
    }

    public Shop() {
    this.number = nextNumber;
    nextNumber++;
    }

    }


    --
    Patrik Löwendahl [C# MVP]

    Din partner för livslångt lärande och utveckling. Utforska över 450 kurser inom exempelvis IT, ledarskap, och affärskompetens. Bli inspirerad och redo för framtidens utmaningar med oss.

    [color=blue]
    > Hi everyone,
    >
    > I'm having a small problem using objects in VB.NET.
    >
    > I've created a class "Shop" and one of its properties is a number
    > which I've named "intNumber" (Integer).
    >
    > I've created several objects of the class Shop and everythink works
    > fine. What I'm trying to do is to modify my class in such a manner
    > that when a create a new object, the property "intNumber" has already
    > a value. Using a constructor you can do this but I would like to give
    > each object a different number, starting at 1 or 0, increasing 1 each
    > time a new object has been created...
    >
    > The first object has automatically number 1 assigned to "intNumber" ,
    > the second number 2, etc...
    >
    > I know this is possible but have forgotten how to do this ...
    >
    > Many thanks
    >
    > John
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Problem with objects and automatic numbering in VB.NET

      <johndevlon@hot mail.com> wrote:[color=blue]
      > I'm having a small problem using objects in VB.NET.
      >
      > I've created a class "Shop" and one of its properties is a number which
      > I've named "intNumber" (Integer).
      >
      > I've created several objects of the class Shop and everythink works
      > fine. What I'm trying to do is to modify my class in such a manner that
      > when a create a new object, the property "intNumber" has already a
      > value. Using a constructor you can do this but I would like to give
      > each object a different number, starting at 1 or 0, increasing 1 each
      > time a new object has been created...
      >
      > The first object has automatically number 1 assigned to "intNumber" ,
      > the second number 2, etc...
      >
      > I know this is possible but have forgotten how to do this ...[/color]

      Use a Shared variable to keep track of how many instances have been
      created before. Don't forget threading - either acquire a lock while
      you're incrementing, or use the Interlocked class.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Patrik Löwendahl [C# MVP]

        #4
        Re: Problem with objects and automatic numbering in VB.NET

        Hello Patrik,

        LOL sorry, read johns post. Shared is the correct term in VB.NET. A bit to
        quick there.


        --
        Patrik Löwendahl [C# MVP]

        Din partner för livslångt lärande och utveckling. Utforska över 450 kurser inom exempelvis IT, ledarskap, och affärskompetens. Bli inspirerad och redo för framtidens utmaningar med oss.

        [color=blue]
        > Hello john
        >
        > Use a static variable to hold the last number:
        >
        > public class Shop {
        > private static int nextNumber = 0;
        > private int number;
        > public int Number {
        > get { retun this.number; }
        > }
        > public Shop() {
        > this.number = nextNumber;
        > nextNumber++;
        > }
        > }
        >
        > --
        > Patrik Löwendahl [C# MVP]
        > http://www.lowendahl.net
        > http://www.cornerstone.se[color=green]
        >> Hi everyone,
        >>
        >> I'm having a small problem using objects in VB.NET.
        >>
        >> I've created a class "Shop" and one of its properties is a number
        >> which I've named "intNumber" (Integer).
        >>
        >> I've created several objects of the class Shop and everythink works
        >> fine. What I'm trying to do is to modify my class in such a manner
        >> that when a create a new object, the property "intNumber" has already
        >> a value. Using a constructor you can do this but I would like to give
        >> each object a different number, starting at 1 or 0, increasing 1 each
        >> time a new object has been created...
        >>
        >> The first object has automatically number 1 assigned to "intNumber" ,
        >> the second number 2, etc...
        >>
        >> I know this is possible but have forgotten how to do this ...
        >>
        >> Many thanks
        >>
        >> John
        >>[/color][/color]


        Comment

        • johndevlon@hotmail.com

          #5
          Re: Problem with objects and automatic numbering in VB.NET


          Hi,
          Many thanx for the fast responses.

          Jon, could you please give a small example ?

          John

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Problem with objects and automatic numbering in VB.NET

            <johndevlon@hot mail.com> wrote:[color=blue]
            > Jon, could you please give a small example ?[/color]

            Something like this:

            Imports System
            Imports System.Threadin g

            Class Test

            Shared instancesCreate d As Integer

            Dim id As Integer


            Sub New
            id = Interlocked.Inc rement (instancesCreat ed)
            End Sub


            Shared Sub Main()

            Dim x0 As New Test
            Dim x1 As New Test
            Dim x2 As New Test
            Dim x3 As New Test

            Console.WriteLi ne (x0.id)
            Console.WriteLi ne (x1.id)
            Console.WriteLi ne (x2.id)
            Console.WriteLi ne (x3.id)

            End Sub


            End Class

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • TalkUdaiAboutBiz

              #7
              RE: Problem with objects and automatic numbering in VB.NET

              Hi John,

              You must define it as a static member and try working around this.

              Regards

              Udai.M
              Team Leader,Consulta nt
              Chimeratechnolo gies
              Bangalore


              "johndevlon@hot mail.com" wrote:
              [color=blue]
              > Hi everyone,
              >
              > I'm having a small problem using objects in VB.NET.
              >
              > I've created a class "Shop" and one of its properties is a number which
              > I've named "intNumber" (Integer).
              >
              > I've created several objects of the class Shop and everythink works
              > fine. What I'm trying to do is to modify my class in such a manner that
              > when a create a new object, the property "intNumber" has already a
              > value. Using a constructor you can do this but I would like to give
              > each object a different number, starting at 1 or 0, increasing 1 each
              > time a new object has been created...
              >
              > The first object has automatically number 1 assigned to "intNumber" ,
              > the second number 2, etc...
              >
              > I know this is possible but have forgotten how to do this ...
              >
              > Many thanks
              >
              > John
              >
              >[/color]

              Comment

              Working...