Cannot modify the return value of 'test.getinfo.UserInfo' because it is not a variable

I am very new to C# so please dont get to technical

I have the following class file :

using System;
using System.Collections.Generic;
using System.Text;

namespace test
{
public struct UserCredentials
{
public String UserID;
public String UserPassword;
}
public class getinfo
{
private UserCredentials _UserInfo;

public UserCredentials UserInfo
{
get { return _UserInfo; }
set { _UserInfo = value; }
}
public string GetXML()
{
string xmloutput = string.Empty;
xmloutput += "<UserCredentials>";
xmloutput += "<UserID>" + this.UserInfo.UserID + "</UserID>";
xmloutput += "<UserPassword>" + this.UserInfo.UserPassword + "</UserPassword>";
xmloutput += "</UserCredentials>";
return xmloutput;
}
}
}

and I have the following code in a Form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using test;

namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
getinfo getxmltext = new getinfo();
getxmltext.UserInfo.UserID = "testuser"; //<-- I get the error here
getxmltext.UserInfo.UserPassword = "testpassword"; //<-- I get the error here
textBox1.Text = getxmltext.GetXML;

}
}
}

The error is :

Error 1 Cannot modify the return value of 'test.getinfo.UserInfo' because it is not a variable C:\Documents and Settings\djp\My Documents\Visual Studio 2005\Projects\WindowsApplication3\WindowsApplication3\Form1.cs 22 13 WinowsApplication3

Error 2 Cannot modify the return value of 'test.getinfo.UserInfo' because it is not a variable C:\Documents and Settings\djp\My Documents\Visual Studio 2005\Projects\WindowsApplication3\WindowsApplication3\Form1.cs 23 13 WindowsApplication3

How do i solve this

Tdar



Answer this question

