Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I give my class a specific input and if I can, how can I give my class an input like:
C#
Foo foo = new Foo()
foo = 1.A;
Posted
Updated 24-Jan-14 19:53pm
v2
Comments
BillWoodruff 25-Jan-14 3:54am    
What is 1.A ?

If your goal here is that to initialize a Field, or Property, of a Class at the time an instance of the Class is created, why not use a constructor, or multiple constructors, if needed.
C#
public class Foo
{
    // public Field of a 'foo
    public int FooID;

    // public Property of a 'foo
    // only the class can set the value of FooString1
    public string FooString1 { get; private set; }

    // public Property of a 'foo
    // the value of FooString2 can be set from within
    // the class or from outside the class
    // since there's no method in the class that sets
    // its value, then it must be set outside the class
    public string FooString2 { get; set; }

    // parameterless constructor #1:
    public Foo()
    {
    }

    // constructor #2:
    public Foo(int fooId, string fooStr)
    {
        FooID = fooId;
        FooString1 = fooStr;
    }
}
Test:
Foo myFoo1 = new Foo();

Foo myFoo2 = new Foo(111, "Foo Two");

// set the value of FooString2 in myFoo2
myFoo2.FooString2 = "String Two of Foo Two";
Note that because no calls are made to :base() in Foo, the parameterless constructor can be omitted ... if not used.
 
Share this answer
 
Comments
Karthik_Mahalingam 25-Jan-14 13:17pm    
5, neat
Rahul VB 25-Jan-14 13:32pm    
hey Karthik how was the movie? and which was it? i am sorry if i asked this question in a wrong forum.
Karthik_Mahalingam 25-Jan-14 13:36pm    
Hi Rahul,
Text only related to CP.
for unofficial conversations, you can catch me at removed@gmail.com :)
Matt T Heffron 25-Jan-14 13:52pm    
Unless you REALLY LOVE spam, don't include your email in postings!
The email harvesters seem to look EVERYWHERE!
Karthik_Mahalingam 25-Jan-14 13:53pm    
Yes Matt i know, For short duration only.
Removed :)
you can try like this


C#
{

           Foo foo = new Foo(); //creating a new instance for Foo class

           Foo fooTemp = GetFoo(); // Getting the Foo object from a method.

           foo = fooTemp;  // Assigning the new Foo object to foo


       }

       public   Foo GetFoo() // Method to get Foo object
       {
               // your coding here
           return new Foo();
       }
 
Share this answer
 
Comments
Rahul VB 25-Jan-14 13:36pm    
+5 : "MOM". This code needs a very implicit understanding, but very nice. You truly are "MOM": man of the moment. Every question and you are always bang on. How do you do it? Well one of the toughest contender is OG(original griff):).

Actually you know this "Foo" stuff sometimes confuses me. But i have got used to it. Great.
Karthik_Mahalingam 25-Jan-14 13:39pm    
Thanks Rahul :)

In programming world they use the Word FOO often, even i wonder why they use it. rather than using some nice names :)
Rahul VB 25-Jan-14 13:41pm    
hey and you colored my word! how did you do that? and yes this "foo" stuff is bad.
Karthik_Mahalingam 25-Jan-14 13:43pm    
check this http://en.wikipedia.org/wiki/Cross-site_scripting
Karthik_Mahalingam 25-Jan-14 13:45pm    
its simple :
<b style="color:red"> your text here </b> ;
Hi. generally you can add property to your class when your class needs any external data for working.also properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. for example :
C#
#region Properties

private string _FirstName;
public string FirstName
{
    get { return _FirstName; }
    set { _FirstName = value; }
}

private string _LastName;
public string LastName
{
    get { return _LastName; }
    set { _LastName = value; }
}

#endregion

#region Constructor

public Person(string firstName, string lastName)
{
    this.FirstName = firstName;
    this.LastName = lastName;
}

#endregion

#region Methods

public string Greeting()
{
    string message = string.Format("Hi {0} {1}", this.FirstName, this.LastName);
    return message;
}

#endregion


now you can create instance of Person class like following:
Person myPer = new Person();
myPer.FirstName = "ali";
myPer.Greeting();
 
Share this answer
 
Hello friend,

firstly
Quote:
Can I give my class a specific input


Definately you can, to give inputs to classes itself the concept of parametrized constructor was introduced.

Constructors are of 2 types:
Quote:
parameterless and parameterized


The default is called a parameterless constructor:
Now suppose you create a new class like this:
C#
public class Class1
   {
   }


From the main method i can call it like this:
Class1 c = new Class1();


- Take notice: Are you passing any parameters inside the parenthesis of new Class1(). No you are not. It is "parameterless". Thats why i call it a parameterless constructor.


Now lets say according to some situation i want to initialize a method with a value when a class is initialized( At the start when the class is initialized) , then you can do this:

Class1 c1 = new Class1("Rahul");



and in Class1 i will say:

C#
public Class1(string s)
       {
           string a = s;
           Console.WriteLine(a);
       }



Now summing up everything i can say:
In Class1:
C#
public class Class1
   {

       public Class1()
       {

       }

       public Class1(string s)
       {
           string a = s;
           Console.WriteLine(a);
       }


   }



In the main i can say:
Class1 c = new Class1();
           Class1 c1 = new Class1("Rahul");


These 2 above statements are called
Quote:
overloads of a class


Here there are just 2 overloads, you can specify many overloads.

hey friend just one homework: try putting static "keyword" before any constructor for example :


C#
public static Class1(string s)
    {
        string a = s;
        Console.WriteLine(a);
    }


- Now compile the code and tell me what happens? and why?

good luck friend. I hope this has helped you. And i hope you have a plethora of doubts on this. And please ask me so that even i gain knowledge from your doubts.


Thanks and happy learning
 
Share this answer
 
v2
Greetings,

You can define a property in your class and set the value to that property.

For Example:
C#
class Foo
   {
       int id = -1;
       public int ID
       {
           set { id = value; }
           get { return id; }
 
       }
   }
 
  Foo f = new Foo();
  f.ID = 100;

Regards
Dominic
 
Share this answer
 
v3

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