Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-03
5
Medium Priority
?
307 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 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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