Two classes which are very similar, I want to pass one or the other into a method
up vote
-1
down vote
favorite
I have two classes, consider the following:
public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.
public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}
if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}
How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.
The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.
I'm not sure how to achieve this.
c# interface
add a comment |
up vote
-1
down vote
favorite
I have two classes, consider the following:
public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.
public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}
if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}
How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.
The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.
I'm not sure how to achieve this.
c# interface
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
2
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have two classes, consider the following:
public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.
public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}
if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}
How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.
The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.
I'm not sure how to achieve this.
c# interface
I have two classes, consider the following:
public class Class1
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
public class Class2
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
}
I want to then write a method which does something with either one of these methods, but my program won't know which one until runtime.
public void DoSomething(???)
{
if (???.GetType() == typeof(Class1)
{
//do stuff related to class 1
}
if (???.GetType() == typeof(Class2)
{
//do stuff related to class 2
}
}
How am I able to pass one of those classes into that method, considering they are different types? I then need to check the type to perform separate actions.
The way this will be used in my application is that Class1 and Class2 will be linked to a ParentClass, and on that ParentClass it will dictate which linked object should be used (either Class1 and Class2) e.g. ParentClass will have a bool on it, and if bool is true, then use Class1 else use Class2, and depending on that boolean is what I want to pass into that method. I don't want to create a separate method as I think it will be best to do this.
I'm not sure how to achieve this.
c# interface
c# interface
asked Nov 9 at 15:29
user3046756
104
104
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
2
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33
add a comment |
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
2
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
2
2
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Use a base class with the properties in common.
The classes derive from it with an implementation of the method DoSomeThing
.
Then, your method DoSomeThing
is called autotomatically for the right type,
public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}
public abstract Class1
{
public override void DoSomeThing(){}
}
public abstract Class
{
public override void DoSomeThing(){}
}
public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}
add a comment |
up vote
0
down vote
Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.
public static void DoSomething(IDoSomething MyObject)
{
MyObject.DoSomeMethod();
}
interface IDoSomething
{
void DoSomeMethod();
}
public class Class1 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
public class Class2 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
add a comment |
up vote
-2
down vote
you could try somehing like this:
public static void DoSomething(object MyObject)
{
if (MyObject.GetType() == typeof(Class1))
{
// (MyObject as Class1 )
}
if (MyObject.GetType() == typeof(Class2))
{
// (MyObject as Class2 )
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Use a base class with the properties in common.
The classes derive from it with an implementation of the method DoSomeThing
.
Then, your method DoSomeThing
is called autotomatically for the right type,
public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}
public abstract Class1
{
public override void DoSomeThing(){}
}
public abstract Class
{
public override void DoSomeThing(){}
}
public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}
add a comment |
up vote
1
down vote
Use a base class with the properties in common.
The classes derive from it with an implementation of the method DoSomeThing
.
Then, your method DoSomeThing
is called autotomatically for the right type,
public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}
public abstract Class1
{
public override void DoSomeThing(){}
}
public abstract Class
{
public override void DoSomeThing(){}
}
public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Use a base class with the properties in common.
The classes derive from it with an implementation of the method DoSomeThing
.
Then, your method DoSomeThing
is called autotomatically for the right type,
public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}
public abstract Class1
{
public override void DoSomeThing(){}
}
public abstract Class
{
public override void DoSomeThing(){}
}
public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}
Use a base class with the properties in common.
The classes derive from it with an implementation of the method DoSomeThing
.
Then, your method DoSomeThing
is called autotomatically for the right type,
public abstract BaseClass
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public abstract void DoSomeThing();
}
public abstract Class1
{
public override void DoSomeThing(){}
}
public abstract Class
{
public override void DoSomeThing(){}
}
public void DoSomething(BaseClass class)
{
class.DoSomeThing();
}
edited Nov 9 at 15:46
Manfred Radlwimmer
10.3k123350
10.3k123350
answered Nov 9 at 15:33
Antoine V
4,9472424
4,9472424
add a comment |
add a comment |
up vote
0
down vote
Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.
public static void DoSomething(IDoSomething MyObject)
{
MyObject.DoSomeMethod();
}
interface IDoSomething
{
void DoSomeMethod();
}
public class Class1 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
public class Class2 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
add a comment |
up vote
0
down vote
Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.
public static void DoSomething(IDoSomething MyObject)
{
MyObject.DoSomeMethod();
}
interface IDoSomething
{
void DoSomeMethod();
}
public class Class1 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
public class Class2 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.
public static void DoSomething(IDoSomething MyObject)
{
MyObject.DoSomeMethod();
}
interface IDoSomething
{
void DoSomeMethod();
}
public class Class1 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
public class Class2 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
Just my preference is to do interfaces over inheritance if I can. Allows to be a little more flexible I think in the future.
public static void DoSomething(IDoSomething MyObject)
{
MyObject.DoSomeMethod();
}
interface IDoSomething
{
void DoSomeMethod();
}
public class Class1 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
public class Class2 : IDoSomething
{
public int CustomerID;
public int DriverID;
public int EmployeeID;
public void DoSomeMethod()
{
throw new NotImplementedException();
}
}
answered Nov 9 at 15:46
Ryan Schlueter
1,5871816
1,5871816
add a comment |
add a comment |
up vote
-2
down vote
you could try somehing like this:
public static void DoSomething(object MyObject)
{
if (MyObject.GetType() == typeof(Class1))
{
// (MyObject as Class1 )
}
if (MyObject.GetType() == typeof(Class2))
{
// (MyObject as Class2 )
}
}
add a comment |
up vote
-2
down vote
you could try somehing like this:
public static void DoSomething(object MyObject)
{
if (MyObject.GetType() == typeof(Class1))
{
// (MyObject as Class1 )
}
if (MyObject.GetType() == typeof(Class2))
{
// (MyObject as Class2 )
}
}
add a comment |
up vote
-2
down vote
up vote
-2
down vote
you could try somehing like this:
public static void DoSomething(object MyObject)
{
if (MyObject.GetType() == typeof(Class1))
{
// (MyObject as Class1 )
}
if (MyObject.GetType() == typeof(Class2))
{
// (MyObject as Class2 )
}
}
you could try somehing like this:
public static void DoSomething(object MyObject)
{
if (MyObject.GetType() == typeof(Class1))
{
// (MyObject as Class1 )
}
if (MyObject.GetType() == typeof(Class2))
{
// (MyObject as Class2 )
}
}
answered Nov 9 at 15:43
Misael Moneró Thompson
193111
193111
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228672%2ftwo-classes-which-are-very-similar-i-want-to-pass-one-or-the-other-into-a-metho%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You could define an interface containing the common method and properties and change the two classes so that both implement it. Then define your method to take a parameter of this interface type
– Klaus Gütter
Nov 9 at 15:33
2
You've tagged this post with "interface", but you're not using one. An interface would solve this problem.
– gunr2171
Nov 9 at 15:33