473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Read private variables between forms

Hello,
who gives one more shining solution to me?
My application load form "A". The form "A" by means of a push-button load
form "B" that in its turn by means of a push-button load form "C". Form "B"
contains one variable "Private"
"VarB". How I can read the value of "VarB" from form "C"? All
the form come loaded with the following brace of instructions:

Sub Button2_Click(ByVal sender As System.Object, ByVal and As
System.EventArgs) Handles Button2.Click
Dim frm As New frm<A/B/C >
frm.ShowDialog()
End Sub
In form "B" have declared a Property "RetVarB" that encapsulates the value
of "VarB".
From form "C" I succeed to read the value of "VarB" with the instruction:
m_varC =frm.RetVarB
if I declare "frm" like variable it only Public of the
Form "B". Not creed but that this is the only possible method. To me
it does not appeal to declare like "Public" the variable one "frm"
in the form "B"

Hello and Thanks.
Nov 21 '05 #1
2 1593
Add a module to your project, call it something like "globals" which
contains your variable. If you set the variable with the "Private" keyword,
you will not be able to read that variable in ANY of your forms. But if you
set it with the "Public" keyword, it will be accessible to all your forms --
from anywhere in your application.

BUT. that goes without saying: "Don't expose what you don't have to." which
basically means, you SHOULD set your variable with the "Private" keyword,
and then add either a "ReadOnly" accessor to read the variable.

For example:
Private _MyVariable As String 'this is a private variable: note the "_"
before the variable name.
'the accessor to read the value
Public ReadOnly Property MyVariable() As String 'note the readonly keyword
Get
Return _MyVariable 'Returns the value of the variable
End Get
End Property

Now if you want to read & write to the variable:
Private _MyVariable As String 'this is a private variable: note the "_"
before the variable name.
'the accessor to read the value
Public ReadOnly Property MyVariable() As String 'note the readonly keyword
Get
Return _MyVariable 'Returns as string value
End Get
Set (ByVal Value As String)
_MyVariable = Value 'accepts a string value
End Set
End Property

By Adding a module that is separate from your forms, you can contain any
number of variables with different accessors. You are not limited to
accessors to read/write to variables, you can use Subroutines "Sub" or
Functions. Example:

Public Sub SetMyVariable(ByVal Str As String)
'set the variable
_MyVariable = Str 'note Str is the parameter that in the routine.
End Sub

Public Funtion MyVariable(ByVal Str As String)
Try
'check to see if you're setting the variable
'or wanting to read from it
Select Case Str
Case "" 'nothing is being passed to the function
Case Else 'something is being passed to the function
'handle the case here
End Select
Catch (Ex As Exception)
Console.WriteLine("Exception: " & Ex.toString())
Exit Function
End Try
End Function

These are just a few examples of how you can deal with variables over
multiple forms. There are other, more efficient ways, but you'll discover
those on your own.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #2
One other thing: Regarding WinForms -- they are objects just like
everything in .NET so they should be treated as such. If you've got
variables that are contained in one form and you want to read them in
another form, you'll run into a hell of a lot of headaches, expecially if
you're destroying the form (object) after you close it -- which is what you
SHOULD do -- that is the reason you want to make sure variables that are
shared throught your application are centralized in a spot that can be
easily accessed by ANY form.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
5
by: Oleg Ogurok | last post by:
Hi all, Is there a way to read other people's session variables? I understand it makes sense that session state is on per-user basis, but still... Is there a way to get a collection of all...
5
by: John Bradley | last post by:
Toooo tired to look this one up. I have data that I need to acces in a small program. The data is accessable from an ASP webpage that I created myself. If I need to see data from record #25, I...
2
by: Vish | last post by:
Hi, I amplanning on having a rea-only and edit states for my form. But it do not want my form and its controls to look different or disabled. I am planning on having a edit button that brings...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
1
by: Shashank | last post by:
Hi all, I am a new member of this community. I am making a http request to a html file placed on a Apache server. On this page there is an embeded perl statement which requires reading ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.