Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a class in which there is a variable(not static) and a function which is used to assign that variable. i am doing as follows but it fails to assign. how to do it?

C#
public class myClass
    {
        public int a;        

        public void setit()
        {
           a = 5;
        }
    }


it shows a = 0 not 5
but i want here 5
Posted
Updated 19-Jun-16 19:16pm
v2

Are you instantiating myClass and then calling setit ?

var c = new myClass();
c.settit();
Console.WriteLine(c.a);


If you want it initialized at the time of construction, then just use:

public int a = 5;

or write a constructor:

public myClass()
{
  a = 5;
}


Marc
 
Share this answer
 
Comments
Maciej Los 20-Jun-16 1:17am    
5!
Your question suggests to me that you are very confused about basic aspects of programming in C#. That is not a "bad" thing ... remember that everyone here, at one time, was a beginner.

I strongly suggest you get a good book on C# and go over the "basics;" Charles Petzold's ".NET Book Zero" can be downloaded ... free ... here: [^].

Points to understand:

1. A Class definition, like your 'myClass, is a kind of abstraction; it's a blue-print for the construction of instances of the Class.

2. You create an instance of a Class with the 'new operator:

myClass InstanceOfMyClass = new myClass();

3. Once you have an instance you can then operate with/to/using (assign to, invoke, access) the public fields, properties, and methods of the Class:

InstanceOfMyClass.a = 100; // 'a will have the value #100<br />
// or<br />
InstanceOfMyClass.Setit(); // 'a will have the value #5


4. by creating public methods with parameters, you enable additional behavior:

<br />
public void SetAValue(int newavalue)<br />
{<br />
   a = newavalue;<br />
}


5. you can also use public properties to get/set values:

private int _a;<br />
public int A {<br />
    set { _a = value; }<br />
    get { return _a;  }<br />
}


Here the private integer variable '_a is called the "backing field" of the Property. .NET offers a simpler way to create a Property with a (hidden) private backing field:

public int A { set; get; }

Now, it's time for you to get to work; there is a certain amount of basic knowledge you need to master, and only you can do this. Get the book by Petzold, or another book on starting C#, and start experimenting. Most people learn by a combination of study and trial-and-error experiments.

You can get the free Visual Studio package, and ... get started.
 
Share this answer
 
v2
Comments
Maciej Los 20-Jun-16 1:17am    
5ed!
BillWoodruff 20-Jun-16 1:22am    
Thanks Maciej !
Karthik_Mahalingam 20-Jun-16 7:06am    
5!
BillWoodruff 20-Jun-16 7:12am    
Thanks, Karthik !
Satpal Lakhera 20-Jun-16 17:31pm    
well, i was trying it in a different way

but here i have a table in sql database as :

Create table SegmentDistribution
(
SegmentId int Identity(1,1) primary key,
SegmentName varchar(50),
SegmentShare float,
SegmentElasticity float,
SegmentStatus bit,
)

and i am trying to save these variable into a class as:

class myClass
{
int SegmentId;
string SegmentName;
single SegmentShare,
single SegmentElasticity,
int SegmentStatus,
}

How can i set those values from database to a class variables ?
should i use different stored procedure for each member ?
or there is any other way ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900