Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing classes to methods

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

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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