Assign ethernet server port in setup

I found this link and I'm trying to assign port to ethernet server in setup function, but with no results.
I declare EthernetServer server (); as global, then in setup function I call server.begin(8080); but it gives me an error:

exit status 1
request for member 'begin' in 'server', which is of non-class type 'EthernetServer()'

Can anyone helps me?

I use an arduino mega with ethernet shield W5500

Exactly which library are you using and does it come with examples ?

in setup:

server = EthernetServer(port);

The problem is the '()' after the name. The compiler reads that as a forward declaration for a function named 'server' that takes no arguments and returns an object of class 'EthernetServer'. What you want is to define an object of class 'EthernetServer' that has no constructor arguments:
EthernetServer server;

Ethernet.h, in the webserver example "server" is defined with the assigned port as a global const

It gives me other errors:
if in declaration i use EthernetServer server(); as global and in setup
server = EthernetServer(port); server.begin();
it thorws assignment of function 'EthernetServer server()'

if i declare EthernetServer server; with no brackets, the error is no matching function for call to 'EthernetServer::EthernetServer()'

use EthernetServer server(4269);
or any other port number you like as dummy value

thanks... but I think I'll assign the correct port value before setup... even if it's a little bit annoying

That probably means that the available constructors require at least one argument.

Will it let you create a pointer?

EthernetServer *serverPtr = NULL;

and then in setup(), assign the pointer a value:

serverPtr = new EthernetServer(port);

To use the pointer you have to use server->function(); in place of server.function();

With pointer there are lot's of problems, the arduino reset itself every 15s for no reason

why with new and pointers?

Why use a global pointer?

The only time you can assign a port number to an EthernetServer instance is at construction. Once you have created a global EthernetServer object, there is no way to assign it a different port. Your idea to create the global instance with a dummy port number means your object is stuck with that dummy port number forever. By creating a global pointer rather than a global instance you can defer the construction until setup() where the port number is known.

Why 'new'?
If you use a simple assignment in setup():
serverPtr = EthernetServer(port);
I'm pretty sure that would point to a local instance which will go out of scope when setup() returns. The 'new' allocates memory in the heap for the instance so it won't be out of scope when setup() returns.

Maybe you can create a 'static' instance:

static EthernetServer server(port);
serverPtr = server;

But you still need the global pointer to make the instance reachable from outside of setup().

that uses the Implicitly-declared copy assignment operator