-1

I'm new to c# and I encountered some errors. I tried looking for answers and tutorials and such, but they're not working for me. Here is the troubling code:

    panou_medii->Visible = true ;
    public: static int[] elem = new int[10] ;

And this is the error: error C2143: syntax error : missing ';' before 'public'. Without the public static in front, I get 4 errors and a warning. PS. I'm using Visual Studio 2010 and working on a form.

EDIT: Sorry for the confusion everyone, it was a visual c++ windows form, not a c#

3
  • 3
    The posted code is not C#. Are you sure you're working in a C# project? May 2, 2016 at 7:56
  • @Bea Welcome to Stackoverflow, can you tell us what kind of project you have created? May 2, 2016 at 7:57
  • To be honest, I started working on a Windows Form yesterday and the tutorials from C# are working.
    – Bea
    May 2, 2016 at 7:59

2 Answers 2

5

Thats because the code is not correct C# syntax, I think you want:

panou_medii.Visible = true;

public static int[] elem = new int[10];
3
  • It's only generating more errors
    – Bea
    May 2, 2016 at 8:00
  • @Bea I think you're haven't created a C# Windows Forms project. But a other one. Probably C or C++ May 2, 2016 at 8:05
  • I checked again and you are right, it was a visual c++ windows form. Sorry for the confusion
    – Bea
    May 2, 2016 at 8:13
1

There is one obvious mistake in the code you show, and probably a second one, depending on what you are really doing.

The easy one is in the line

public: static int[] elem = new int[10] ;

In C# access modifiers (like public, private, protected etc.) are not followed by a colon (:), so the correct syntax is

public static int[] elem = new int[10];

But your first line:

panou_medii->Visible = true ;

Here you use a dereferencing/member access operator (->). This operator can only be used with pointers in an unsafe context.
This is not very common in C#, and since you said you are working on a form, I guess it's not what you intended.

To access properties of a class instance in C#, we use the . operator. Visible in your example is probably a property of a Control and panou_medii is an instance of that Control (or precisely a variable containing a reference of that instance).

So to access the Visible of set instance use

panou_medii.Visible = true;

If you are getting more errors now, you will need to show some more of your code. For a start: if the two lines you showed are following each other directly, there's definitly something wrong. The first line is a statement that can only exist inside a method. The second line is a member declaration that cannot be contained in a method. (If you declare a variable inside a method, you don't use access modifiers like public).

Not the answer you're looking for? Browse other questions tagged or ask your own question.