Problem with custom constructor for a struct

10/06/2013 18:33 desTenshi#1
Hay guys i have the following problem:

I have a struct for saving some faked "weather" data. So i want to store a random number as measure value and the date + time it was measured. My teacher told me to use a custom constructor so that i can give the values when creating a new one. So far so good but when i did it Visual Studio marked me the first value in the master constructor as error.
The error is :
PHP Code:
The this object can only be used when all fields have been assigned
And this is the struct code :
PHP Code:
struct WeatherData
    
{
        private 
int _fakeValue;
        private 
string _fakeDate;
        
// getter und setter for the privat members
        
public int FakeValue
        
{
            
get { return _fakeValue; }
            
set _fakeValue value; }
        }
        public 
string FakeDate
        
{
            
get { return _fakeDate; }
            
set _fakeDate value; }
        }
        
// overrides the inherited ToString method with my own version
        
public override string ToString()
        {
            return 
String.Format("Wert: {0}, gemessen am: {1}"FakeValueFakeDate);
        }

        public 
WeatherData(int value)
        :
this(valueDateTime.Now.ToString()){}

        public 
WeatherData(string date)
        :
this(0date){}

        
// here is the error but i don't know why... 
        
public WeatherData(int valuestring date)
        {
            
this.FakeValue value;
            
this.FakeDate date;
        }
    } 
So would be nice if anyone could tell me what's wrong there ^^
And if you have any ideas of making it better just tell me :o

desTenshi

ps:

This whole program is a school project i got from my teacher, so don't say something about the topic of it xD ...
10/06/2013 20:42 Easy-Emu#2
You should inside the class:

Code:
public WeatherData(int value, string date)
{
    _fakeValue = value;
    _fakeDate = date;
}
10/06/2013 21:01 desTenshi#3
Quote:
Originally Posted by Easy-Emu View Post
You should inside the class:

Code:
public WeatherData(int value, string date)
{
    _fakeValue = value;
    _fakeDate = date;
}
Well in this case it would be ok if i use it, but if i would use any tests or something in the setter i would have to use this code again here. So in my eyes it's not the best way to solve the problem by just using the private variable :/
10/07/2013 03:18 nkkk#4
another solution is calling the default contructor before wich sets all fields to their default values:
Code:
        public WeatherData(int value, string date)
            :this()
        {
            this.FakeValue = value;
            this.FakeDate = date;
        }
note that you cannot change the compiler generated default constructor. i think this is more a question of taste then of anything else.
10/07/2013 11:20 desTenshi#5
Quote:
Originally Posted by nkkk View Post
another solution is calling the default contructor before wich sets all fields to their default values:
Code:
        public WeatherData(int value, string date)
            :this()
        {
            this.FakeValue = value;
            this.FakeDate = date;
        }
note that you cannot change the compiler generated default constructor. i think this is more a question of taste then of anything else.
Well that works. But i still don't understand why VS say's that's an error.. I can compile it and it works just fine.
I know i cant change the default constructor for a struct, but in which way does this effect the main constructor? (sorry if that's a stupid question :x )
10/07/2013 12:15 Shawak#6
You don't use :this() {..}

Code:
	public WeatherData(int value)
	{
		WeatherData(value, DateTime.Now.ToString());
	}

	public WeatherData(string date)
	{
		WeatherData(0, date);
	}

        public WeatherData(int value, string date)
        {
		_fakeValue = value;
		_fakeDate = date;
        }
Edit: nvm, mixed something up
10/07/2013 14:06 Easy-Emu#7
Quote:
Originally Posted by desTenshi View Post
Well in this case it would be ok if i use it, but if i would use any tests or something in the setter i would have to use this code again here. So in my eyes it's not the best way to solve the problem by just using the private variable :/
it's normal to use private variables inside the class and the public getter/setter only in other classes
10/07/2013 14:21 desTenshi#8
Quote:
Originally Posted by Shawak View Post
You don't use :this() {..}

Code:
	public WeatherData(int value)
	{
		WeatherData(value, DateTime.Now.ToString());
	}

	public WeatherData(string date)
	{
		WeatherData(0, date);
	}

        public WeatherData(int value, string date)
        {
		_fakeValue = value;
		_fakeDate = date;
        }
Why ? It's normal constructor chaining?

Quote:
Originally Posted by Easy-Emu View Post
it's normal to use private variables inside the class and the public getter/setter only in other classes
Well but that would be against the logic to implement the code only once.
Also the book Pro C# 5.0 and the .net 4.5 framework tells to use the c# properties. In my eye's it's smarter than write the code for class / struct data checks two times.