-2

I'm trying this simple code below but surprisingly the compiler says

"the name pq does not exist in current context"

However as you see it´s inside the desired scope. Even with adding getters and setters I can´t do that.

namespace AskYourQuestion
{
    public struct QuestionNum
    {
        public string Q1;
    }

    class Questions
    {

        QuestionNum pq = new QuestionNum();
        pq.Q1 = "hi";

    }
}

I want to make a struct with some strings and create some classes that initialize the strings in specific languages and in main program depends on the user language, the strings appear for him.

msd
  • 19
  • 5
  • 5
    Put your statement inside a method – Ousmane D. Nov 21 '17 at 18:44
  • 2
    You can't put the line `pq.Q1 = "hi";` just like that in the class scope. It needs to be in a method scope – Gilad Green Nov 21 '17 at 18:44
  • You should also learn why [mutable structs are evil](https://stackoverflow.com/questions/441309/why-are-mutable-structs-evil) – juharr Nov 21 '17 at 18:52
  • Just some tips. Structs should be avoided unless there is a good reason for them, and normally they would only be used when the struct has a fixed memory footprint. A string field, which can be variable length, instantly makes that suspicious. Also, fields, should be private, but in C# you can just replace that semicolon with `{ get; set; }` to convert it to a property and solve that problem – Novaterata Nov 21 '17 at 18:52
  • 3
    `QuestionNum pq = new QuestionNum() { Q1 = "hi" };` is an alternative – L J Nov 21 '17 at 18:54
  • @LJ thanks. and can I access it from another class? – msd Nov 21 '17 at 19:03
  • 1
    It would really help if you explained what you hope to accomplish, not just what you are doing. – Dour High Arch Nov 21 '17 at 19:08
  • @msd , yes if you set the right [Access Modifier](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers) For example, `internal QuestionNum pq = new QuestionNum() { Q1 = "hi" };` – L J Nov 21 '17 at 19:09
  • @DourHighArch , I wanna make a struct with some strings and create some classes that initialize the strings in specific languages and in main program depends on the user language, the strings appear for him. – msd Nov 21 '17 at 19:17
  • “I wanna make a struct” is what you are doing, not a destination. *Why* do you “wanna” do that? What do you hope to achieve? A localizable UI? What kind of UI? ASP.NET? WinForms? Both APIs have localization built-in; you are probably on the wrong track completely. – Dour High Arch Nov 21 '17 at 19:53
  • @DourHighArch , Im building a telegram bot using its own api. actually user selects his/her language and questions appear for him/her. there's no need for user input at all. am I in the right path? – msd Nov 21 '17 at 20:06

3 Answers3

1

You should put that line in a method or constructor...

 public class Questions
{

    QuestionNum pq = new QuestionNum();

    Questions()
    {
        pq.Q1 = "hi";
    }

}
Novaterata
  • 4,356
  • 3
  • 29
  • 51
Milan Raval
  • 1,880
  • 1
  • 16
  • 33
1

Here are few inputs to help you further dissect the problem.

Current:

namespace AskYourQuestion
{
    public struct QuestionNum
    {
        public string Q1;
    }

    class Questions
    {
        QuestionNum pq = new QuestionNum();
        // pq.Q1 = "hi"; --> This will not work
        // Why? See below        
    }
}

Class is a specification where we encapsulate the members it should hold.

Here the Questions class encapsulate a member pq of type QuestionNum and we should specify on how the Questions class and it's encapsulating members would be constructed.

Different ways to do this:

  • Default it: QuestionNum pq = new QuestionNum() { Q1 = "Hi" };
  • Construct it: public Questions(string defaultValue) { this.pq.Q1 = defaultValue; }
  • Methods / setters

Examples for each:


Default it:

namespace AskYourQuestion
{
    public struct QuestionNum
    {
        public string Q1;
    }

    class Questions
    {
        internal QuestionNum pq = new QuestionNum() { Q1 = "Hi" };
    }
}

Construct it:

namespace AskYourQuestion
{
    public struct QuestionNum
    {
        public string Q1;
    }

    class Questions
    {
        internal QuestionNum pq = new QuestionNum() { Q1 = "Hi" };

        public Questions()
        {
        }

        public Questions(string defaultValue)
        {
            this.pq.Q1 = defaultValue;
        }
    }
}

To use it:

Questions quest = new Questions("World");
Console.WriteLine(quest.pq.Q1);

There are many other ways, but you need to choose the best case based on your problem.

L J
  • 5,249
  • 3
  • 19
  • 29
0

You can also

public struct QuestionNum
{
    public string Q1;
}

public partial class Form1 : Form
{
    QuestionNum pq = new QuestionNum()
    {
        Q1 = "something"
    };
}
Thadeu Fernandes
  • 505
  • 1
  • 6
  • 23