none
Static variable in Class RRS feed

  • Question

  • Hello,

    I understand we cant have static variable in a class. Is there any alternative to achieve a static variable in a class in vb.net ? I don't need shared.

    Thanks,

    Monday, July 6, 2015 10:12 PM

Answers

  • What I asked that why I need to do ResetTransaction everytime I process a new file. It was my understanding that when I do "Dim obj as New FileProcessing" , all shared/nonshared member would start from 0 automatically but that's not happening. To me doing "new" is like new separate instance of everything and everything below it.

    ' Reset before Processing any File Transaction
    RegisterStat.ResetTransaction() ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
    Dim obj As New FileProcessing
    obj.Processing() 

    Because that's what a shared variable is.  You code declared it as shared,

    Public Class Tran
        Private Shared _LastRecID As Integer = 0

    so however many instances of the class you created there was only one instance of that variable and every usage of that variable referred to that one instance with one value. 

    Therefore, you need to reset it at the start of each processing cycle.

    " To me doing "new" is like new separate instance of everything and everything below it" is correct - except for anything that is shared.

    From your examples you wanted to
    - Use an incrementing ID for each instance
    - Avoid the need to reset the counters at the start of each cycle

    The second code I posted does this.

    Processing File:19/07/2015 8:34:45 AM
    TranRecID Register4:1
    TranRecID Register3:1
    TranRecID Register3:2
    TranRecID Register1:1
    TranRecID Register3:3
    TranRecID Register3:4
    TranRecID Register3:5
    TranRecID Register2:1
    TranRecID Register3:6
    TranRecID Register2:2
    Processing File:19/07/2015 8:34:45 AM
    TranRecID Register4:1
    TranRecID Register4:2
    TranRecID Register1:1
    TranRecID Register3:1
    TranRecID Register3:2
    TranRecID Register3:3
    TranRecID Register3:4
    TranRecID Register4:3
    TranRecID Register3:5
    TranRecID Register4:4
    So if you don't want to have to reset you variable at the start of each cycle, don't use Shared.

    • Marked as answer by Me.Saqib Tuesday, July 21, 2015 12:11 PM
    Saturday, July 18, 2015 10:43 PM