Cannot modify the return value of 'test.getinfo.UserInfo' because it is not a variable

  • DanDro

    UserCredentials is a struct, which has what is known as "Value semantics", which means it acts just like a number. So, basically, whereever you has an object of type UserCredentials, mentally substitute a "5" to see why it isn't working:

    xmloutput += "<UserID>" + 5 + "</UserID>"; OK
    5 = "testuser"; ERROR;

    The simpliest solution would be to just make UserCredentials a class instead of a struct. (Just change "struct" to "class" in the first line)



  • Frank Flack

    Ok, as the previous poster said, the simplest solution is to make UserCredentials a refference type (class).

    One other possible workarround is to use the following code:

    UserCredentials tempUser = getxmltext.UserInfo;

    tempUser.UserID = "someuser";

    tempUser.UserPassword = "password";

    getxmltext.UserInfo = tempUser;

    instead of:

    getxmltext.UserInfo.UserID = "testuser"; //<-- I get the error here
    getxmltext.UserInfo.UserPassword = "testpassword"; //<-- I get the error here

    Hope this helps.



  • Wil Burton

    Thanks for helping me I just dont know how to setup a class in a class to raise the propteries to the forms code using that namespace.


    Ok here is what I did so far:
    //Ok i made a class for the usercredientials:

    public class UserCredentials
    {
    private String _UserID;

    public String UserID
    {
    get { return _UserID; }
    set { _UserID = value; }
    }
    private String _UserPassword;

    public String UserPassword
    {
    get { return _UserPassword; }
    set { _UserPassword = value; }
    }

    }

    //I also made a class for deleteshipment

    public class DeleteShipment
    {
    // I Tried this assuming that I had to setup an instance in the deleteshipment code

    UserCredentials UserInfo = new UserCredentials();

    private string _SoNum;

    public string SoNum
    {
    get { return _SoNum; }
    set { _SoNum = value; }
    }

    private String _UserWorkStation;

    public String UserWorkStation
    {
    get { return _UserWorkStation; }
    set { _UserWorkStation = value; }
    }

    public string GetXML()
    {
    return "test";
    }
    }

    When I went into the for and used this namespace I still dont see the
    userinfo properties to set them.

    I guess i have to make userinfo raised somehow but I am unclear on that

    :(


    Tdar


  • rcurrie

    Thanks to all of you and Nimrand thanks for that extra info I will incorporate it.   I do have one other question say I wanted to so something like this. based on the stuff above, Is there a way to do as a dropdown list for setting the value, say i wanted to have set values like:

        public class UserCredentials
        {
            public String UserID;
            public String UserPassword;
            public String UserType;
               /* i would like just a list to show up of user types, Like admin, user, etc....*/

    }

    Keep in mind this is just for example, I am using microsoft's login controls for any real security.

    Tdar


  • Kamen

    public class UserCredentials
    {
    public String UserID;
    public String UserPassword;
    public String UserType;
    /* i would like just a list to show up of user types, Like admin, user, etc....*/

    }

    [Tdar: you probably already know this, but for the sake of future readers...]

    In order to do this as simply as possible, you can use an enum:

    public enum UserType

    {

    Administrator,

    Regular,

    Guest

    }

    public class UserCredentials

    {

    public String UserID;

    public String UserPassword;

    public UserType UserType;

    }

    Graeme


  • Seth Cohen

    James,

    Are you saying to make it a class then use a class within a class To make the functionality that I requre

    Tdar


  • Ram&amp;#38;&amp;#35;243&amp;#59;n T&amp;#38;&amp;#35;233&amp;#59;bar

    This should do it.

    namespace test
    {
    public class UserCredentials
    {
    public String UserID;
    public String UserPassword;
    }
    public class getinfo
    {
    private UserCredentials _UserInfo = new UserCredentials();

    public UserCredentials UserInfo
    {
    get { return _UserInfo; }
    set { _UserInfo = value; }
    }
    public string GetXML()
    {
    string xmloutput = string.Empty;
    xmloutput += "<UserCredentials>";
    xmloutput += "<UserID>" + this.UserInfo.UserID + "</UserID>";
    xmloutput += "<UserPassword>" + this.UserInfo.UserPassword + "</UserPassword>";
    xmloutput += "</UserCredentials>";
    return xmloutput;
    }
    }
    }

    Also, a few notes on style. By convention, you should name all types using PascalCase, meaning getinfo would be GetInfo, instead. Also, type names should generally be nouns. GetInfo is a verb phrase, which is usually used for method names (because methods perform actions), but not types (because types describe objects/nouns). These kinds of rules are less important while you are still learning, but I find its much easier understand other people's code (particularly large bodies of it) when they follow the conventions established for that language.

    Hope this helps.


  • cmk

    Thanks Tdar. The same problem was encountered by me.



  • josericardo_jr

    >> Are you saying to make it a class then use a class within a class

    Yes. (Note that String is a class, and you already use that within your class)



  • Tall Dude

    "Ok, as the previous poster said, the simplest solution is to make UserCredentials a refference type (class). "


    Well I understand that just I am not sure excatly how to code this, because I am way new, if someone could provide an example it to me is worth 1000 words. I have been searching high and low for an example of this on the net and in books.


  • Alan Robbins

    :)


  • Matt Birchall

    I know i am new to C# but this breaks it more,
    What I wanted to do was make a struct that would contain userid and password because I dont want to have to create this for ever class, I am assumeing that variable in this case means that I did not define it correcty, when I made it a class as you sujested, but this causes a runtime error of " Message="Object reference not set to an instance of an object." thats means I did not do " when i setup the reference even more problems happen.

    Let me try to explain it another way also:

    I want to have a class that has a pre setup struct for so i can do this when calling
    the class(getxmltext)

    getxmltext.UserInfo.UserPassword = "whatever"

    See code above also..

    Tdar

    maybe someone else has another way based on what I am tring to do


  • Cannot modify the return value of 'test.getinfo.UserInfo' because it is not a variable