Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Properties getting null values back instead of actual values

Posted on 2016-08-31
5
Medium Priority
?
40 Views
Last Modified: 2016-08-31
I am trying to have a form which gets submitted and passes various text box parameters down along the way.  So I have properties in class for that with private strings (didn't use the auto-implemented properties but tried both).

Here is where the values originate from a WPF form:

            var exe = new DataReadWrite();
            var uv = new UserValues();

            uv.strFirstName = tbxFirstName.Text;
            uv.strLastName = tbxLastName.Text;
            uv.strEmailAddress = tbxEmailAddress.Text;

            exe.exeInsUser();

In the debugger the values above are populated...

Now when exeInsUser() chokes, it is due to the values being null as here:

separate class to hold the values:

    public class UserValues
    {
        private String firstname;
        private String lastname;
        private String emailaddress;

        public string strFirstName
        {
            get
            {
                return firstname;
            }

            set
            {
                firstname = value;
            }
        }

        public string strLastName
        {
            get
            {
                return lastname;
            }

            set
            {
                lastname = value;
            }
        }

        public string strEmailAddress
        {
            get
            {
                return emailaddress;
            }

            set
            {
                emailaddress = value;
            }
        }


The execution fails here with a null reference exception:

separate class to execute:

            var oPa = new DataParameters();
            var str = new UserValues();

            StoredProcParameter[] oPL = oPa.insUsersParm( str.strFirstName
                                                        , str.strLastName
                                                        , str.strEmailAddress);

I even tried implementing an interface (although the code above has this removed in the implementation) but everything is null!?

Any help is greatly appreciated!
0
Comment
Question by:cyimxtck
[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
  • 3
5 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 1000 total points
ID: 41778733
You have to pass in the UserValues object, and I don't see that you are doing that.

For example:

            var exe = new DataReadWrite();
            var uv = new UserValues();

            uv.strFirstName = tbxFirstName.Text;
            uv.strLastName = tbxLastName.Text;
            uv.strEmailAddress = tbxEmailAddress.Text;

            exe.exeInsUser();

Open in new window


I don't see where exe is getting the uv values from. I would expect something similar to:

            var exe = new DataReadWrite();
            var uv = new UserValues();

            uv.strFirstName = tbxFirstName.Text;
            uv.strLastName = tbxLastName.Text;
            uv.strEmailAddress = tbxEmailAddress.Text;

            exe.exeInsUser(uv);

Open in new window


The exeInsUser() method would use the values from uv to populate the SQL parameters.
0
 

Author Comment

by:cyimxtck
ID: 41778752
I am not passing them right there I am getting them later in another routine from the UserValues.

In the bottom half of the original post:

separate class to execute:

            var oPa = new DataParameters();
            var str = new UserValues();

            StoredProcParameter[] oPL = oPa.insUsersParm( str.strFirstName
                                                        , str.strLastName
                                                        , str.strEmailAddress);
0
 

Author Comment

by:cyimxtck
ID: 41778757
public void exeInsUser()
        {
            insUsers();
        }
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 1000 total points
ID: 41778801
var str = new UserValues();

You create a new object, and the properties don't have anything assigned to them at that point.

Either pass in a UserValue object

public void exeInsUser(UserValue uv)
        {
            insUsers(uv);
        }

Open in new window


as was suggested or move insUsers into the UserValue class, and reference the existing object.
0
 

Author Closing Comment

by:cyimxtck
ID: 41778826
Perfect!
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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