emi_sastra
asked on
Declare Variables That Visible to Public Shared Sub
Hi All,
I have a sub/function like below.
How could variable sbdSQLCommand visible to Create_ERV_SP_Get_Kas_Vouc her ?
Thank you.
I have a sub/function like below.
Public Class clsCreateInventoryStoredProcedure
Private sbdSQLCommand As New System.Text.StringBuilder
Public Shared Sub Create_ERV_SP_Get_Kas_Voucher()
End Sub
End Class
How could variable sbdSQLCommand visible to Create_ERV_SP_Get_Kas_Vouc
Thank you.
ASKER
I have changed to :
Dim sbdSQLCommand As New System.Text.StringBuilder
Severity Code Description Project File Line Suppression State
Error BC30369 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.
Thank you.
Dim sbdSQLCommand As New System.Text.StringBuilder
Severity Code Description Project File Line Suppression State
Error BC30369 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.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. I get it.
- Please note that in web applications, it is not a good idea to use Shared functions/methods/variable without understanding their implications. If you declare a class level variable as shared - no matter how many objects you create of that class, they will all "share" that variable.
Will you please provide link that explain regarding your explanation ?
Thank you.
- Please note that in web applications, it is not a good idea to use Shared functions/methods/variable
Will you please provide link that explain regarding your explanation ?
Thank you.
The above statement was based on experience but I can explain it with an example. Let's take your scenario and twist it a bit.
Let's assume there are two users. UserA and UserB who can log on to your web site. When they login, if they are authenticated successfully you use a static/shared string variable named "AllowAccessToTable". For UserA, this value is TableA and for UserB this value is TableB. Now, let's assume UserA logs in first(hence AllowAccessToTable is set to TableA) and is able to perform his actions without any issues. After a couple of minutes, UserB logs in, now AllowAccessToTable is TableB. So next time UserA clicks on anything or interacts with the page, that causes it to reload(fully or partially), it will now allow UserA to perform actions on the TableB. So as I said above, Shared variable will be "shared" i.e. one and the same for N number of objects of that class being created.
There are many opinions in the industry about Shared variables - good and bad both. I think it depends on how you use them. For example, SQL connection or API / Service End Points are often times Singleton (which uses Shared/Static).
Let's assume there are two users. UserA and UserB who can log on to your web site. When they login, if they are authenticated successfully you use a static/shared string variable named "AllowAccessToTable". For UserA, this value is TableA and for UserB this value is TableB. Now, let's assume UserA logs in first(hence AllowAccessToTable is set to TableA) and is able to perform his actions without any issues. After a couple of minutes, UserB logs in, now AllowAccessToTable is TableB. So next time UserA clicks on anything or interacts with the page, that causes it to reload(fully or partially), it will now allow UserA to perform actions on the TableB. So as I said above, Shared variable will be "shared" i.e. one and the same for N number of objects of that class being created.
There are many opinions in the industry about Shared variables - good and bad both. I think it depends on how you use them. For example, SQL connection or API / Service End Points are often times Singleton (which uses Shared/Static).
ASKER
Ok. I get it.
Thus, for a Public Shared Sub/Function, what variable type should I use for avoiding shared problem ?
Thank you.
Thus, for a Public Shared Sub/Function, what variable type should I use for avoiding shared problem ?
Thank you.
If you can describe the business process, I will be able to provide better guidance. What exactly this function needs to do?
ASKER
- If you can describe the business process, I will be able to provide better guidance. What exactly this function needs to do?
Nothing special. A class to store SP (calling SP and return dataset or anything else), for calling outside it.
Thank you.
Nothing special. A class to store SP (calling SP and return dataset or anything else), for calling outside it.
Thank you.
If it is being called outside and just returning the dataset and is going to be common for all users, you can mark all of them shared and you are good to go. Nothing special needs to be done.
ASKER
Hi Chinmay,
Thank you very much for your help.
Thank you very much for your help.
sbdSQLCommand is a class level variable as it is defined at a class level - not inside a sub. So all the functions/methods of the class can access it.
When you mark a variable as private, it is private for outside of the class not within the class.
Regards,
Chinmay.