Startup properties

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Can you store "public" variables that may be accessed from anywhere and
still have a Form (not SubMain) as the designated startup object ?

For example.... let's say I want all the form's Text property equal to
"Program Name and Version"

In a public class I tried....

Public Class SomeVariables

Public strProgNameVer as String = "My Name"

End Class

On the load of Form1 I placed

Me.Text = strProgNameVer

Nothing appeared in the text property of the form AND the program did not
crash due to lack of a declaration of strProgNameVer.

I kind of expected one or the other... either see the text, or crash due to
errror...

Thanks !
 
Rob said:
Can you store "public" variables that may be accessed from anywhere and
still have a Form (not SubMain) as the designated startup object ?

For example.... let's say I want all the form's Text property equal to
"Program Name and Version"

In a public class I tried....

Public Class SomeVariables

Public strProgNameVer as String = "My Name"

End Class

I don't think you can get away with it. The variable declaration has to be
in the scope of the class being used to be seen. Apparently, all you did up
above was make a local variable that can only be seen in the class above.

Public Class Form1 is a class.
On the load of Form1 I placed

Me.Text = strProgNameVer

Nothing appeared in the text property of the form AND the program did not
crash due to lack of a declaration of strProgNameVer.

Maybe, your project doesn't have Option Explicit set to (ON), which will
allow a none declared variable to be used, initialized to nullstring in this
case and the will not blow up because of it or show a compile error.
I kind of expected one or the other... either see the text, or crash due
to errror...

If you're not trying to do that below, then maybe you need to be using a
global Variableobj to hold and access global variables.

Public Class Form1
Public teststr As String = "Help"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Text = teststr

End Sub

End Class
 
So try this:

1) Create a Public Class Named Settings

2) in that class create a public shared member called name as string

Public Shared name As String = "Myname"

3) In your form you can now use the value Settings.name and get the string
'Myname' from it.
 
Thanks Ray !!!


Ray Cassick said:
So try this:

1) Create a Public Class Named Settings

2) in that class create a public shared member called name as string

Public Shared name As String = "Myname"

3) In your form you can now use the value Settings.name and get the string
'Myname' from it.
 
Ray Cassick said:
So try this:

1) Create a Public Class Named Settings

2) in that class create a public shared member called name as string

Public Shared name As String = "Myname"

3) In your form you can now use the value Settings.name and get the string
'Myname' from it.

You certainly cannot get away with this in C#. I see you can do this in
VB.net. I guess my question would be why would you do this?

Why wouldn't you just have a Module.vb with a Public Const set or even
Public Name as String = "Hello"?
 
Hi,

I think I did try that, but maybe i did something else wrong... ...
As to why not start with Sub Main() (which is my interpretation of what you
are saying)...
In VB.net - I could start out with Sub Main, however, then you lose other
startup options.... watch the check boxes when you do.

So, I was wanting to keep the Open Form as the first step, then access the
public varibles whenever I want them.

Thanks
 
Rob said:
Hi,

I think I did try that, but maybe i did something else wrong... ...

As to why not start with Sub Main() (which is my interpretation of what
you are saying)...
In VB.net - I could start out with Sub Main, however, then you lose other
startup options.... watch the check boxes when you do.

So, I was wanting to keep the Open Form as the first step, then access the
public varibles whenever I want them.

VB is a different animal. It's always been that way starting back to VB 3.0.
MS has kept some concepts in VB that don't lend itself towards good Oops
programming practices, like making a Public variable visible that can be
seen globally. One may be able to use this concept in doing Windows desktop
development, which should be on a limited basis, and it should never be used
in a Web solution.
 
Mr. Arnold said:
VB is a different animal. It's always been that way starting back to VB
3.0. MS has kept some concepts in VB that don't lend itself towards good
Oops programming practices, like making a Public variable visible that can
be seen globally. One may be able to use this concept in doing Windows
desktop development, which should be on a limited basis, and it should
never be used in a Web solution.

Two points...



1) I would have not really recommended this in a web solution. Web was not
mentioned.



2) The user has to be completely aware of the implication of using ANY
global variables.