All replies

  • I understand we cant have static variable in a class. Is there any alternative to achieve a static variable in a class in vb.net ? I don't need shared.

    What are you trying to achieve?

    You can't have static members in a class because that doesn't make sense for a class.  Static variables have their values preserved between invocations of a method.  A class is not a method, so static has no meaning at the class level.   You should describe what you expect to happen for this variable.

    Monday, July 6, 2015 10:21 PM
  • A static variable in VB is a variable which keeps its value from the last time and always placed inside a method. 

    https://msdn.microsoft.com/en-us/library/z2cty7t8.aspx?f=255&MSPPError=-2147217396

    Be aware that the word does in fact not tell what it is. Static is just a kind of legacy word for something fixed in memory. And that is not the case. 

    That is also not anymore with C types languages where the keyword Static means a kind of fixed variable in memory. 

    For VB is the synonym for the C# keyword Static; Shared. 

    Be aware languages are different. 

    You call probably a donkey also not a donkey in your natural language. 


    Success
    Cor



    Monday, July 6, 2015 10:24 PM
  • I m trying to assign objectID (like 1,2,3...) to each object of the class when the object is created.

    Monday, July 6, 2015 10:25 PM
  • I m trying to assign objectID (like 1,2,3...) to each object of the class when the object is created.

    OK.  What is the difficulty that you are having in assigning these IDs?  Why won't a simple assignment work for you?

    Monday, July 6, 2015 10:28 PM
  • I m trying to assign objectID (like 1,2,3...) to each object of the class when the object is created.

    So you make a public property objectID in  the object, it has a backing private variable you set to 1, and you remove the Set off of the public property.

    You can make a read only property. If you can't change the object's property, it already has a value set an initialization, it makes it kind of static, huh? :)

    http://www.dotnetperls.com/property-vbnet

    Monday, July 6, 2015 10:36 PM
  • I m trying to assign objectID (like 1,2,3...) to each object of the class when the object is created.

    OK.  What is the difficulty that you are having in assigning these IDs?  Why won't a simple assignment work for you?

    Where would you store your counter?
    Monday, July 6, 2015 11:13 PM
  • Where would you store your counter?
    What counter?   You have to provide more information about what you are trying to do and what your problem is if you want to get useful responses.
    Monday, July 6, 2015 11:17 PM
  • Each object I would create would get a ID that would store in a property. When first object will be created, it will get ID =1. Second will get ID=2 and so on.

    So how do I know when object n is created, what ID it will get?


    Monday, July 6, 2015 11:45 PM
  • So how do I know when object n is created, what ID it will get?

    How are these objects being created?  The usual way of knowing what ID to assign is to keep track of the last ID that got assigned.  Is that the  counter you are referring to?

    Do you mean that the first instance of a class has an object with an ID if 1, and the second instance of a class has an object with and ID of 2, and so on?  If so, then you need to explain why - that will probably reveal where the ID counter should be maintained.    For instance, the counter might be a shared variable, or it might be saved in a file or a database.

    In general, the process would be to create a (shared) factory class that creates and dispenses the objects.  As each class instance is created it requests an object from the factory, and that object is created with a correct ID. The counter is maintained within the shared class.

    But the question really relies on why these objects need sequential contiguous IDs.

    Tuesday, July 7, 2015 12:02 AM
  • Each object is also holding datatable property. So object is parent and datatable is child. At the end all the objects and their datatables are send to database and these ids are a way to link a object with its datatable.

    In my dll, I did create shared variable thinking that the scope of this shared variable will be at dll level but it is not. Every time the dll get called, this shared variable is keep on incrementing rather than being destroyed when the dll is done.

    Tuesday, July 7, 2015 12:18 AM
  • Each object is also holding datatable property. So object is parent and datatable is child. At the end all the objects and their datatables are send to database and these ids are a way to link a object with its datatable.

    In my dll, I did create shared variable thinking that the scope of this shared variable will be at dll level but it is not. Every time the dll get called, this shared variable is keep on incrementing rather than being destroyed when the dll is done.

    What do you mean Parent/Child?  If the datatable is a property of the object that creates it, then there is no need to identify it by ID - you will access it as a property of the object, and that will ensure you are accessing the datatable associated with that particular instance of the object.

    If the datatable is created separately from the object, and you need to connect them, then the best way to do that is to use a dictionary that associates each object with one datatable.

    If the ID is only used to connect the two items and does not have to be sequential and contiguous, the you can use the facility built-in to the .Net framework to create a unique identifier - see:
    https://msdn.microsoft.com/en-us/library/system.guid.newguid%28v=vs.110%29.aspx

    I can't follow your last statement, as I read your description as requiring that the index IS incremented each time it is used, and that it is NOT destroyed just because the DLL is 'done'. That's the point of a shared variable.  If you need to reset it at some point, you can.

    Tuesday, July 7, 2015 12:53 AM
  • I dont need shared then. 

    I need some variable that can tell me the  counter value so I can set the id of  each object. I thought  thats how static variables work ....

    Tuesday, July 7, 2015 1:03 AM
  • I dont need shared then. 

    I need some variable that can tell me the  counter value so I can set the id of  each object. I thought  thats how static variables work ....

    You don't need shared, but you could do it using a shared variable.   If you tried that and it didn't work then you should describe what you have done.  What is this DLL, and why is the shared variable in the DLL and not in a module?

    Static variable don't change between invocations of the method within which they are declared.   If you are creating all these instances in a method, then you can declare and initialise a static variable in that method, and incrementing that value each time the method creates a new object will mean that each one gets a unique ID.   You can't declare the static variable at the class level, however, because classes don't get 'called'.

    Tuesday, July 7, 2015 1:12 AM
  • This dll get called from exe on the processing of file. Everytime a file is there to process, the exe calls the dll and file get processed. The shared variable doesn't start with 0 everytime the dll loaded. That's the problem
    Tuesday, July 7, 2015 1:39 AM
  • Each object is also holding datatable property. So object is parent and datatable is child. At the end all the objects and their datatables are send to database and these ids are a way to link a object with its datatable.

    In my dll, I did create shared variable thinking that the scope of this shared variable will be at dll level but it is not. Every time the dll get called, this shared variable is keep on incrementing rather than being destroyed when the dll is done.

    So what happens when your application is closed? The counter would go back to 1, 2, 3, and so on. Then if you send these data to your database, you will have duplicate keys in your tables. Why not let the database handle the id generation. Depends on what database you are using, you can create an Identity or AutoIncrement or whatever means that your DB can generate a unique key for each row. It's a bad idea to generate database keys on your own. 
    Tuesday, July 7, 2015 1:47 AM
  • This dll get called from exe on the processing of file. Everytime a file is there to process, the exe calls the dll and file get processed. The shared variable doesn't start with 0 everytime the dll loaded. That's the problem

    Then put the variable in the EXE and call it every time a file is there to process.

    With .Net, loading the DLL is not under your control, so you should explain what 'everytime the dll loaded' means.  Do you mean every time the DLL is called, or do you mean everytime some class defined in the DLL is instantiated, or do you mean every time the EXE is executed. or something else?

    Tuesday, July 7, 2015 1:50 AM
  • I mean when the dll is called
    Tuesday, July 7, 2015 2:11 AM
  • I mean when the dll is called

    Then the variable is not being correctly maintained as static.   You have provided no indication about how you declared, initialised or used this variable in the DLL, so it is not possible to comment on why that is.

    Use a static variable in the method where you call the DLL, and assign the ID in that code instead of in the DLL.

    Or create a shared method in a module that returns the next sequential ID and increments it.

    Or change the DLL to access a unique GUID instead of trying to maintain its own ID.

    Tuesday, July 7, 2015 3:07 AM
  • Here is my dll code:

    Public Class Tran
        Private Shared _LastRecID As Integer = 0
        Public ReadOnly SRecID As Integer = 0
        Public data As New DataTable
    
        Public Sub New()
            ' initializing from constructor...
            _LastRecID += 1
            SRecID = _LastRecID
            data.Columns.Add("ID", GetType(Integer))
            data.Columns.Add("STime", GetType(DateTime))
        End Sub
    End Class
    
    Public Class ProcessSales
        Public Sub New()
        End Sub
    
        Public Sub Process()
            Dim objTran As New Tran
            Dim objID As Integer = objTran.SRecID
        End Sub
    
    End Class

    Here is my test exe
        Sub Main()
            Dim obj As ProcessSales = New ProcessSales
            obj.Process()
        End Sub
    
    

    Tuesday, July 7, 2015 5:12 AM
  • Here is my dll code:

    That code doesn't make sense to me.  Sub Main is called once on application startup.  That code can't ever create an object with anything other than ID = 0.

    Do you mean that you expect the ID to be incremented each time the application is executed?  If so, you have to save it somewhere and retrieve it at each startup - static etc does not apply, it's just a matter of storing the value somewhere. 

    Or are you actually calling it from a method in a form?

    Public Class Tran Public ReadOnly SRecID As Integer = 0 Public data As New DataTable Public Sub New(_LastRecId As Integer) ' initializing from constructor... SRecID = _LastRecID data.Columns.Add("ID", GetType(Integer)) data.Columns.Add("STime", GetType(DateTime)) End Sub End Class Public Class ProcessSales Private _ThisRecID As Integer = 0 Public Sub New(ThisRecId As Integer) _ThisRecId = ThisRecId End Sub Public Sub Process() Dim objTran As New Tran(_ThisRecId) Dim objID As Integer = objTran.SRecID End Sub End Class

    Sub SomeMethod() Private Static _LastRecID As Integer = 0 Dim obj As ProcessSales = New ProcessSales(_LastRecId) _LastRecId += 1 obj.Process() End Sub

    Tuesday, July 7, 2015 6:19 AM
  • The code in main sub was just to show u an example how I was calling the dll's process method. As I mentioned before , this exe process files and this dll get called when a file appear on directory. So it's not just one time as I shown but on everytime the file appears.
    Tuesday, July 7, 2015 10:44 AM
  • The code in main sub was just to show u an example how I was calling the dll's process method. As I mentioned before , this exe process files and this dll get called when a file appear on directory. So it's not just one time as I shown but on everytime the file appears.

    I gave you more than 12 hours ago a reply. If you are not able to program, than don't try it. If you look at my reply than it is easy to implement what Acamar wrote.

    Public Class Form1
        Public Dummies As New List(Of Dummie)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static ID As Integer = 0
            ID += 1
            Dummies.Add(New Dummie(ID))
        End Sub
        Public Class Dummie
            Public Property Nr As Integer
            Public Sub New(ID As Integer)
                Nr = ID
            End Sub
        End Class
    End Class
    But you seems to think in an apology that driving a car means putting a horse behind it instead of petrol and then you are surprised it does not go likewise intended. 


    Success
    Cor

    Tuesday, July 7, 2015 12:03 PM
  • What do we call a functionality where different objects of a Class shared a variable among themselves? Meaning that if objects read that variable, they will see the same value.

    Tuesday, July 7, 2015 12:20 PM
  • You really should take some study what means:

    • reference type
    • object
    • Class
    • Shared
    • Static
    • value type.

    An integer is a value type and therefore it is not an object in the way you describe. 

    But why are you so lazy and don't you simple test the code I gave you. It really does not go if you put a horse behind the car. 

    I made it and you are to lazy just to copy, past and run and then look what the result is.


    Success
    Cor

    Tuesday, July 7, 2015 12:32 PM
  • I know what your code is doing and I understand it too but you not answering what i m asking you.
    Tuesday, July 7, 2015 12:47 PM
  • Each object is also holding datatable property. So object is parent and datatable is child. At the end all the objects and their datatables are send to database and these ids are a way to link a object with its datatable.

    In my dll, I did create shared variable thinking that the scope of this shared variable will be at dll level but it is not. Every time the dll get called, this shared variable is keep on incrementing rather than being destroyed when the dll is done.

    Why don't you keep it simple and use the ADO.NET Entity Framework? EF , an ORM, will take care of all of it for you.
    Tuesday, July 7, 2015 1:15 PM
  • I know what your code is doing and I understand it too but you not answering what i m asking you.

    I always write here "If somebody wants to swim from New York to London, and asks how to do that"

    I give the answer to take a boot or a plane. 

    However if you insist, "Jump in the Hudson and start swimming."

    Or is it just a kind of trolling question because C# has inherited the legacy Static members from C++. 

    Ok, there was no need to introduce the problems caused by that into VB at the moment VB became OOP. 

     

    Success
    Cor


    Tuesday, July 7, 2015 4:02 PM
  • The code in main sub was just to show u an example how I was calling the dll's process method.

    As I said above, the place where you need to maintain the counter is in the method that calls the code that creates the instance that needs that ID.   So providing a dummy Sub Main that does not have any similarity to the method you are actually using is a bit pointless.  That it needs to be executed repeatedly with different values is the whole basis of your problem.

    Use the code format in the example I provided above, using the method that detects the new file where I have used 'SomeMethod'. But be aware that that is very confused code that will be difficult to debug and maintain.


    • Edited by Acamar Tuesday, July 7, 2015 10:02 PM sp
    Tuesday, July 7, 2015 10:01 PM
  • The problem I was having that shared variable wasn't resetting to 0 when the dll was getting called on every new file from exe. I didn't know that Shared memory is set at application level, not dll level.
    This was the problem but I was thinking it is at the dll level and it would reset to 0.
    Then I thought to ask if there is something different than shared, again at class level in which I got response that static members exist in methods and not in class and obviously class is not method so they cant exist in class. Got one step cleared.
    At this point, I start getting responses on static variables and how to use them. I guess my fault as I asked that I need static variable at class level instead of shared and the only reason I asked because the shared wasn't resetting once the app started even shared was declared in dll and I was thinking that when dll will be called from app, this shared would start with 0 value but then I started to ask for static at class level (which at the time of question),  I wasn't aware of static at method level that would reset to 0.
    My concern was to find something that would be reset at class level, not like shared which would never.
    I guess people keep directing me to use static with a method where I would instantiate my class and get benefit of static to get incremented value.
    What I don't understand that why shared is bad way to use variable among classes and if it doesn't get reset then I can reset it on the main method call of dll.
    Thursday, July 9, 2015 7:19 PM
  • The problem I was having that shared variable wasn't resetting to 0 when the dll was getting called on every new file from exe. I didn't know that Shared memory is set at application level, not dll level.
    This was the problem but I was thinking it is at the dll level and it would reset to 0.

    You don't 'call' a DLL in .Net.  You make use of the methods in the DLL.  A shared variable is not shared memory - how the shared variable works is subject to a very precise definition.  How a shared variable is declared is critical to how it can be used.  As you have not included any of that code it is not possible to comment on why it did not behave as you expected. 

    Then I thought to ask if there is something different than shared, again at class level in which I got response that static members exist in methods and not in class and obviously class is not method so they cant exist in class. Got one step cleared.
    At this point, I start getting responses on static variables and how to use them. I guess my fault as I asked that I need static variable at class level instead of shared and the only reason I asked because the shared wasn't resetting once the app started even shared was declared in dll and I was thinking that when dll will be called from app, this shared would start with 0 value but then I started to ask for static at class level (which at the time of question),  I wasn't aware of static at method level that would reset to 0.

    It appears that you asked a question about static variables when you meant to ask a question about shared variables, but other than that I can't follow the above comments.  Perhaps that's because you are still talking about 'resetting' a variable when a DLL is 'called' which is a concept that simply does not exist in .Net.  What exactly is the code that you are identifying as 'calling' a DLL?

    My concern was to find something that would be reset at class level, not like shared which would never.
    I guess people keep directing me to use static with a method where I would instantiate my class and get benefit of static to get incremented value.
    What I don't understand that why shared is bad way to use variable among classes and if it doesn't get reset then I can reset it on the main method call of dll.

    Shared is not a bad way to use a variable, and nobody said it was.  Your question related to static variables, so that's why you got answers about how to use a static variable.   The description of your task made it clear that a static variable was appropriate.   If you want to use a shared variable, incrementing it each time it gets used, then that's OK too.  But that wasn't your question.

    A static variable declared and used in the method that creates the instance that needs the ID is the obvious and simple way to go.  But there are many other ways to do it, including (I believe) an option that doesn't need an ID at all. 

    If you are concerned about why your use of the shared variable didn't work, then you need to show the code you used.

    Friday, July 10, 2015 12:44 AM
  • The problem I was having that shared variable wasn't resetting to 0 when the dll was getting called on every new file from exe. I didn't know that Shared memory is set at application level, not dll level.
    This was the problem but I was thinking it is at the dll level and it would reset to 0.
    Then I thought to ask if there is something different than shared, again at class level in which I got response that static members exist in methods and not in class and obviously class is not method so they cant exist in class. Got one step cleared.
    At this point, I start getting responses on static variables and how to use them. I guess my fault as I asked that I need static variable at class level instead of shared and the only reason I asked because the shared wasn't resetting once the app started even shared was declared in dll and I was thinking that when dll will be called from app, this shared would start with 0 value but then I started to ask for static at class level (which at the time of question),  I wasn't aware of static at method level that would reset to 0.
    My concern was to find something that would be reset at class level, not like shared which would never.
    I guess people keep directing me to use static with a method where I would instantiate my class and get benefit of static to get incremented value.
    What I don't understand that why shared is bad way to use variable among classes and if it doesn't get reset then I can reset it on the main method call of dll.

    If you displayed the code from your .Dll and the code used for accessing the .Dll for whatever part of the .Dll you are refering to then you wouldn't have a thread that appears convoluted.

    It's up to you to provide the details. Usually code is simplest since those responding can usually understand the issues with it regardless of the terminology the thread creator constantly outputs in posts which may or may not be enough to provide all those responding with enough information to form an idea of what to do to assist the thread originator.

    Now you say people tell you to do this or that or the other. Yet you never provided enough info for people to direct you to anything IMO.

    So don't complain please.

    I suggest you provide all the code you are refering to. And then perhaps this thread will come to fruition. Or maintain your path. Either way it's irrelevant to me but could assist you.

    There's a term called GIGO. I believe it reflects why this thread is not answered. When not enough information is provided about a question the resulting answers can not be accurate.


    La vida loca

    Friday, July 10, 2015 4:08 AM
  • Here is my code: 

    There are two project: Console app and Class library project. 

    Module1 will go to Module1.vb in console app.

    RegisterStat will go in RegisterStat.vb in Class lib.

    File Processing will go in FileProcessing.vb in Class lib. 


    ' MODULE 1
    
    Imports TestVBLib
    
    Module Module1
    
        Sub Main()
           
            ' Processing 5 files
            For num As Integer = 1 To 5
    
                ' Reset before Processing any File Transaction
                RegisterStat.ResetTransaction()         ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
                Dim obj As New FileProcessing
                Console.WriteLine("Processing File:" + DateTime.Now.ToString)
                obj.Processing()
    
            Next
            Console.ReadLine()
    
        End Sub
    
    End Module
    ' REGISTER STAT 
    
    Public Class RegisterStat
    
        Private Shared _LastTranRecID As Integer
    
        Public TranRecID As Integer = 0
    
        Public Sub MakeSale()
            _LastTranRecID += 1
            TranRecID = _LastTranRecID
        End Sub
    
        Public Shared Sub ResetTransaction()
            _LastTranRecID = 0
        End Sub
    
    End Class
    ' FILE PROCESSING
    
    Public Class FileProcessing
    
        Dim Register1 As New RegisterStat
        Dim Register2 As New RegisterStat
        Dim Register3 As New RegisterStat
        Dim Register4 As New RegisterStat
        Dim rnd As New Random()
    
        Public Sub Processing()
    
            For recs As Integer = 1 To 4 'RecInFile
    
                Dim RegisterRecType As Integer
                RegisterRecType = New Random().Next(1, 5)
    
                Select Case (RegisterRecType)
                    Case 1
                        ' Record found for Register 1
                        Register1.MakeSale()
    
                        Console.WriteLine("TranRecID Register1:" + Register1.TranRecID.ToString())
                    Case 2
                        ' Record found for Register 2
                        Register2.MakeSale()
    
                        Console.WriteLine("TranRecID Register2:" + Register2.TranRecID.ToString())
                    Case 3
                        ' Record found for Register 3
                        Register3.MakeSale()
    
                        Console.WriteLine("TranRecID Register3:" + Register3.TranRecID.ToString())
                    Case 4
                        ' Record found for Register 4
                        Register4.MakeSale()
    
                        Console.WriteLine("TranRecID Register4:" + Register4.TranRecID.ToString())
                End Select
    
                Try
                    Threading.Thread.Sleep(5)
                Catch ex As Exception
    
                End Try
            Next
        End Sub
    
    End Class
    



    Saturday, July 11, 2015 12:42 PM
  • Here is my code: 

    There are two project: Console app and Class library project. 
    Module1 will go to Module1.vb in console app.
    RegisterStat will go in RegisterStat.vb in Class lib.
    File Processing will go in FileProcessing.vb in Class lib. 

    That code does not require any shared methods or variables.  I presume that is because you have provided a simplified example, and in the real case the ID will be accessed from various different parts of your application, and therefore needs to be shared.

    Your FileProcessing class creates instances of RegisterStat. This is incorrect for a shared class.  I have rewritten the code to avoid the need to instance the shared class, and also to restrict the sharing to the class methods.

    ' REGISTER STAT 
    Public Class RegisterStat
    
        Private Shared _LastTranRecID As Integer
    
        Public Shared Function MakeSale() As Integer
            _LastTranRecID += 1
            Return _LastTranRecID
        End Function
    
        Public Shared Sub ResetTransaction()
            _LastTranRecID = 0
        End Sub
    
    End Class
    
    ' FILE PROCESSING
    Public Class FileProcessing
    
        Public Sub Processing()
            Dim RND As New Random()
    For recs As Integer = 1 To 4 'RecInFile Dim RegisterRecType As Integer RegisterRecType = RND.Next(1, 5) Select Case (RegisterRecType) Case 1 ' Record found for Register 1 Console.WriteLine("TranRecID Register1:" + RegisterStat.MakeSale.ToString()) Case 2 ' Record found for Register 2 Console.WriteLine("TranRecID Register2:" + RegisterStat.MakeSale.ToString()) Case 3 ' Record found for Register 3 Console.WriteLine("TranRecID Register3:" + RegisterStat.MakeSale.ToString()) Case 4 ' Record found for Register 4 Console.WriteLine("TranRecID Register4:" + RegisterStat.MakeSale.ToString()) End Select Next Threading.Thread.Sleep(5) End Sub End Class

    Was your very first problem actually "Error:  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"?  That would explain why you have created those instances.  If so, then you will see how this revised code has avoided that error.

    Saturday, July 11, 2015 11:27 PM
  • There are 4 registers that would 4 objects of register. Each register object is tracking its own sale. 

    I never got ""Error:  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"?  

    The problem was in my main module that why I need to do reset shared variable on processing of each file? If I don't call reset, then each new file processing  _LastTranRecID doesn't start with 0.

        ' Reset before Processing any File Transaction
                RegisterStat.ResetTransaction()         ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
                Dim obj As New FileProcessing
                Console.WriteLine("Processing File:" + DateTime.Now.ToString)
                obj.Processing()

    Sunday, July 12, 2015 12:01 AM
  • There are 4 registers that would 4 objects of register. Each register object is tracking its own sale.

    Do you mean 4 instances of RegisterStat?  You can't have a shared object and also have distinct instances - the two concepts are completely opposite.

    Are you quite sure you have a clear understanding of 'shared'?  It does not mean 'accessible from different modules or methods'.   It has a precise technical definition, and precise rules about how it is to be used. 

    I don't call reset, then each new file processing  _LastTranRecID doesn't start with 0.

    If you want the procedure to start again at 0 then you have to set the shared variable to 0.  That's what 'shared' means - one instance is created at first access, and that one instance is then used for all subsequent references to the object.  As I said above, there is no such thing a 'calling' a DLL like this, and there is no reason to expect that one access to the object is any different than any other access.

    The alternative is not to use shared at all, but to create one class instance for each file to be processes.  A class instance is created with default values (eg, 0) and that class instance retains values as set until it is disposed of. That's what I though you were originally trying to do.   So an alternative structure would look like this:

    ' MODULE 1
    Imports TextVBLib
    Module Module1
    
        Sub Main()
    
            ' Processing 5 files
            For num As Integer = 1 To 5
    
                Dim obj As New FileProcessing
                Console.WriteLine("Processing File:" + DateTime.Now.ToString)
                obj.Processing()
    
            Next
            Console.ReadLine()
    
        End Sub
    End Module
    ' REGISTER STAT 
    Public Class RegisterStat
        Private _LastTranRecID As Integer = 0
    
        Public Function MakeSale() As Integer
            _LastTranRecID += 1
            Return _LastTranRecID
        End Function
    End Class
    
    ' FILE PROCESSING
    
    Public Class FileProcessing
    
        Dim Register1 As New RegisterStat
        Dim Register2 As New RegisterStat
        Dim Register3 As New RegisterStat
        Dim Register4 As New RegisterStat
    
        Public Sub Processing()
            Dim rnd As New Random()
            For recs As Integer = 1 To 10 'RecInFile
                Dim RegisterRecType As Integer
                RegisterRecType = rnd.Next(1, 5)
    
                Select Case (RegisterRecType)
                    Case 1
                        ' Record found for Register 1
                        Console.WriteLine("TranRecID Register1:" + Register1.MakeSale.ToString())
                    Case 2
                        ' Record found for Register 2
                        Console.WriteLine("TranRecID Register2:" + Register2.MakeSale.ToString())
                    Case 3
                        ' Record found for Register 3
                        Console.WriteLine("TranRecID Register3:" + Register3.MakeSale.ToString())
                    Case 4
                        ' Record found for Register 4
                        Console.WriteLine("TranRecID Register4:" + Register4.MakeSale.ToString())
                End Select
            Next
            Threading.Thread.Sleep(5)
        End Sub
    
    End Class
    Sunday, July 12, 2015 12:25 AM
  • Do you mean 4 instances of RegisterStat?  You can't have a shared object and also have distinct instances - the two concepts are completely opposite.

    Why cant I do this? I see this many places where the class has instance and w shared members as in this in article msdn shared article


    Monday, July 13, 2015 12:46 AM
  • Why cant I do this? I see this many places where the class has instance and w shared members as in this in article msdn shared article

    That's a different keyword in a different language - it's not relevant to what you are trying to do.  If you want to see how to use shared variables, look at the VB example:
    https://msdn.microsoft.com/en-us/library/zc2b427x.aspx

    You can have non-shared members and methods in a class that also has shared members and methods, but you can only access the shared members or methods through the class type, not through the class instance.   If you attempt to access the shared members or methods through the instance the compiler will change the access to the type, and that can create nasty errors because your code no longer matches what is happening.

    ' REGISTER STAT 
    Public Class RegisterStat
    
        Private Shared _LastTranRecID As Integer
    
        Public Shared Function MakeSale() As Integer
            _LastTranRecID += 1
            Return _LastTranRecID
        End Function
    
        Public Shared Sub ResetTransaction()
            _LastTranRecID = 0
        End Sub
    
        Public Function ShowHash() As Integer
            Return Me.GetHashCode
        End Function
    End Class
    
    ' FILE PROCESSING
    Public Class FileProcessing
    
        Dim Register1 As New RegisterStat
        Dim Register2 As New RegisterStat
        Dim Register3 As New RegisterStat
        Dim Register4 As New RegisterStat
    
        Public Sub Processing()
            Dim RND As New Random()
            For recs As Integer = 1 To 4 'RecInFile
                Dim RegisterRecType As Integer
                RegisterRecType = RND.Next(1, 5)
    
                Select Case (RegisterRecType)
                    Case 1
                        ' Record found for Register 1
                        Console.WriteLine("TranRecID Register1:" + RegisterStat.MakeSale.ToString() & " " & Register1.GetHashCode)
                    Case 2
                        ' Record found for Register 2
                        Console.WriteLine("TranRecID Register2:" + RegisterStat.MakeSale.ToString() & " " & Register2.GetHashCode)
                    Case 3
                        ' Record found for Register 3
                        Console.WriteLine("TranRecID Register3:" + RegisterStat.MakeSale.ToString() & " " & Register3.GetHashCode)
                    Case 4
                        ' Record found for Register 4
                        Console.WriteLine("TranRecID Register4:" + RegisterStat.MakeSale.ToString() & " " & Register4.GetHashCode)
                End Select
            Next
            Threading.Thread.Sleep(5)
        End Sub
    
    End Class
    
    The non-shared members or methods are not associated with your ID.

    Monday, July 13, 2015 1:38 AM
  • Hi Acamar,

    I didn't get chance to follow up as I was thinking that I m unable to tell what my problem but I want to try explain one more time.

    Your last post got marked as answered but the solution you posted wasn't the problem I m having . I never asked that why I cant access shared members of class using instance or if I were getting ""Error:  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"

    Moreover the code I provided, I created instance of RegisterStat in FileProcessing class and you did too.

    My class isn't a shared class from day one but somehow you thought it was.

    If I compare your last code vs my code, the only difference I see, you converted MakeSale function to Shared which I didn't have shared. I don't understand why you made MakeSale function shared and then access it via Class rather instance or if I also make it shared, I don't think I will get any difference result.

    You need to tell me exactly what I m doing wrong. I m sure you have more knowledge than me but somehow I m not following you and accept my apologies for this.

    Friday, July 17, 2015 9:38 PM
  • Your last post got marked as answered but the solution you posted wasn't the problem I m having . I never asked that why I cant access shared members of class using instance or if I were getting ""Error:  Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"

    No.  You asked about Static, which confused everyone until you gradually described what you were actually trying to do.

    My class isn't a shared class from day one but somehow you thought it was.

    No.  I thought the problem was that your class was NOT shared, and therefore you were not getting the ID incrementing correctly.  That's why your reference to resetting the ID was confusing.

    Whether or not you create an instance of your class for some other purpose is not relevant to the use of the shared ID.  It simply means that you are using that class for something in addition to providing the ID, which is a bit confusing for an example, but quite OK in practice.

    If I compare your last code vs my code, the only difference I see, you converted MakeSale function to Shared which I didn't have shared. I don't understand why you made MakeSale function shared and then access it via Class rather instance or if I also make it shared, I don't think I will get any difference result.

    NO! 

        RegisterStat.MakeSale.ToString()

    is accessing the shared function through the type, not the instance. That is the only way you can access a shared method or variable.  If you try to do it through the instance the compiler will change your code for you, creating problems.

    if I also make it shared, I don't think I will get any difference result.

    What happened when you tried?

    You need to tell me exactly what I m doing wrong. I m sure you have more knowledge than me but somehow I m not following you and accept my apologies for this.

    If you are still not getting the result you need then you must show the code you are now using (the code that implements one or the other of the structures I posted above), show the exact results you are getting, and the exact results that you need to get.

    Friday, July 17, 2015 9:53 PM
  • Despite that the meaning of the answer is right, is it completely ignoring that in VB a class can have a Static Element. 

    Static and Shared are both keywords of VB and a Static variable can be used as class. Without this information it is in my perception not complete as answer. However, the question is done from the perspective of for this forum OT program languages.

     

    @Acamar, I've used the inform part of the abuse button to make this known to a moderator.

     


    Success
    Cor

    Saturday, July 18, 2015 6:37 AM
  • @Acamar, I've used the inform part of the abuse button to make this known to a moderator.

    I believe we have established that OP is not talking about Static, despite the title.  If you are suggesting that the original misuse of the term, or the reference to an example in C#, makes makes the post off-topic, that is not correct.

    Saturday, July 18, 2015 7:15 AM
  • @Acamar, I've used the inform part of the abuse button to make this known to a moderator.

    I believe we have established that OP is not talking about Static, despite the title.  If you are suggesting that the original misuse of the term, or the reference to an example in C#, makes makes the post off-topic, that is not correct.

    That is my point, those who find this thread would think that there is no "Static" in a VB Class. I don't mean that it is off topic but it needs at least the addition that there can be user "Static" in a VB Class. 

    Not the point that there should be marked an answer of me, that does not bother me at all, you've seen I've enough points. 


    Success
    Cor

    Saturday, July 18, 2015 11:31 AM
  • I don't know if this will help...  This code shows how a class can have an ID that is used (shared) among instances of the class. It is a simple example that I pulled from a working class.  The class:

    Public Class DemoID
        Private Shared ReadOnly _startVal As Integer = -1  'define before _baseID - default starting value 
        Private Shared _baseID As Integer = _startVal      'base ID - shared by all
    
        Private _thisID As Integer                         'id of this instance
    
        Public Sub New()
            Me._thisID = Threading.Interlocked.Increment(_baseID)
        End Sub
    
        ''' <summary>
        ''' return the ID assigned to this instance
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ThisID() As Integer
            Return Me._thisID
        End Function
    
        ''' <summary>
        ''' reset the base ID to the default start value
        ''' </summary>
        ''' <remarks></remarks>
        Public Shared Sub ResetBaseID()
            Threading.Interlocked.Exchange(_baseID, _startVal)
        End Sub
    
        ''' <summary>
        ''' set the base ID so that the next id assigned will be newBaseID
        ''' </summary>
        ''' <param name="newBaseID">the next ID to be assigned</param>
        ''' <remarks></remarks>
        Public Shared Sub SetBaseID(newBaseID As Integer)
            Threading.Interlocked.Exchange(_baseID, newBaseID - 1)
        End Sub
    
        ''' <summary>
        ''' returns the next ID to be assigned
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetNextID() As Integer
            Return Threading.Interlocked.Exchange(_baseID, _baseID) + 1
        End Function
    End Class

    Lets look at the simplest of examples of using the class.  It creates an instance of the class, shows the instances ID and the next ID that will be assigned.

            Dim foo As New DemoID
            Debug.WriteLine("'  this instance id is " & foo.ThisID.ToString)
    
            Debug.WriteLine("'  next id to be assigned is " & DemoID.GetNextID.ToString)
            '  this instance id is 0
            '  next id to be assigned is 1

    Now lets create several instances and see what happens:

            'create a container to hold the items
            Dim idList As New List(Of DemoID)
    
            For x As Integer = 1 To 3 'assign some ID's
                idList.Add(New DemoID)
            Next
    
            For Each ids As DemoID In idList 'show the ids assigned
                Debug.WriteLine("'  this instance id is " & ids.ThisID.ToString)
            Next
            '  this instance id is 0
            '  this instance id is 1
            '  this instance id is 2

    Up to this point we have started at zero.  Lets say that you wanted to have a new starting point.  The same code as above, but with a new starting point:

            'create a container to hold the items
            Dim idList As New List(Of DemoID)
    
            DemoID.SetBaseID(42) 'the next id assigned will be 42
    
            For x As Integer = 1 To 3 'assign some ID's
                idList.Add(New DemoID)
            Next
    
            For Each ids As DemoID In idList 'show the ids assigned
                Debug.WriteLine("'  this instance id is " & ids.ThisID.ToString)
            Next
            '  this instance id is 42
            '  this instance id is 43
            '  this instance id is 44

    The class was written with threading in mind, hence what looks like funny values.  To test that this id assignment is thread safe we will use a parallel for next loop.

    'thread safe container Dim idList As New Concurrent.ConcurrentBag(Of DemoID) DemoID.SetBaseID(100) Debug.WriteLine("' next id to be assigned is " & DemoID.GetNextID.ToString) 'assign some ID's Dim lr As Threading.Tasks.ParallelLoopResult lr = Parallel.For(0, 100, Sub(ctr As Integer) idList.Add(New DemoID) Threading.Thread.Sleep(0) End Sub) 'wait for loop to end Do While Not lr.IsCompleted Threading.Thread.Sleep(0) Loop Dim ct As Integer ct = (From d In idList Select d.ThisID).Distinct.Count If idList.Count <> ct Then Stop 'did we get unique ids? if not stop For Each ids As DemoID In idList 'show the ids assigned, maybe not in order Debug.WriteLine("' P this instance id is " & ids.ThisID.ToString) Next

    '  P this instance id is 108
    '  P this instance id is 107
    '  P this instance id is 106
    '  P this instance id is 105
    '  P this instance id is 104
    '  P this instance id is 103
    '  P this instance id is 102
    '  P this instance id is 100
    '  P this instance id is 101

    'etc


    I hope this helps.


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer




    • Edited by dbasnett Saturday, July 18, 2015 4:13 PM
    Saturday, July 18, 2015 4:07 PM
  • @Dbasnett,

    That is not a Static Variable inside a Class in VB. I hate this kind of code therefore I did not make it before but I made it to show this sample.

    With this code I show a Static Variable inside a class. (Most who answer are trying to think like the OP, in a kind of Java way. However, this is a VB forum)

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox1.Text = CStr(Count.Count)
        End Sub
    End Class
    Public Class Count
        Public Shared Function Count() As Integer
            Static Counter As Integer
            Counter += 1
            Return Counter
        End Function
    End Class


    Success
    Cor

    Saturday, July 18, 2015 4:37 PM
  • Wasn't it determined that the OP used 'Static' to express a concept and not really the Static keyword? 

    Your insistence that whatever is proposed must use the static keyword is pedantic.  Most posters seek an answer and are willing to consider alternatives.  How many times do we see questions about Array and end up taking the poster to List?

    BTW - from what I can tell the people participating in this discussion know that there is a Static in VB, and how to use it.  They have said as much, so you do not need to harp on the point.  The OP has seen it and if they feel that it must be used I feel confident they will let us know. 

    One other thing, I saw you mentioned how many points you have.  It only means you have a lot of points. 


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    Saturday, July 18, 2015 5:19 PM
  • if I also make it shared, I don't think I will get any difference result.

    What happened when you tried?

    If I make the function shared then couple changes I need to make in code. One I would need to access it using type and then I would need to take out  TranRecID member out as its not shared but ultimately result was same. The reason I left MakeSale non shared as I m trying to make sale from Register object, not from class but again this is all beyond the issue.

    What I asked that why I need to do ResetTransaction everytime I process a new file. It was my understanding that when I do "Dim obj as New FileProcessing" , all shared/nonshared member would start from 0 automatically but that's not happening. To me doing "new" is like new separate instance of everything and everything below it.

    ' Reset before Processing any File Transaction
    RegisterStat.ResetTransaction() ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
    Dim obj As New FileProcessing
    obj.Processing()

     

    Saturday, July 18, 2015 5:45 PM
  • What might be helpful is to take a step back from the code and talk about what the system is trying to accomplish. It seems like this has something to do with cash register(s)?

    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    Saturday, July 18, 2015 6:15 PM
  • Wasn't it determined that the OP used 'Static' to express a concept and not really the Static keyword? 

    Your insistence that whatever is proposed must use the static keyword is pedantic. 

    I hope I get the same attention from you when I ask a question in this thread. 

    "How to use a semicolon in VB", that is the same as you answer now to the OP. 

    It simply make no sense. "Static" is a word used from the time we had hard fixed memory and that was addressed in a static way. Those days are already 40 years behind us.

    However, there is a keyword in Java. Which has the name Static, that does not mean that it express something it could also have been Poblel. It says nothing.

    Therefore for VB was taken for that purpose it real is the name Shared.

    Pedantic, I would not dare to say that to you. In my culture we call that offending in an extra ordinary way.


    Success
    Cor





    Saturday, July 18, 2015 6:37 PM
  • I hope I get the same attention from you when I ask a question in this thread. 

    "How to use a semicolon in VB", that is the same as you answer now to the OP.

    What?

    It simply make no sense. "Static" is a word used from the time we hard fixed memory and that was addressed in a static way. Those days are already 40 years behind us.

    What makes no sense?  What does the history of memory storage have to do with anything?

    However, there is a keyword in Java. Which has the name Static, that does not mean that it express something it could also have been Dbasnett. It says nothing

    What does Java have to do with this?  I have no idea what, "that does not mean that it express something it could also have been..." means.  It sounds Zen'ish (is that a word?).

    Therefore for VB was taken for that purpose it real is the name Shared.

    More Zen?

    Pedantic, I would not dare to say that to you. In my culture we call that offending in an extra ordinary way.

    It was meant to be factual, not offensive.  Sorry you choose to take it that way.  Over the years some of the things you have said seemed offensive and personal towards other members.  I have always attributed that to be because we come from different cultures, and I ignore it.  I have noticed some of the other members don't.


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer


    • Edited by dbasnett Saturday, July 18, 2015 6:59 PM
    Saturday, July 18, 2015 6:49 PM
  • I hope I get the same attention from you when I ask a question in this thread. 

    "How to use a semicolon in VB", that is the same as you answer now to the OP.

    What?

    It simply make no sense. "Static" is a word used from the time we hard fixed memory and that was addressed in a static way. Those days are already 40 years behind us.

    What makes no sense?  What does the history of memory storage have to do with anything?

    However, there is a keyword in Java. Which has the name Static, that does not mean that it express something it could also have been Dbasnett. It says nothing

    What does Java have to do with this?  I have no idea what, "that does not mean that it express something it could also have been..." means.  It sounds Zen'ish (is that a word?).


    'Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it.'  JohnWein

    Multics

    My Serial Port Answer

    I don't need so much text as you, what do your reply have to do with this question "Static variable in a class".

    Be aware acting aggressive is seen by some man as the solution to out-argue. However, they only think about that. My last reply to you. Not because I'm impressed, but just because you have shown your own mischance in this.

    I saw you changed your reply a little bit. I was not happy to write this above. I mostly admires your contributions. But in this case it is almost John (Skeet from Reading) and Cor. And those who know history of the newsgroups know what that means. (We have no personal arguments both at all, it is just C# and VB). 

    :-)


    Success
    Cor



    • Edited by Cor Ligthert Saturday, July 18, 2015 9:07 PM typo, Jonn is not a detective :-)
    Saturday, July 18, 2015 6:56 PM
  • What I asked that why I need to do ResetTransaction everytime I process a new file. It was my understanding that when I do "Dim obj as New FileProcessing" , all shared/nonshared member would start from 0 automatically but that's not happening. To me doing "new" is like new separate instance of everything and everything below it.

    ' Reset before Processing any File Transaction
    RegisterStat.ResetTransaction() ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
    Dim obj As New FileProcessing
    obj.Processing()

    Your class variable was shared.

    Public Class Tran
       
    Private Shared _LastRecID As Integer = 0

    That variable will exist as a single value in a single instance of the class.  When you instantiate a new object all the non-shared members of the object will be initialised. But a shared member is NOT initialised - it exists as one and only one variable across all instance of the type.  That's what shared means.

    When you access the shared member of the instance (_LastRecId) you are accessing the  single shared variable, so the value will not be reset to the initial value.   The compiler tells you that you are not accessing the instance variable, and changes your code so to access the type variable.

    If you want to reset that variable to zero at some point you can do so.

    If you use a shared variable like that then you must reset it to zero when that is required.

    But in your case you wanted to :

    - Use a single class to create each transaction
    - Use a running ID number sequence unique to each transaction within a set
    - Eliminate the need to reset the shared variable to zero at the start of each transaction set.

    That's what the second example I provided above does.

    Processing File:19/07/2015 8:03:54 AM
    TranRecID Register4:1
    TranRecID Register1:1
    TranRecID Register4:2
    TranRecID Register4:3
    TranRecID Register2:1
    TranRecID Register1:2
    TranRecID Register4:4
    TranRecID Register2:2
    TranRecID Register1:3
    TranRecID Register4:5
    Processing File:19/07/2015 8:03:54 AM
    TranRecID Register3:1
    TranRecID Register2:1
    TranRecID Register1:1
    TranRecID Register4:1
    TranRecID Register4:2
    TranRecID Register1:2
    TranRecID Register1:3
    TranRecID Register2:2
    TranRecID Register1:4
    TranRecID Register3:2

    The ID increments using a separate sequence for each transaction set, and all IDs reset on the start of the next file.   Note that there are simpler ways to do this.


    • Edited by Acamar Saturday, July 18, 2015 10:14 PM sp
    Saturday, July 18, 2015 10:08 PM
  • That is my point, those who find this thread would think that there is no "Static" in a VB Class.

    No.  The meaning of Static and Shared for VB has been made quite clear in the various posts.

    Saturday, July 18, 2015 10:18 PM
  • What I asked that why I need to do ResetTransaction everytime I process a new file. It was my understanding that when I do "Dim obj as New FileProcessing" , all shared/nonshared member would start from 0 automatically but that's not happening. To me doing "new" is like new separate instance of everything and everything below it.

    ' Reset before Processing any File Transaction
    RegisterStat.ResetTransaction() ' TO RESET SHARED VARIABLE, THIS STATEMENT IS MUST.
    
    Dim obj As New FileProcessing
    obj.Processing() 

    Because that's what a shared variable is.  You code declared it as shared,

    Public Class Tran
        Private Shared _LastRecID As Integer = 0

    so however many instances of the class you created there was only one instance of that variable and every usage of that variable referred to that one instance with one value. 

    Therefore, you need to reset it at the start of each processing cycle.

    " To me doing "new" is like new separate instance of everything and everything below it" is correct - except for anything that is shared.

    From your examples you wanted to
    - Use an incrementing ID for each instance
    - Avoid the need to reset the counters at the start of each cycle

    The second code I posted does this.

    Processing File:19/07/2015 8:34:45 AM
    TranRecID Register4:1
    TranRecID Register3:1
    TranRecID Register3:2
    TranRecID Register1:1
    TranRecID Register3:3
    TranRecID Register3:4
    TranRecID Register3:5
    TranRecID Register2:1
    TranRecID Register3:6
    TranRecID Register2:2
    Processing File:19/07/2015 8:34:45 AM
    TranRecID Register4:1
    TranRecID Register4:2
    TranRecID Register1:1
    TranRecID Register3:1
    TranRecID Register3:2
    TranRecID Register3:3
    TranRecID Register3:4
    TranRecID Register4:3
    TranRecID Register3:5
    TranRecID Register4:4
    So if you don't want to have to reset you variable at the start of each cycle, don't use Shared.

    • Marked as answer by Me.Saqib Tuesday, July 21, 2015 12:11 PM
    Saturday, July 18, 2015 10:43 PM
  • Got it. 

    Also I wasn't trying to set a separate counter for each register. 

    So if register2 made first sale, it will get ID =1, register3 made second sale, it will id =2, register1 made third  sale, it will get ID=3 and so on so forth....

    • Marked as answer by Me.Saqib Tuesday, July 21, 2015 12:10 PM
    • Unmarked as answer by Me.Saqib Tuesday, July 21, 2015 12:10 PM
    Tuesday, July 21, 2015 4:30 AM
  • Got it.

    In that case it is likely that one or more of the responses was useful in answering your question.  You should mark those responses as answers.

    Tuesday, July 21, 2015 6:43 AM