Solved

Passing classes to methods

Posted on 2014-11-15
5
134 Views
Last Modified: 2014-11-16
Hello,
I have a WCF application which has a TransactionServices.cs . In here I create and instance of a User class called MyUser and an instance of an Environment class called MyEnvironment.  I have another class definition called Commands.cs in which I have defined various commands. These commands will require access to MyUser & MyEnvironment so I have created methods within Commands.cs to receive both of these classes from TransactionServices.cs

TransactionServices.cs
                MyCommands = new Commands();
                MyCommands.ReceiveMyUser(MyUser);
                MyCommands.ReceiveMyEnvironment(MyEnvironment);
                MyCommands.MyCommands("TESTCMD1");

Commands.cs
         public void ReceiveMyUser(UserMaster MyUser)
        {
        }
        public void ReceiveMyEnvironment(Environment MyEnvironment)
        {
        }

When ReceiveMyUser(UserMaster MyUser) is envoked from TransactionServices.cs I can see that MyUser is passed. However when ReceiveMyEnvironment(Environment MyEnvironment) is envoked, MyEnvironment is passed but MyUser is Null. When I call TESTCMD1 after these 2 receive methods , both MyUser & MyEnvironment are null. Why are these objects set to null each time I call MyCommands ?  Is there a better way of doing this ?

regards
Pat
0
Comment
Question by:pclarke7
[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 34

Expert Comment

by:it_saige
ID: 40444766
Can you provide the code for Commands.cs and TransactionServices.cs.  Also how do you instantiate Commands.cs???

-saige-
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 250 total points
ID: 40445007
What do the ReceiveMyUser and ReceiveMyEnvironment methods do? Your sample seems to imply they don't do anything. Do you expect their parameters to be stored somewhere?
From your description, they should be properties.
public UserMaster MyUser { get; set; }
public Environment MyEnvironment { get; set; }

Open in new window

0
 

Author Comment

by:pclarke7
ID: 40445927
Thanks to both of you for your comments,
Louis, you were correct. The problem was that I was not assigning the values. I mistakenly believed that because I passed in the same name that it would be automatically assigned

Commands.cs
         public void ReceiveMyUser(UserMaster MyUser)
        {
        }
        public void ReceiveMyEnvironment(Environment MyEnvironment)
        {
        }

When I changed it to the following, it worked

Commands.cs
         public void ReceiveMyUser(UserMaster PassedMyUser)
        {
            MyUser=PassedMyUser;
        }
        public void ReceiveMyEnvironment(Environment PassedMyEnvironment)
        {
          MyEnvironment=PassedMyEnvironment;
        }

It does seem a little odd that I have to receive in the object under a different name (PassedMyUser) just so I can assign it to the original name (MyUser).

Pat
0
 
LVL 34

Accepted Solution

by:
it_saige earned 250 total points
ID: 40446005
You don't have to use a different name, you just have to identify which MyUser you are referring to since there are two, one defined in the class and the other defined by the method.  This is usually accomplished by specifying *this* to represent the class defined variable, e.g. -
public void ReceivedMyUser(UserMaster MyUser)
{
	this.MyUser = MyUser;
}

Open in new window


-saige-
0
 

Author Closing Comment

by:pclarke7
ID: 40446025
Thanks for your help
Pat
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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