Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parameter class

Posted on 2011-09-27
9
Medium Priority
?
317 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:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

916 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