Solved

parameter class

Posted on 2011-09-27
9
307 Views
Last Modified: 2012-06-21
i have two classes (.cs) in the same namespace..
one contains the parameters and the other class uses it in a method..
now class B contains a constant FIRSTCONST
how can i use it in class A for dispalymethod


namespace helloprogram
class A : interface1
{
  public void displaymethod()
      {
            console.writeline(FIRSTCONST );
      }
}


namespace helloprogram
class B
{
 public static int FIRSTCONST = "Hello"
}


all i want to do is i want to write all constants in one cs file and use them in another cs file
both are in same namespace
0
Comment
Question by:shragi
9 Comments
 
LVL 7

Accepted Solution

by:
Slimfinger earned 167 total points
ID: 36712741
You have to either create an instance of class B to reference the value:

      public void displaymethod()
      {
           B obj = new B();
            console.writeline(obj.FIRSTCONST );
      }

Or make class B static:

      public static class B { }

      public void displaymethod()
      {
            console.writeline(B.FIRSTCONST );
      }
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 36712774
You have to either create an instance of class B to reference the value:
You do NOT have to create an instance of a class to refer to a public static member. You simply refer to a static member by prefacing it with the class name, and not an instance name.

For example:

string value = B.FIRSTCONST;

Open in new window


You do not have to mark the class as static as Slimfinger suggested, though you certainly can if you like.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 167 total points
ID: 36712792
Declare it as a constant, not as a variable, so you will be able to use it straight on the class, without having to initialize an object for no reason.

namespace helloprogram
{
class A 
{
  public void displaymethod()
      {
            Console.writeline(B.FIRSTCONST );
      }
}
}


namespace helloprogram
{
class B
{
 public const string FIRSTCONST = "Hello";
}
}

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 36712803
To access a method, member or constan declared as static, use class name:

helloprogram.B.FIRSTCONST

You don´t need instance for static values.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36712846
@JamesBurger
Declare it as a constant, not as a variable, so you will be able to use it straight on the class, without having to initialize an object for no reason.
Static members are initialized when they are first referenced. You would not need to create an instance of the object in order to have a static member initialized--the first reference to the variable would invoke the class' static constructor (and static initializers).
0
 
LVL 40
ID: 36712892
@kaufmed

I know what static variables are.

I took the question as it was asked: class B contains a constant FIRSTCONST.

Although they can be used in a similar way, static variables and constants are not the same thing. They are not treated the same way by the compiler. If shragi needs a constant, we should show him how to declare a constant, not a fixed variable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36712940
I took the question as it was asked: class B contains a constant FIRSTCONST.
Ah, I missed "constant" in the OP. The original code, sans constant and your previous statement are in conflict, which is why I posted my comment. Taking the addition of the const modifier into account, then it makes a bit more sense  = )

Although they can be used in a similar way, static variables and constants are not the same thing.
Ne'er claimed they were  ; )
0
 

Author Comment

by:shragi
ID: 36712953
I want to declare a constant....

and more over when i create an object for class

as
classb obj = new classb()
and when I say
obj.

i don't see any constant after dot
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36712965
I don't know if this is simply a copy/paste error, but try marking your classes as public.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Class Library Dynamics For Connectstring Information 2 21
Icons and Colors for Terms 3 24
Error in JQuery 5 37
Point to Current Row Ater Refresh Datagridview 3 20
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now