-1

I'm pretty sure that I am duplicating a question, but somehow in my example it doesn't work, please see the code:

class Program
{
    static void Main(string[] args)
    {
        Test test1 = new Test("str", "strrr");
    }

}
class Test
{

    public string testValue, mType;

    public Test(string value, string messageType)
    {
        this.testValue = value;
        this.mType = messageType;

    }

    public Test (string value) : this (value, messageType)
    {
        //want to manipulate value and messageType here
    }


}

messageType in this constructor public Test (string value) : this (value, messageType) says it doesn't exist in the current context. I want to call it in this way because first of all I want my code instantiating the class with two strings, and then I want to provide value only to one parameter constructor but not lose messageType, because I will use it within this constructor public Test (string value). I read about chaining and instantiating constructors but it seems that this thing is opposite to what I read. Sorry not a lot practice yet with programming if this is a simple question, I would like to know how the code should look. What I have read before pointing this question: Call one constructor from another, How call constructor inside other constructor?, http://www.csharp411.com/constructor-chaining/, it doesn't work for me and again sorry if I am duplicating or doing silly things.

Community
  • 1
  • 1
  • To call a different constructor with customized parameters, they need to be able to be evaluated in the "constructor space". Or static variables – Falgantil May 09 '15 at 11:27
  • 1
    What is `messageType` in that second constructor? Where does it come from? – Lasse V. Karlsen May 09 '15 at 11:28
  • If is it possible can you provide an example ? – SeeSharpWithGoggles May 09 '15 at 11:28
  • It should come from first constructor, because instantiating object i give him string strrr and it should be put in that variable messageType, and constructor `public Test (string value) : this (value, messageType)` should use it. Because inside it it will be manipulated. – SeeSharpWithGoggles May 09 '15 at 11:31
  • Why are you not working with `value` in the overload which takes two parameters? – Yuval Itzchakov May 09 '15 at 11:36
  • Maybe like so: `public Test (string value) : this (value, value)`? – abto May 09 '15 at 11:38
  • Because i need to get that value in second constructor and messageType to get in second constructor, but messageType only will be worked inside of it so first i need to assign values which i getting from database some string value and messageType, and i need messageType to be visible because i will use it in second constructor, if that explains anything. – SeeSharpWithGoggles May 09 '15 at 11:39
  • Hi abot, it doesn't do the thingwhen first constructor called, it should assign two differrent strings, and those strings should be manipulated in second constructor, value should be provided as value between () and messageType will be inside of it manipulated. – SeeSharpWithGoggles May 09 '15 at 11:42
  • So it does not mater which values are set through the chained constructor, because you will change it in the called constructor afterward. – abto May 09 '15 at 11:46
  • Why does messageType HAVE to come from first constructor? What is wrong with passing or null or some other keyword to tell the 2nd ctor to get it from the database? Does it really matter where that call to the database happens? – paparazzo May 09 '15 at 12:08
  • The call to the database and message string comes before creating an object of Test class and then i want to put two values in it, and then manipulate those values inside and provide them to constructor with one parameter. If that explains something. – SeeSharpWithGoggles May 09 '15 at 12:15
  • Why you need to manipulate strings inside constructor? I think better place for this `static` method – Fabio May 09 '15 at 13:36

3 Answers3

0

So in your example, you have to constructors: one accepting value and type and the other accepting only value. In such case, if the user provides a value only, the type should have a default assumed value. For example, you could do something like:

public Test (string value) : this (value, "Default Message Type")
{

}

What this code does is that it gets the value from the user, i.e. the caller of the constructor, and pass it to the other constructor, along with a default message called "Default Message Type".

Notice that you don't need to put any code in this constructor, since it will call the other constructor which will set both the value and the type.

Hope this helps.

EDIT: From the comments, I understood from that @ArnosGo wants to do some manipulation on value and add some logic that finds the value of messageType, then pass both to the other constructor. Unfortunately, it is not possible to do some manipulation on data before calling the other constructor, but here is a trick to do this:

class Test
{
    public string testValue, mType;

    public Test(string value, string messageType)
    {
        Initialise(value, messageType);
    }

    public Test (string value)
    {
        // Do some manipulation here and found out the value of messageType.
        Initialise(value, messageType);
    }

    protected void Initialise(string value, string messageType)
    {
        this.testValue = value;
        this.mType = messageType;
    }
}

Basically, you are creating a function that does what both constructors do, then the constructors only calculate the values and pass them to that function.

