Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parameter class

Posted on 2011-09-27
9
Medium Priority
?
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 7

Accepted Solution

by:
Bill Nolan earned 668 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 664 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 668 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

719 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