0

The project I am developing was created in visual studio 2010. I would like to make a small addition to this project. I need to use nameof function to pass data to combobox. But, Visual studio 2010 doesn't include nameof function.

public partial class Form1 : Form
{
    ComboBox _comboBox;
    List<Instructor> _instructors;

    public Form1()
    {
        //InitializeComponent();

        var set = new HashSet<Instructor>(new InstructorComparer());
        var xml = XElement.Load("test.xml");
        foreach (var node in xml.Elements("Test").Elements("Instructor"))
        {
            var instructor = new Instructor
            {
                Num = (int)node.Attribute("Num"),
                Name = node.Element("Name").Value
            };
            set.Add(instructor);
        }

        _instructors = set.ToList();

        _comboBox.DisplayMember = nameof(Instructor.Num);
        _comboBox.DataSource = _instructors;
    }
}

What solutions do you have for this problem? thanks

dositelu
  • 25
  • 5
  • 1
    What is the problem with your code? – Hossein Sabziani Feb 06 '23 at 05:42
  • @HosseinSabziani It says "The name 'nameof' does not exist in the current context". visual studio 2010 doesn't include nameof function – dositelu Feb 06 '23 at 05:43
  • ==> https://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter – Hossein Sabziani Feb 06 '23 at 05:49
  • @HosseinSabziani It says "An object reference is required for the non-static field, method, or property 'Instructor.Num.get'". – dositelu Feb 06 '23 at 05:58
  • remove static from ==> public `static` class MemberInfoGetting --- public `static` string GetMemberName(Expression> memberExpression) – Hossein Sabziani Feb 06 '23 at 06:05
  • @HosseinSabziani didn't work. I added the complete code. It always says "An object reference is required for the non-static field, method, or property 'Instructor.Num.get'" – dositelu Feb 06 '23 at 06:16
  • The code you added seems incomplete? Is this the code you took over? And now we support the use of vs2019 and vs2022. – Jiale Xue - MSFT Feb 06 '23 at 06:24
  • @JialeXue-MSFT This is the code that i wanted to add. I would like to use vs 2022 but the project was created in vs 2010. I just want to alternative for "nameof" function. The code above written in vs 2022. – dositelu Feb 06 '23 at 06:30
  • I've added "C#4.0" tag (which matches version of C# shipped with VS 2010) assuming you want code to still compile in VS2010/C#4.0 based on prominent mentioning of VS 2010 in the question. If that is not the case please [edit] the question to remove tag/and mentioning of original version (you may clarify what version you actually targeting - probably 9 or 10 unless you building for .Net 5/6/7). In either case please do not add "visual studio" tag to questions about code *written* in VS (re-read tag's info for details - https://stackoverflow.com/tags/visual-studio/info). – Alexei Levenkov Feb 06 '23 at 06:35
  • 4
    "I would like to use vs 2022 but the project was created in vs 2010" - just because it was *originally* created in VS2010 doesn't mean you have to *keep* using VS2010. Unless it's actually targeting some technology that has now been retired, I'd expect it to work fine in VS2022, and I'd *strongly* advise doing that. – Jon Skeet Feb 06 '23 at 07:20

2 Answers2

2

Just write "Num" instead of nameof(Instructor.Num) as this is what nameof evaluates to.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
1

The nameof feature is part of C# 6. Visual Studio 2010 by default uses C# version 4. It can be changed by using a newer .NET Framework SDK.

If you need to use C# 6 language features with the current .NET Framework you can try using Microsoft.Net.Compilers NuGet package. If you add it to your project(s) the C# compiler from this package will be used instead of the default one.

This package itself depends on the version of MSBuild and .NET Framework, so you'll have to use some old version of this package. Please check version 1.3.2, I was able to use it with VS 2013, and the next version (2.0.0) required a newer MSBuild which in turn required newer .NET Framework.

Here is the list of versions of Microsoft.Net.Compilers package with the detailed information: https://github.com/dotnet/roslyn/blob/main/docs/wiki/NuGet-packages.md

According to the following description, version 1.3.2 should be the right one:

Versions 1.x mean C# 6.0 and VB 14 (Visual Studio 2015 and updates). For instance, 1.3.2 corresponds to the most recent update (update 3) of Visual Studio 2015.

Mike Mozhaev
  • 2,367
  • 14
  • 13