Solved

Passing classes to methods

Posted on 2014-11-15
5
128 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 32

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 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

16 Experts available now in Live!

Get 1:1 Help Now