When I went to college my professor told me that if one of my applications
was going to use a global variable I needed a note from my mom. I hate
talking to my mom so I never used them :)



Anyone can write crap in any language.



Just because VB lets you do things that maybe you should not do does not
make it a bad language. Hell if that was the case the inventor of C should
be shot in the head. But I am not going to get into a religious language
argument here.



He asked a question and I answered it.



Rob, He is right though. Any time you are considering using a global
variable the first thing you need to ask yourself is WHY? I have never seen
a case where it could not be solved another way that in the end gets you
better code that is simpler to maintain and preserves good OOP design.
 
Just because VB lets you do things that maybe you should not do does not
make it a bad language. Hell if that was the case the inventor of C should
be shot in the head. But I am not going to get into a religious language
argument here.

I never said VB was a bad language. I have used VB for many years, more
years that I would even care to remember. However, since VB.Net is
proprietary to MS and is not a standard, unlike C#.Net, then MS can take
liberties with VB.NET that will allow a developer to do things, because MS
doesn't have to answer to anyone.
 
Got it. Might have been on the offensive too quick there :)

Too many years of putting up with VB Bashers :)
 
I guess I don't understand why you think you can't get away with this in
C#. You certainly can create a public static variable in a Settings.cs
class and access it directly from the rest of the application.
 
Kelly Ethridge said:
I guess I don't understand why you think you can't get away with this in
C#. You certainly can create a public static variable in a Settings.cs
class and access it directly from the rest of the application.

lol, I have used the static keyword on a Class not a variable declaration. I
see that it can be set too. The use of this is just as bad in C# as it is in
VB with the Shared. I would most certainly not give this the light of day in
either language. :)
 
Thanks for all the input folks....

This is a windows app, not a web app.

Here is why I want to use a global variable....

I simply want to put the name and version of the program in the text
property of every form. If I have 15 forms in a program, then I must
remember to change it in the Load of every form. How best would I
accomplish this ? I chose not to start out with a module, because if I
do, some of the startup properties must be changed (like single instance
app - which I want to keep selected).

I feel like I am going to get bashed bad for this, but here goes...

Why are public variables so offensive ? Does it all have to do with
security (maybe use a constant instead) ? Are they just as bad as using
Public properties ? While we are on the topic, what is the BEST way to pass
variable between forms ?

Again, thank you for all your input !!!
 
Rob said:
Thanks for all the input folks....

This is a windows app, not a web app.

Here is why I want to use a global variable....

I simply want to put the name and version of the program in the text
property of every form. If I have 15 forms in a program, then I must
remember to change it in the Load of every form. How best would I
accomplish this ? I chose not to start out with a module, because if I
do, some of the startup properties must be changed (like single instance
app - which I want to keep selected).

The proper way to to this would be to have a Help About Menu option. If you
go to any Windows Desktop application on your machine like Word, Excel or
any desktop application of that nature, you will see the Help with an About
where this information is keep.

That's kind of a standard that is used. One doesn't put this information on
every form. You should be able to use Google and find out how to format an
Help About, even if you have to go back to a VB 6 example.

Maybe, you can find a book or an article about the guidelines for Windows UI
design.

http://www.google.com/search?hl=en&q=Microsoft+windows+Interface+guidelines&btnG=Search
I feel like I am going to get bashed bad for this, but here goes...

Why are public variables so offensive ? Does it all have to do with
security (maybe use a constant instead) ? Are they just as bad as using
Public properties ? While we are on the topic, what is the BEST way to
pass variable between forms ?

You should try to keep the scope of a varibale *local* as much as possible,
IMHO. I am sure you can find pros and cons of global vs local variable
usage.

http://www.softwareacademy.de/cpplernen/?cpp=Functions&ziel=Global and Local Scope

There is nothing stopping you from passing variables in a form's constructor
like any other class to keep the Local scope on variables.

http://www.internetcross.com/display/62.php

There is nothing worst than trying to find out the reasons for a Global
variable and its intended usage as a developer just flat-out becomes lazy
with the usage, and they are all over the place with different meanings
sometimes with re-use them for a different reason along the way. :)

IMHO, if you need Gobal variables or even functions at times, then put them
in a public object.
 
Back
Top