friend keyword info

  • Thread starter Thread starter John Young
  • Start date Start date
J

John Young

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
 
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
 
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]
 
Sherif said:
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=value.ToString();
}
}

Use:

public cGlobals()
{
//
// TODO: Add constructor logic here
//
g_datasaved = true;
g_tenantid="NONE";
}

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
 
John said:
Definition:

internal static string g_tenantid
{
get
{
return g_tenantid;
}
set
{
g_tenantid=value.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="NONE";
}

Since g_tenantid is static, you cannot access it directly from an
instance constructor. Try cGlobals.TenantId = "NONE" or
cGlobals.g_tenantid = "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
 
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]
 
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
 
Back
Top