Solved

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

Posted on 2014-11-03
5
287 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 250 total points
Comment Utility
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
Comment Utility
Thanks Anarki and It_Sage.  Your answers were perfect !!!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now