Solved

C# Properties getting null values back instead of actual values

Posted on 2016-08-31
5
34 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
  • 3
5 Comments
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 250 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 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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