Click here to Skip to main content
15,881,873 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
How can we add values to a object. i have created a object and i want to assign some values to it.

Ex:
C#
object obj;

I have two values say number,mobile. and i want to add these values to the object obj.
if i type obj.number,obj.mobile the values has to display

Regards,
S.inayat Basha.
Posted
Updated 22-Dec-11 12:56pm
v3

Most of the time, this is accomplished by way of "properties":
C#
public class MyObject 
{
    public int Number { get; set; }
    public string Mobile { get; set; }
}

And you set them like this:
C#
MyObject myObject = new MyObject();
myObject.Number = 0;
myObject.Mobile = "Abcdefg";

This is all EXTREMELY basic .Net knowledge. I recommend that if you're not familiar with .Net, you buy a book or take a class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Dec-11 13:16pm    
Yes, yes, you are right, my 5, but perhaps you are trying to fix the something OP cannot fix right now. I explain it in my answer, please see.
--SA
Uday P.Singh 22-Dec-11 13:28pm    
5ed :)
Monjurul Habib 22-Dec-11 14:05pm    
nice answer,5!
The object data type (i.e. the mother of all the other types) has NOT (of course) such 'values' (i.e. data members). You have to first define your own class, for instance:
C#
class MyObject
{
   public string number;
   public string mobile;
   public MyObject( string n, string m ){ number = n; mobile = m;}
}


and then use the way you prefer (if legal), for instance
C#
object obj;
obj = new MyObject("666666666666", "999999999999");
 
Share this answer
 
Comments
inayat basha 22-Dec-11 9:29am    
i am using this object in a aspx.cs file.. and i want to write the code in same page only
thatraja 22-Dec-11 9:40am    
Just call the class in your code-behind
inayat basha 22-Dec-11 9:46am    
if i am getting mutiple number then how can i add ???
#realJSOP 22-Dec-11 13:19pm    
You REALLY need to take a class and LEARN about what you're trying to do.
Sergey Alexandrovich Kryukov 22-Dec-11 13:16pm    
Yes, yes, you are right, my 5, but perhaps you are trying to fix the something OP cannot fix right now. I explain it in my answer, please see.
--SA
You are trying to use the paradigm totally foreign to all .NET languages. Your thinking resembles JavaScript and looks totally exotic to all classical OP languages including all of the .NET, C++, Object Pascal, Python, Eiffel, whatever.

All answers on this page are correct, but your thinking is too far from the topics people are writing about. I'm not saying your thinking is wrong, it's just from a very different background, having almost nothing in common with classical OOP paradigm used by .NET. You should start with learning classical OOP paradigm from the very beginning.

Take a look: http://en.wikipedia.org/wiki/Object-oriented_programming[^].

And them, take an elementary manual on C# and .NET and try to understand the very basics. This is actually much easier to understand then the paradigm you might have come from.

—SA
 
Share this answer
 
Comments
CPallini 22-Dec-11 13:28pm    
Good advice, indeed. My 5+.
Sergey Alexandrovich Kryukov 22-Dec-11 13:52pm    
Thank you very much.
--SA
Monjurul Habib 22-Dec-11 14:06pm    
nice answer,5!
Sergey Alexandrovich Kryukov 22-Dec-11 14:08pm    
Thank you, Monjurul.
--SA
And when you are a bit more experienced you can have a look at Attached Properties.
This allows you to assign foreign properties to any (Dependency)Object.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Dec-11 13:18pm    
Correct, but OP is trying to use "Object". He would not be able to understand you, because his confusion is much deeper -- this is totally different paradigm, totally foreign to .NET. I explain it in my answer, please see.
--SA
Henning Dieterichs 22-Dec-11 16:15pm    
In which way this is a total different "paradigm" (assuming the use of this is not exaggerated so that the object does (or is) nothing)?
I would call it "seperation of concerns" as differnt domains can have independet properties on model-objects.
And that attached properties are totally foreign to .Net is not exactly true:
Extension Methods are in principle the same - and are very usefull when you want to avoid dependencies.
Beside this: My solution is only an alternative - it doesn't mean that I would use this in every case, but there are cases, in which this could be really helpful.
How about using a dictionary in your object , for example :
C#
public class Person
{
  public Person()
  {

  }

  public Dictionary<string, string> Numbers = new Dictionary<string,string>();
}


And use it in this way :
C#
Person person = new Person();
person.Numbers.Add("land tel", "+1 998 7766676");
person.Numbers.Add("mobile", "+1 878 897878");

foreach (var number in person.Numbers)
{
    Console.WriteLine("{0} = {1}", number.Key, number.Value);
}



Hope it helps.
 
Share this answer
 

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