Solved

Passing classes to methods

Posted on 2014-11-15
5
130 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
  • 2
  • 2
5 Comments
 
LVL 33

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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 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