Solved

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

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

Thanks,
D.
0
Comment
Question by:developer2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 30

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 30

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 34

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

622 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