Solved

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

Posted on 2014-11-03
5
303 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to copy file 14 92
Include MS Database in Winforms Project using Intellilock 5 34
How does this modal work? 3 34
junit initializtion error 2 22
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

752 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