-4

I am trying to handle a simple structure. All that i need to have all variables right in the structure which are later gonna be used as a connection string. I am not going to assign any new values to the listed variables. I know how to do the same with classes but now i wish to discover how to hit is with structs. Also, i don't need these parameters string p1 etc in the constructor but without them, i am getting an error. Please help)

public struct ConnectionString
{
    // vars declaration
    public string ip, login, password;

    // struct constructor
    public ConnectionString(string p1, string p2, string p3)
    {
        ip = "192.168.1.1";
        login = "boris";
        password = "123456";
    }
}

But when i call it, i've got an empty output:

ConnectionString ss = new ConnectionString();
Console.WriteLine("connection string: " + ss.password);
Diego Slinger
  • 139
  • 11
  • 1
    Okay, but what is your question? What are you having problems with? *Why* do you want to use a struct for this? –  Oct 17 '17 at 20:44
  • 3
    "how to do this with structs" : NOT. Structs are for special cases, this is not one of those. Value type semantics are of no use here. – H H Oct 17 '17 at 20:45
  • @HenkHolterman what are those cases? – Diego Slinger Oct 17 '17 at 20:47
  • 1
    Also, public fields with camelCasing break mutiple rules. – H H Oct 17 '17 at 20:47
  • 2
    You are creating your `ConnectionString` object without using the 3-parameter constructor, so of course none of it is populated. –  Oct 17 '17 at 20:47
  • @Amy i would like to use the mentioned struct as the connection string. – Diego Slinger Oct 17 '17 at 20:48
  • 2
    @DiegoSlinger i'm not sure what you're responding to, but that does not answer any question i have asked nor does it provide any new information i didn't already have. –  Oct 17 '17 at 20:49
  • 1
    @DiegoSlinger: You forgot to pass any values to your constructor. As a result the code is using the implicit parameterless constructor. You also forgot to *use* any parameters that you pass to the constructor. – David Oct 17 '17 at 20:51
  • 1
    "what are those cases" : I'm not going to write a tutorial here, Google. But bypassing a required constructor, as @Amy pointed out, is one 'solution' you don't have the problem for. – H H Oct 17 '17 at 20:51
  • This is worth a read: https://stackoverflow.com/questions/2414906/what-are-the-differences-between-value-types-and-reference-types-in-c – edtheprogrammerguy Oct 17 '17 at 20:51
  • @Amy these 3 parameters in the constructor are not used at all. I've mentioned it. I put them there otherwise i am getting an error. Variables Ip, Login and Password are already given a value. ip = "192.168.1.1"; login = "boris"; password = "123456"; – Diego Slinger Oct 17 '17 at 20:52
  • 1
    Why don't you remove them? You aren't using them anyway. `public ConnectionString()`? No, you are only giving those 3 fields values **when that constructor is called**. You are **not** calling that constructor. –  Oct 17 '17 at 20:53
  • Set a breakpoint in your constructor, check that `ip = "192.168.1.1";` is never executed. – H H Oct 17 '17 at 20:54
  • 1
    Possible duplicate of [Why can't I define a default constructor for a struct in .NET?](https://stackoverflow.com/questions/333829/why-cant-i-define-a-default-constructor-for-a-struct-in-net) – mjwills Oct 17 '17 at 20:57

3 Answers3

3
// struct constructor
public ConnectionString(string p1, string p2, string p3)
{
    ip = "192.168.1.1";
    login = "boris";
    password = "123456";
}

But when i call it, i've got an empty output:

ConnectionString ss = new ConnectionString();
Console.WriteLine("connection string: " + ss.password);

You are not calling the constructor you defined; you're calling the implicitly-generated default constructor, which is why none of the fields are populated.

EDIT: From the documentation, note the following:

Within a struct declaration, fields cannot be initialized unless they are declared as const or static.

Options:

1) Delete your constructor and declare the fields const and provide values, i.e.

public struct ConnectionString
{
    // vars declaration
    public const string ip = "192.168.1.1";
    public const string login = "boris";
    public const string password = "123456";
}

2) Populate those fields after calling the default constructor,

3) Call the constructor (with parameters) you defined,

4) (best option) not use structs for this problem.

Reference: Using Structs (C# Programming Guide)

Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26
1

Since you are not using parameters passed in the constructor inside it, you can call it as shown below. As others have already commented, getting connection string is not something, you should do with struct. Structs are generally used when a group of related parameters and functionality are to be collectively used, and made available as set for smaller scopes of call. They are passed by value, not by reference.

ConnectionString connect = new ConnectionString("","","");
Console.WriteLine("connection string: " + connect.password);
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
0

Instead of:

public ConnectionString(string p1, string p2, string p3) .....

use:

public static ConnectionString Create()
{
    return new ConnectionString { ip = "a", login = "b", password = "c" };
}

and use:

ConnectionString ss = ConnectionString.Create();

This is assuming you must use a struct at all, of course.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • If i call the constructor like you said: ConnectionString ss = new ConnectionString.Create(); I am gettig an error Severity Code Description Project File Line Suppression State Error CS0426 The type name 'Create' does not exist in the type 'ConnectionString' SadRecreate – Diego Slinger Oct 17 '17 at 21:23
  • @DiegoSlinger You have to add the code before it (`public static ConnectionString Create()......`) inside the struct `ConnectionString`. – ispiro Oct 17 '17 at 21:40
  • my code looks like this:namespace StarterProject { public struct ConnectionString { // vars declaration public string ip, login, password; // struct constructor public static ConnectionString Create() { return new ConnectionString { ip = "a", login = "b", password = "c" }; } } } – Diego Slinger Oct 18 '17 at 05:27
  • @DiegoSlinger And you still get the error `The type name 'Create' does not exist in the type 'ConnectionString' `? If yes, then I don't know. – ispiro Oct 18 '17 at 08:25