Hi,
I have a class (cGlobals.cs) which I create in my main form (frmMain). I
have declared a variable at the top of my frmMain class (cGlobals gVars;).
I then create an instance in my forms Load event (gVars=new cGlobals;).
Now, I can access gVars in my main form, but not from any other form. Do I
need to add anything to my cGlobals.cs file to allow all forms to access the
class? I've seen references to friend/internal etc., but am not 100% sure
on how to use them.
Thank you for any help given...
John Young 8 1178
Hello
There is no friend keyword in C#, only in C++. You have to use internal,
which will make the member available to all other classes in the assembly
Best regards,
Sherif
"John Young" <polomint77@bla hblahblah_klbja y.freeserve.co. uk> wrote in
message news:un******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I have a class (cGlobals.cs) which I create in my main form (frmMain). I have declared a variable at the top of my frmMain class (cGlobals gVars;). I then create an instance in my forms Load event (gVars=new cGlobals;). Now, I can access gVars in my main form, but not from any other form. Do
I need to add anything to my cGlobals.cs file to allow all forms to access
the class? I've seen references to friend/internal etc., but am not 100% sure on how to use them.
Thank you for any help given...
John Young
Declaring a variable in the form means that EACH frmMain instance will have
a separate instance of that variable. You might only have one instance of
the form in your application during runtime, but the variable isn't TRULY
global (or static, as we say in the C# world).
In your cGlobals class, make sure all the global fields are prefixed with
"static". Next, make Properties to access those fields, and prefix the
Properties with "internal static". Now you don't need to create a "new"
instance of the cGlobals class. You can access any of the static properties
anywhere in your project by calling cGlobals.XXXX (where XXXX is an internal
static property of that class).
-Rob Teixeira [MVP]
"John Young" <polomint77@bla hblahblah_klbja y.freeserve.co. uk> wrote in
message news:un******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I have a class (cGlobals.cs) which I create in my main form (frmMain). I have declared a variable at the top of my frmMain class (cGlobals gVars;). I then create an instance in my forms Load event (gVars=new cGlobals;). Now, I can access gVars in my main form, but not from any other form. Do
I need to add anything to my cGlobals.cs file to allow all forms to access
the class? I've seen references to friend/internal etc., but am not 100% sure on how to use them.
Thank you for any help given...
John Young
Sherif ElMetainy wrote: Hello
There is no friend keyword in C#, only in C++. You have to use internal, which will make the member available to all other classes in the assembly
To clarify, the object within the form (not the class itself) should be
marked internal. If you prefer, you may keep it private and define
accessor methods with the form to control access to the variable.
--
There are 10 kinds of people. Those who understand binary and those who
don't. http://code.acadx.com
(Pull the pin to reply)
Thanks for the quick answers. One problem though. When I try to set the
value of one of the variables, I get a stack overflow exception. Here's the
definition, and code snippet of how I use it..
Definition:
internal static string g_tenantid
{
get
{
return g_tenantid;
}
set
{
g_tenantid=valu e.ToString();
}
}
Use:
public cGlobals()
{
//
// TODO: Add constructor logic here
//
g_datasaved = true;
g_tenantid="NON E";
}
Does that look correct to you. I have removed all references in my source
to do with new(ing) an instance of the class.
Also, a quicky. Is there any way of copying and pasting code between VS2003
and OE which will display in OE correctly. All the formatting goes haywire
!
Thanks again..
John
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:uc******** ******@tk2msftn gp13.phx.gbl... Declaring a variable in the form means that EACH frmMain instance will
have a separate instance of that variable. You might only have one instance of the form in your application during runtime, but the variable isn't TRULY global (or static, as we say in the C# world).
In your cGlobals class, make sure all the global fields are prefixed with "static". Next, make Properties to access those fields, and prefix the Properties with "internal static". Now you don't need to create a "new" instance of the cGlobals class. You can access any of the static
properties anywhere in your project by calling cGlobals.XXXX (where XXXX is an
internal static property of that class).
-Rob Teixeira [MVP]
"John Young" <polomint77@bla hblahblah_klbja y.freeserve.co. uk> wrote in message news:un******** ******@TK2MSFTN GP12.phx.gbl... Hi,
I have a class (cGlobals.cs) which I create in my main form (frmMain).
I have declared a variable at the top of my frmMain class (cGlobals
gVars;). I then create an instance in my forms Load event (gVars=new cGlobals;). Now, I can access gVars in my main form, but not from any other form.
Do I need to add anything to my cGlobals.cs file to allow all forms to access the class? I've seen references to friend/internal etc., but am not 100%
sure on how to use them.
Thank you for any help given...
John Young
John Young wrote: Definition:
internal static string g_tenantid { get { return g_tenantid; } set { g_tenantid=valu e.ToString(); } }
Why is the property marked internal? Also, why are you calling the
ToString method when the property can only return/accept a string to
start with? Last but not least, you're getting an overflow expception
because your property calls itself. Chang the name of the property to
something like TenantId.
Use:
public cGlobals() { // // TODO: Add constructor logic here // g_datasaved = true; g_tenantid="NON E"; }
Since g_tenantid is static, you cannot access it directly from an
instance constructor. Try cGlobals.Tenant Id = "NONE" or
cGlobals.g_tena ntid = "NONE".
Does that look correct to you.
Actually, your class looks a bit mangled. I'd recommend you clean it up
before moving further.
--
There are 10 kinds of people. Those who understand binary and those who
don't. http://code.acadx.com
(Pull the pin to reply)
> Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId.
I was following the information given by Rob Teixeira [MVP]. I was calling
the ToString method as a test to see if that may clear up the stack problem
(not sure really, but sometimes the strangest things work. The name of the
property was my mistake, I must not have been thinking straight at the
time..
Thanks for the pointers.
John
"Frank Oquendo" <fr*******@acad x.com> wrote in message
news:Oj******** ******@TK2MSFTN GP09.phx.gbl... John Young wrote:
Definition:
internal static string g_tenantid { get { return g_tenantid; } set { g_tenantid=valu e.ToString(); } }
Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId.
Use:
public cGlobals() { // // TODO: Add constructor logic here // g_datasaved = true; g_tenantid="NON E"; }
Since g_tenantid is static, you cannot access it directly from an instance constructor. Try cGlobals.Tenant Id = "NONE" or cGlobals.g_tena ntid = "NONE".
Does that look correct to you.
Actually, your class looks a bit mangled. I'd recommend you clean it up before moving further.
-- There are 10 kinds of people. Those who understand binary and those who don't.
http://code.acadx.com (Pull the pin to reply)
Your Property Set block is writing a value back to itself (and keeps doing
this over and over and over... until you run out of stack space).
You need a separate field to hold the value. Try this in your class:
private static string m_TenantID;
internal static string TenantID
{
get { return m_TenantID; }
set { m_TenantID = value; }
}
The internal keyword keeps the values from being published outside the
assembly (which is what I assume you want). If you want the values to be
published outside the assembly, then switch the property access modifier to
"public" instead. I'm a fan of explicit coding.
-Rob Teixeira [MVP]
"John Young" <polomint77@bla hblahblah_klbja y.freeserve.co. uk> wrote in
message news:uL******** ********@TK2MSF TNGP10.phx.gbl. .. Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId. I was following the information given by Rob Teixeira [MVP]. I was
calling the ToString method as a test to see if that may clear up the stack
problem (not sure really, but sometimes the strangest things work. The name of
the property was my mistake, I must not have been thinking straight at the time..
Thanks for the pointers.
John
"Frank Oquendo" <fr*******@acad x.com> wrote in message news:Oj******** ******@TK2MSFTN GP09.phx.gbl... John Young wrote:
Definition:
internal static string g_tenantid { get { return g_tenantid; } set { g_tenantid=valu e.ToString(); } }
Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId.
Use:
public cGlobals() { // // TODO: Add constructor logic here // g_datasaved = true; g_tenantid="NON E"; }
Since g_tenantid is static, you cannot access it directly from an instance constructor. Try cGlobals.Tenant Id = "NONE" or cGlobals.g_tena ntid = "NONE".
Does that look correct to you.
Actually, your class looks a bit mangled. I'd recommend you clean it up before moving further.
-- There are 10 kinds of people. Those who understand binary and those who don't.
http://code.acadx.com (Pull the pin to reply)
You are right about only wanting it published inside my assembly. I
realised about the property problem as I started reading the post from
Frank. Thanks again (although I'm sure I'll have other questions later
<g>).
John
"Rob Teixeira [MVP]" <RobTeixeira@@m sn.com> wrote in message
news:#i******** ******@TK2MSFTN GP11.phx.gbl... Your Property Set block is writing a value back to itself (and keeps doing this over and over and over... until you run out of stack space).
You need a separate field to hold the value. Try this in your class:
private static string m_TenantID;
internal static string TenantID { get { return m_TenantID; } set { m_TenantID = value; } }
The internal keyword keeps the values from being published outside the assembly (which is what I assume you want). If you want the values to be published outside the assembly, then switch the property access modifier
to "public" instead. I'm a fan of explicit coding.
-Rob Teixeira [MVP]
"John Young" <polomint77@bla hblahblah_klbja y.freeserve.co. uk> wrote in message news:uL******** ********@TK2MSF TNGP10.phx.gbl. .. Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId.
I was following the information given by Rob Teixeira [MVP]. I was
calling the ToString method as a test to see if that may clear up the stack problem (not sure really, but sometimes the strangest things work. The name of the property was my mistake, I must not have been thinking straight at the time..
Thanks for the pointers.
John
"Frank Oquendo" <fr*******@acad x.com> wrote in message news:Oj******** ******@TK2MSFTN GP09.phx.gbl... John Young wrote:
> Definition: > > internal static string g_tenantid > { > get > { > return g_tenantid; > } > set > { > g_tenantid=valu e.ToString(); > } > }
Why is the property marked internal? Also, why are you calling the ToString method when the property can only return/accept a string to start with? Last but not least, you're getting an overflow expception because your property calls itself. Chang the name of the property to something like TenantId.
> Use: > > public cGlobals() > { > // > // TODO: Add constructor logic here > // > g_datasaved = true; > g_tenantid="NON E"; > }
Since g_tenantid is static, you cannot access it directly from an instance constructor. Try cGlobals.Tenant Id = "NONE" or cGlobals.g_tena ntid = "NONE".
> Does that look correct to you.
Actually, your class looks a bit mangled. I'd recommend you clean it
up before moving further.
-- There are 10 kinds of people. Those who understand binary and those
who don't.
http://code.acadx.com (Pull the pin to reply)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ike Naar |
last post by:
Given the following C++ snippet:
template < typename T > class A { // 1
public : // 2
class AA { } ; // 3
} ; // 4
// 5
template < typename T > class B { // 6
friend class A < T > :: AA ; // 7
} ; // 8
|
by: Teddy |
last post by:
Hello all
consider the class Date declaretion below:
class Date {
public:
Date();
Date(int year, int month, int day);
Date(const string&);
int getYear() const;
|
by: Steve |
last post by:
I'm just curious, why did give VB.Net a Friend keyword but not C#?
|
by: Jesper |
last post by:
I need to grant a class access to protected fields of
another class in the way its possible in C++ with the
friend keyword. However I would like to keep the class
protected towards other class within the same
program/assembly. The two classes are not 'related'
(inherited).
How can I do this. I cant see how the keyword Internal
can be used...
|
by: Ben Galvin |
last post by:
Hi,
I'm looking for an equivalent to the C++ 'friend' keyword in C# (for those
who don't know, this lets you give a specific class access to all the
private/protected members of another class).
I'm trying to write an object persistence mechanism where all of the
persistence logic for each class is situated in a seperate class (the Mapper...
| |
by: Howard Swope |
last post by:
I keep running into places in my code where I would like only a particular
class of object to be able to access certain members of another class of
object. In C++ the friend keyword provided this functionality. The only
thing I have found in C# is internal but this is still broader in scope than
I am looking for.
Is there a way in C# to get...
|
by: Paul Cheetham |
last post by:
Hi,
I am writing an application with a large number of various classes, and
I want some of them to have Friend access to protected members of other
classes.
i.e. I want class A to have access to internal members of Class B
In C++ this was simple, as in the definition for class B, I would
declare calss A as a friend.
Seeing that C# had a...
|
by: Filimon Roukoutakis |
last post by:
Dear all,
I have the following concept
name {
space1 {
class Friend {
};
|
by: Immortal Nephi |
last post by:
I design a class for general purpose. I do not allow a client to read
or modify interface and implemention. I allow them to write a new non-
member function outside of class' interface and implmention. The
problem is that non-member function cannot access private data member.
The friend keyword is not the solution. I am aware that friend...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |