0
pragma solidity ^0.4.17;


contract hoteltranscation  {

  struct hotel{
    string HotelName;
    uint256 BookingNumber;
  }

   function HotelBooking(string _HotelName,uint256 _BookingNumber ) public   {
     hotel storage addtrascation =  hotel (_HotelName,_BookingNumber);
   }
}

I have declared a struct as public varable and i want to use it as storage variable inside a function

2) I am getting the following error

':12:6: TypeError: Type struct hoteltranscation.hotel memory is not implicitly convertible to expected type struct hoteltranscation.hotel storage pointer.\n     hotel storage addtrascation =  hotel (_HotelName,_BookingNumber);\

3) I want to make the state varables as storage varables and store some data

  • . i have updted the question i want to create a varable refrence to state varable inside the function so to reduce the gas consuming – md venkatesh Oct 25 '18 at 6:35
1

Taking a stab in the dark to what you are trying to do here, feel free to update your question, and I can either remove my answer or update it.

It looks like you want to input the values of a hotel struct, and then store that somewhere permanently, maybe an array of hotel bookings.

You can do that like this:

pragma solidity ^0.4.17;

contract hoteltranscation  {

    struct hotel{
        string HotelName;
        uint256 BookingNumber;
    }

    hotel[] HotelBookings;

    function HotelBooking(string _HotelName, uint256 _BookingNumber) public   {
        HotelBookings.push(hotel (_HotelName, _BookingNumber));
    }

    function GetHotelBooking(uint _num) public view returns(string, uint256) {
        return (HotelBookings[_num].HotelName, HotelBookings[_num].BookingNumber);
    }
}

No need to get too fancy here. If for some reason you need to use a temporary variable before you push to the global array, that would be a perfect use of a memory variable:

pragma solidity ^0.4.17;

contract hoteltranscation  {

    struct hotel{
        string HotelName;
        uint256 BookingNumber;
    }

    hotel[] HotelBookings;

    function HotelBooking(string _HotelName, uint256 _BookingNumber) public   {

        hotel memory tempHotel = hotel (_HotelName, _BookingNumber);
        // Do some logic
        HotelBookings.push(tempHotel);
    }

    function GetHotelBooking(uint _num) public view returns(string, uint256) {
        return (HotelBookings[_num].HotelName, HotelBookings[_num].BookingNumber);
    }
}

Does this help?

  • Thanks Means when storing with data of struct we use array or mapping is it correct – md venkatesh Oct 25 '18 at 7:02
  • You don't have to. You can also store just a single value (hotel SingleHotelBooking;), but it probably makes sense to put it in the "contract context" so that it is stored in the contract and accessible from other functions. Maybe read this for more details on the memory and storage keyword. – Shawn Tabrizi Oct 25 '18 at 7:04
  • 1
    Here is a relevant quote from the article: storage cannot be newly created in a function. Any storage referenced variable in a function always refers a piece of data pre-allocated on the contract storage (state variable). Any mutation persists after function call. – Shawn Tabrizi Oct 25 '18 at 7:05
  • Sorry for bothering you But should we mandtery name array and struct same name does it have any link – md venkatesh Oct 25 '18 at 7:10
  • Sorry, I didn't quite understand your question, but the naming of variables, structs, and arrays are all independent of one another. They just need to be consistent. – Shawn Tabrizi Oct 25 '18 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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