Solved

C# Properties getting null values back instead of actual values

Posted on 2016-08-31
5
37 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 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 40

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting PHP triple des decrypt to c# 9 148
PowerShell: ForEach-Object Export to CSV 4 86
async questions 5 45
msiexec won't run 4 12
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

742 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