Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing classes to methods

Posted on 2014-11-15
5
Medium Priority
?
137 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 35

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 1000 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 35

Accepted Solution

by:
it_saige earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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