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

x
?
Solved

C# Properties getting null values back instead of actual values

Posted on 2016-08-31
5
Medium Priority
?
41 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 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 41

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

971 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