Solved

Can a  class object be passed as parameter in c#?

Posted on 2014-11-03
5
297 Views
Last Modified: 2014-11-03
Can a class object be passed as parameter in c#?

Thanks,
D.
0
Comment
Question by:developer2012
  • 2
  • 2
5 Comments
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 40420037
What do you mean "class object"?
Object of some class?
Class as a class type?

In both cases the answer is 'yes'.

See example:
        Object createObjectFrommClassType(Type myType){
    
            Object myObject = Activator.CreateInstance(myType);
            return myObject;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //possible usage
            object s = createObjectFrommClassType(typeof(Int32));

            System.Diagnostics.Debug.WriteLine("created object is string: " + (s.GetType().ToString() == "System.Int32"));
        }

Open in new window

But would you give more information on what's required?
0
 

Author Comment

by:developer2012
ID: 40420085
Just trying to pass the object of a class as a parameter..

Would I pass it as any other parameter?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 40420125
Mmmm, still not quite sure what's required.

Say, if you have a method (event handler in this case) like
private void button2_Click(object sender, EventArgs e)

Open in new window

then you pass two objects as parameters.

So, what is the problem?
Would this help?:
Passing Parameters (C# Programming Guide)
0
 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40420191
Short answer to your question.  Yes, a class can be passed as a parameter.
using System;

public class Program
{
	public class SomeClass
	{
		public int ID { get; set; }
		public string Name { get; set; }
	}

	public class SomeOtherClass
	{
		public SomeClass SomeClass { get; set; }

		public SomeOtherClass(SomeClass SomeClass)
		{
			this.SomeClass = SomeClass;
		}

		public override string ToString()
		{
			if (SomeClass != null)
				return string.Format("SomeClass is defined as: ID = {0}; Name = {1}", SomeClass.ID, SomeClass.Name);
			else
				return "SomeClass is not defined...";
		}
	}

	public static void Main()
	{
		SomeClass someClass = new SomeClass() { ID = 1, Name = "I am some class" };
		SomeOtherClass otherClass = new SomeOtherClass(someClass);
		Console.WriteLine(otherClass);

		// Wait for user input to close the program.
		Console.ReadLine();
	}
}

Open in new window


Produces the following output:Capture.JPG
What anarki is telling you is spot on.  Take for example his/her pointing out the button2_Click signature.  Object and EventArgs are both classes.

-saige-
0
 

Author Closing Comment

by:developer2012
ID: 40420508
Thanks Anarki and It_Sage.  Your answers were perfect !!!
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 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