Solved

parameter class

Posted on 2011-09-27
9
309 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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