Solved

parameter class

Posted on 2011-09-27
9
305 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
Comment Utility
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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
@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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
@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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

13 Experts available now in Live!

Get 1:1 Help Now