Rafid
  • 18,991
  • 23
  • 72
  • 108
  • It doesn't because messageType is string too, which is not default and everytime will be differrent when quering from database. But it needs to be worked inside of this constructor `public Test (string value) : this (value, messageType)` – SeeSharpWithGoggles May 09 '15 at 11:44
  • So you mean you want to do some logic that finds out the messageType and then pass it to the other constructor? – Rafid May 09 '15 at 11:44
  • Where does this query to the database occur? – abto May 09 '15 at 11:44
  • yes Rafid correct takes from first constructor both strings and uses in second, value will be the same, but messageType will be only manipulated in functions inside of second constructor. Abto that messageType already comes as string which i want to manipulate as shown. – SeeSharpWithGoggles May 09 '15 at 11:46
  • Ah, unfortunately then you won't be able to do any logic before redirecting to the other constructor, so you will have to put the same logic again. However, what you can do is have a common method that is called by both of them, which will save you the duplication. Let me edit my answer with some example. – Rafid May 09 '15 at 11:48
  • If you call `new Test(value)` I don't see any code which is providing anything for `messageType`. Maybe you can add that part to your questions code? – abto May 09 '15 at 11:49
  • i want to call only `new Test("some value string", "messagetype string" )` not other type and from this two type constructor provide all values to one parameter constructor already inside, value would be put here `public Test ( "some value string") : this (value, messageType)` and inside of this one parameter constructor i want to manipulate with `"messagetype string"` if that makes any sense – SeeSharpWithGoggles May 09 '15 at 11:55
  • Rafid in this piece of code `public Test (string value) { // Do some manipulation here and found out the value of messageType. Initialise(value, messageType); }` messageType says not in the context for me. – SeeSharpWithGoggles May 09 '15 at 12:04
  • Yeah, because you have to define it and put some value in it before passing it, otherwise where does the compiler get the value from? – Rafid May 09 '15 at 12:07
  • It should get from two parameters constructor when instantiating class `Test test1 = new Test("value string", "messageType value string");` from here everything comes when creating an object. – SeeSharpWithGoggles May 09 '15 at 12:17
0

The call to the database and message string comes before creating an object of Test class and then i want to put two values in it, and then manipulate those values inside and provide them to constructor with one parameter. If that explains something.

Based on OP's comment, I think approach with static method will be better
Because values are manipulated only once and properties assigned only once.

public class Test
{
    private static ManipulateValues(string value, string msgtype)
    {
        //Do your manipulating 
        Return manipulatedValue;
    }

    public static Create(string value, string msgtype)
    {
        return new Test(Test.ManipulateValues(value, msgtype), msgtype); 
    }

    public Test(string value, string msgType)
    {
        this.testValue = value;
        this.mType = messageType;
    }

}

Then use it:

string messageType = someValue;
string value = someDatabaseValue;
Test temp = Test.Create(value, messageType)
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Constructor with one parameter should access somehow messageType to use within it, so the null not the case in my opinion, because all manipulation with value and messageType should occur in COnstructor with one parameter, if that makes sense any ? – SeeSharpWithGoggles May 09 '15 at 11:52
  • Then use updated suggestion of @Rafid. Create method and call it inside of contructors. – Fabio May 09 '15 at 11:54
  • Interesteing example fabio, but when i do like this: `public string GetMessageType() { return this.mType; }` i do public because, my value mType is public string, so in constructor `GetMessageType()` says error `Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Test.GetMessageType()' C:\Users\AG\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 38 27 ConsoleApplication1` – SeeSharpWithGoggles May 09 '15 at 12:10
  • Sorry, my bad. `GetMessageType` method must be static. Check updated answer again – Fabio May 09 '15 at 12:38
  • Ok i understood now, what i want to achieve is not possible, but your code does a thing i can think about it, one more question. Can other constructor be called inside a Method like a method, i guess not ? for example: `public void DoSomething() { Test(this.testValue); }` – SeeSharpWithGoggles May 09 '15 at 12:50
  • Constructor create new object. Based on the information you given, I think you can try suggestion of @Rafid – Fabio May 09 '15 at 13:09
0

I think what you want to do is call Test test1 = new Test("Value", "MessageType"); to instantiate an object. Then the constructor with two strings will set these parameters to testValue and mType fields and call the first constructor to make manipulations.

In your code the first constructor does not call the second one, actually the second constructor calls the first one. If you want the first constructor to call the second your constructor signatures should be like this:

public Test(string value, string messageType) : this(value)
public Test(string value)

But here you cannot send messageType parameter to the second constructor. Therefore you should write an initialization method, put the content of the second constructor in this method and call it from both constructors and do not use constructor chaining.

public Test(string value, string messageType)
{
    this.testValue = value;
    this.mType = messageType;

    Initialize();
}
public Test(string value)
{
    this.testValue = value;

    Initialize()
}

public void Initialize()
{
    //This is the code in the second constructor
}
Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
  • Thanks qqww2 it does what i need, thank you very much :) – SeeSharpWithGoggles May 09 '15 at 12:58
  • Sorry. Seems like constructor with two parameters call constructor with one parameter: `public Test(string value, string messageType) : this(value)`. In that way `Initialize` method called twice and properties will be assigned twice. Sorry again, it seems no sense for me – Fabio May 09 '15 at 13:19
  • No, in the code below the constructor `public Test(string value, string messageType)` does not have `: this(value)` call. – Yusuf Tarık Günaydın May 09 '15 at 13:25