I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables too much - that it's bad
programming practice. Is there an easy way to deal with this? I would like
to do things in the "Best Practices" way but at the same time I don't want
to make a federal case out of it.
This comes up over and over again, for example, when using products like
Infragistics. If I do something to a record in a grid, I need to identify
which record we're talking about in the enter event, and then make that
value available in the After Bla bla event.
So that I don't have 30 different public variables, what's a better but
still relatively simple way of doing this? Or is it really that bad to have
all those public variables? 17 5347
Woody,
I think this is a trap we all fall into from time to time.
And its mostly to do with coming from a programming environment which used
to support Global Variables maybe pre OOP.
In the OOP world. You wont need this provided that you have your
classes/containers,accessor methods etc worked out properly. Maybe you need
to re-think your design, or has it gone too far for this ?
Regards - OHM
Woody Splawn wrote: I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables too much - that it's bad programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it.
This comes up over and over again, for example, when using products like Infragistics. If I do something to a record in a grid, I need to identify which record we're talking about in the enter event, and then make that value available in the After Bla bla event.
So that I don't have 30 different public variables, what's a better but still relatively simple way of doing this? Or is it really that bad to have all those public variables?
--
Best Regards - OHM
O_H_M{at}BTInternet{dot}com
Maybe you mix global variables with public variables.
global variables (in VB6 called with "global")
are in VB.NET "public shared" Variables.
for example:
public Class MyClass1
public shared MyGlobalVariable as string 'is a global Variable.
End Class
You now can use MyClass1.MyGlobalVariable without making a instance of the
Class MyClass1.
This is what some people think "a bad design"
Normal Public Variables aren't a bad design i think.
Like this:
public Class MyClass1
public MyVariable as string 'is a public Variable but not Global
End Class
But there are people saying "that's also Bad, use Properties instead"
My Opinion is:
Avoid public shared Variables if you can.
But there are some situations where the design is better with public shared
variables !!
Public shared Variables are dangerous because you can change the value of it
in any Part of the Code and that can be lead to bugs that are hard to find.
Think about the situation that a College of you (or Youself)
sets some of those Shared Publics to a Value that makes your Code crashing
in an other Code part that hasn't anything to do with what you are doing.
So it's better if the Variables belong to the Object that they should belong
to.
My Advice:
Follow the Rule as long as it makes sence, but do not follow it blind and
for any cost.......
If you are smart, keep in mind the any rule should be broken if it's
necessary.
"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP09.phx.gbl... I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables too much - that it's bad programming practice. Is there an easy way to deal with this? I would
like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it.
This comes up over and over again, for example, when using products like Infragistics. If I do something to a record in a grid, I need to identify which record we're talking about in the enter event, and then make that value available in the After Bla bla event.
So that I don't have 30 different public variables, what's a better but still relatively simple way of doing this? Or is it really that bad to
have all those public variables?
Infragistics. If I do something to a record in a grid, I need to identify which record we're talking about in the enter event, and then make that value available in the After Bla bla event.
For this example you don't need PUBLIC Variables.
The Grid is in a Form. (A Form is simply a kind of Class)
So declare your Variables as private in the Form
Class MyForm
inherits Windows.Forms.Form
private selectedRow as row
private bla as string
private blub as integer
end class
If you need and of this outside of the Form Class
the create Properties to access these Variables.
"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP09.phx.gbl... I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables too much - that it's bad programming practice. Is there an easy way to deal with this? I would
like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it.
This comes up over and over again, for example, when using products like Infragistics. If I do something to a record in a grid, I need to identify which record we're talking about in the enter event, and then make that value available in the After Bla bla event.
So that I don't have 30 different public variables, what's a better but still relatively simple way of doing this? Or is it really that bad to
have all those public variables?
Thank you for your reply. I think I may not be using the right syntax when
I say global. What I mean, in this instance, is a variable that can be
seen by by various events on a Winform.
I posted a similar question to my original question here in an Infragistics
newsgroup. Perhaps the exchange may be of interest.
Original Question: What is your and Andrew's method for caching or saving the old or original value in the Enter event?
I mean, I know I can create a variable that has a scope that both events can see but there are numerous instances where logic similar to this may need to be implemented. Are you suggesting public variables in each case? The books I read suggest this is bad programming practice. Do you challenge that or is there something I'm missing.
"Yes, public field variables is bad programming practice but how you cache
the values is really up to you; I believe you have any number of options.
You could probably just create a simple class that can be passed a control
and an object value, then you can use a hashtable to store the values (using
the control as the key for the entry in the hashtable). Another option is to
create a class specifically for the form that has public properties to
store/retreive the values for specific controls. Another option is to store
the values in private fields in the form since the form knows about the
controls and has access to the events of the controls; then if you need to
access the variables from outside the form, you could expose readonly
properties."
All of the above seems rather complicated. I replied that perhaps I could
make use of the tag property of the form.
> Maybe you mix global variables with public variables.
Yes, I think this is the case. Follow the Rule as long as it makes sence, but do not follow it blind and for any cost....... If you are smart, keep in mind the any rule should be broken if it's necessary
Yes, I think this is right.
Straight ahead Captain Chaos!
> So declare your Variables as private in the Form
Yes, I understand but I might have 20 or 30 of these kind of variables, I
just thought it might not be cool or good practice to create so many. Maybe
I could create generic variables like intGeneric1 that can be used in more
than one place. That is, if the variable is always reset to its original
value when used and if I am confident that I will not inadvertantly allow
the instances where they are used to step on each other.
Thank you for your help.
I have about 20 private Variables too in some Classes......
If you use the Same kind of Variables in many places
then create a sparated Class where these Variables are exists.
There you can define them Public "The easy way"
Or create them private an access them via Public Properties "The hard way
but some gurus mean that right"
Then create a instance of the Class every time you need this bunch of
Variables.
Class MYVars
private mName as string
public property Name as string
get
Name=mName
set (Value as string)
mName=Value
End Property
End Class
class Form1 'There is your Grid
private myBunchofVariables as new Class MYVars
end class
If you use these Variables always together with the Grid you can also
create your own Grid Control by inherit the Grid and extend the Grid.
Create a UserControl
then Change the inherits Windows.Forms.UserControl to inherits
TheGridControlClassname
and the extend the Grid.
Then always use your own Grid and you have all these Functions.
Try to make Classes whereevery you need to do the same things.
"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:OW**************@TK2MSFTNGP10.phx.gbl... So declare your Variables as private in the Form Yes, I understand but I might have 20 or 30 of these kind of variables, I just thought it might not be cool or good practice to create so many.
Maybe I could create generic variables like intGeneric1 that can be used in more than one place. That is, if the variable is always reset to its original value when used and if I am confident that I will not inadvertantly allow the instances where they are used to step on each other.
Thank you for your help.
Each class should be self contained, but may contain instances of other
class types.
Regards - OHM
Woody Splawn wrote: Thank you for your reply. I think I may not be using the right syntax when I say global. What I mean, in this instance, is a variable that can be seen by by various events on a Winform.
I posted a similar question to my original question here in an Infragistics newsgroup. Perhaps the exchange may be of interest.
Original Question: What is your and Andrew's method for caching or saving the old or original value in the Enter event?
I mean, I know I can create a variable that has a scope that both events can see but there are numerous instances where logic similar to this may need to be implemented. Are you suggesting public variables in each case? The books I read suggest this is bad programming practice. Do you challenge that or is there something I'm missing.
"Yes, public field variables is bad programming practice but how you cache the values is really up to you; I believe you have any number of options. You could probably just create a simple class that can be passed a control and an object value, then you can use a hashtable to store the values (using the control as the key for the entry in the hashtable). Another option is to create a class specifically for the form that has public properties to store/retreive the values for specific controls. Another option is to store the values in private fields in the form since the form knows about the controls and has access to the events of the controls; then if you need to access the variables from outside the form, you could expose readonly properties."
All of the above seems rather complicated. I replied that perhaps I could make use of the tag property of the form.
--
Best Regards - OHM
O_H_M{at}BTInternet{dot}com
"Woody Splawn" <wo***@splawns.com> schrieb So declare your Variables as private in the Form
Yes, I understand but I might have 20 or 30 of these kind of variables, I just thought it might not be cool or good practice to create so many. Maybe I could create generic variables like intGeneric1 that can be used in more than one place. That is, if the variable is always reset to its original value when used and if I am confident that I will not inadvertantly allow the instances where they are used to step on each other.
Maybe it's already been covered, but.. :
Variables don't hang around anywhere. They are part of an object. So the
question is whether to make them public or private (or friend, protected,
....). They can be public if changing their content doesn't make the object
inconsistent.
So, it depends on your object model (and sometimes there is not a 100%
solution where to put variables).
--
Armin http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
I appreciate your input. Thank you.
Making the variable public in a class is an easy solution to what I have
been trying to do. I have another question. It is rather basic and I don't
know if I can ask it correctly but here goes. What I have in the example
above solves one problem but I have run up against another problem that may
or may not be solved with variables or properties in classes. There are a
number of situations where I would really like to have access to data that
persists. What I mean by persist is I would like to be able to set a value
when I open the applicaiton in the first instance, but when I close the
application and open it again, I would like to be able to access the same
value. I come from a Sybase (PowerBuilder) background. In PB this is
thought of as a structure but in VS a structure is used differently, i.e. ,
as far as I can tell, it does not persist.
I have been dealing with the problem in VS by saving whatever the value is
to a field in a table in a database. I use SQLServer as my database. This
works but I have the feeling that there is probably another way and the
other way may be more efficient. If it is not, then fine, I will continue
to use database tables to solve this but if there is a relatively easy way
to do it in a class, perhaps you could inform me.
Thank You.
Woody
Hi Woody, been trying to do. I have another question. It is rather basic and I
don'tknow if I can ask it correctly but here goes. What I have in the example above solves one problem but I have run up against another problem that may or may not be solved with variables or properties in classes. There are a number of situations where I would really like to have access to data that persists. What I mean by persist is I would like to be able to set a value when I open the applicaiton in the first instance, but when I close the application and open it again, I would like to be able to access the same value. I come from a Sybase (PowerBuilder) background. In PB this is thought of as a structure but in VS a structure is used differently, i.e. , as far as I can tell, it does not persist.
What do you mean by an application in PB, is it an couterpart of a class in
VB.NET or an application in VB.NET(a process).
If you mean the class, you may try to use the public shared variable.
If you mean the latter one, I think you may need to persist it into the
file, database or registry. Based on my experience, the registry is a good
place to persist the data.
If you have any concern on this issue, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hey Ho Woody
Sample:
You have a Database Connection an other Variables.
You and want to use that DatabaseConnection Object in other Forms.
Create a Class that holds all the Stuff
Class ApplicationData
public DBConnection as bla
public Name as bla
public anything as bla 'Or the way with the
Properties..... private Variables and public properties
End Class
The in your StartForm create an Instance of this Class.
Class Form1
private mAppData as new ApplicationData
End Class
From this Form you then open shurely other Forms.
If you Open the other Form give the other Form the Data.
Class Form1
private mAppData as new ApplicationData
sub SomeButton_Click(
dim myForm2 as new Form2
myForm2.Init(mAppData)
end sub
End Class
Class Form2
private mAppData as new ApplicationData
public Sub Init(appData as ApplicationData)
mAppData=appData
me.show()
end sub
'Now in Form2 you can use access mAppData to get the Connection of the
Database.
End Class
It's always the same game, always give the Data to the Class you need it.
An other nicer Way ist to overgive the Data in the Contructur. (If it's not
a Form class)
Then:
Class AnyClass
private mAppData as new ApplicationData
Public Sub New(appData as ApplicationData)
mAppData=appData
end sub
End Class
Then give the date if create the instance of the other Class
Class Form1
private mAppData as new ApplicationData
sub SomeButton_Click(
dim anyClass as new AnyClass(mAppData)
end sub
End Class
"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:ea**************@TK2MSFTNGP10.phx.gbl... I appreciate your input. Thank you.
Making the variable public in a class is an easy solution to what I have been trying to do. I have another question. It is rather basic and I
don't know if I can ask it correctly but here goes. What I have in the example above solves one problem but I have run up against another problem that
may or may not be solved with variables or properties in classes. There are a number of situations where I would really like to have access to data that persists. What I mean by persist is I would like to be able to set a
value when I open the applicaiton in the first instance, but when I close the application and open it again, I would like to be able to access the same value. I come from a Sybase (PowerBuilder) background. In PB this is thought of as a structure but in VS a structure is used differently, i.e.
, as far as I can tell, it does not persist.
I have been dealing with the problem in VS by saving whatever the value is to a field in a table in a database. I use SQLServer as my database.
This works but I have the feeling that there is probably another way and the other way may be more efficient. If it is not, then fine, I will continue to use database tables to solve this but if there is a relatively easy way to do it in a class, perhaps you could inform me.
Thank You.
Woody
Addition:
You can also put functions into this "ApplicationData" Class.
For example if you have Database related funktion where you need the DB
Connection.
The only place where I normaly use public shared is to make
Function Libraries.
I mean function that itself has no Data and you need them in many places.
Then it's usefull because you don't have to create an instance of it
every time oyu need it.
For Example if you have Function's like Calculate(value1, value2)
then I make:
public Class MyLibrary
public shared function Calculate(value1, value2) as integer
end function
end class
But this class should have no private Variables or public variables !!!
(only in the function some privates)
These Functions I put normaly in a extra DLL so anybody can use it.
"Captain Chaos" <Ca**********@nospam.com> schrieb im Newsbeitrag
news:bt*************@news.t-online.com... Hey Ho Woody
Sample:
You have a Database Connection an other Variables. You and want to use that DatabaseConnection Object in other Forms.
Create a Class that holds all the Stuff
Class ApplicationData public DBConnection as bla public Name as bla public anything as bla 'Or the way with the Properties..... private Variables and public properties End Class
The in your StartForm create an Instance of this Class.
Class Form1 private mAppData as new ApplicationData
End Class
From this Form you then open shurely other Forms.
If you Open the other Form give the other Form the Data.
Class Form1 private mAppData as new ApplicationData sub SomeButton_Click( dim myForm2 as new Form2 myForm2.Init(mAppData) end sub End Class
Class Form2 private mAppData as new ApplicationData public Sub Init(appData as ApplicationData) mAppData=appData me.show() end sub
'Now in Form2 you can use access mAppData to get the Connection of
the Database.
End Class
It's always the same game, always give the Data to the Class you need it.
An other nicer Way ist to overgive the Data in the Contructur. (If it's
not a Form class)
Then:
Class AnyClass private mAppData as new ApplicationData Public Sub New(appData as ApplicationData) mAppData=appData end sub End Class
Then give the date if create the instance of the other Class
Class Form1 private mAppData as new ApplicationData sub SomeButton_Click( dim anyClass as new AnyClass(mAppData) end sub End Class
"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag news:ea**************@TK2MSFTNGP10.phx.gbl... I appreciate your input. Thank you.
Making the variable public in a class is an easy solution to what I have been trying to do. I have another question. It is rather basic and I don't know if I can ask it correctly but here goes. What I have in the
example above solves one problem but I have run up against another problem that may or may not be solved with variables or properties in classes. There are
a number of situations where I would really like to have access to data
that persists. What I mean by persist is I would like to be able to set a value when I open the applicaiton in the first instance, but when I close the application and open it again, I would like to be able to access the
same value. I come from a Sybase (PowerBuilder) background. In PB this is thought of as a structure but in VS a structure is used differently,
i.e. , as far as I can tell, it does not persist.
I have been dealing with the problem in VS by saving whatever the value
is to a field in a table in a database. I use SQLServer as my database. This works but I have the feeling that there is probably another way and the other way may be more efficient. If it is not, then fine, I will
continue to use database tables to solve this but if there is a relatively easy
way to do it in a class, perhaps you could inform me.
Thank You.
Woody
What do you mean by an application in PB
Sorry for the misunderstanding. In my message I stated that I came from a
Sybase (PowerBuilder) background. PB is my shorthand for PowerBuilder.
Based on my experience, the registry is a good place to persist the data.
This is an interesting thought. Can you direct me to any additional
information on how to do this? Is this considered standard operating
procedure? Can it be done basically the same way on Win 2000 and EP
registrys?
Hi Woody,
Here is a link, you may take a look.
RegistryKey Class [Visual Basic] http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfMicrosoftWin32RegistryKeyClassTopic.asp
But are you sure you want to persist the data after a process is
terminated? If so there is related to Data persistence issue, for large
amount of data, you may also need to persist in the database or file.
If you just need to exchange data between different forms or threads, you
may try to use the global variables.
If you have any concern on this issue, please post here.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hi Woody,
Did you have any concern on this issue?
If so please post in the newsgroup.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hi Woody,
Did you have any concern on this issue?
If so please post in the newsgroup.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
14 posts
views
Thread by lkrubner |
last post: by
|
7 posts
views
Thread by Fuming Wang |
last post: by
|
1 post
views
Thread by mark4asp |
last post: by
|
2 posts
views
Thread by Patient Guy |
last post: by
|
25 posts
views
Thread by Daniel Bass |
last post: by
|
6 posts
views
Thread by Andrea Williams |
last post: by
|
17 posts
views
Thread by Davíđ Ţórisson |
last post: by
|
4 posts
views
Thread by DaveM |
last post: by
| | | | | | | | | | | |