1

I am getting the following error for the code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7 ;

contract Variables {
   //Global variables
   uint public pubInt = 25;
   bool public truth = false;
   string public msg = "Hello World";

   function dosomething() public{
       uint8 v = 30;
       address sender = msg.sender;
   }

}

TypeError: Member "sender" not found or not visible after argument-dependent lookup in string storage ref. --> Web3/Variables.sol:12:25: | 12 | address sender = msg.sender;

Any help is greatly appreciated.

2 Answers 2

1

You are declaring a state variable with the reserved keyword msg:

string public msg = "Hello World";

You don't want to do that. You should not try to declare variables that are reserved keywords of the Solidity language, like msg which is globally available. So you are effectively 'overriding' it or shadowing it.

Better declare it like:

string public message = "Hello World";

The whole code would look like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7 ;

contract Variables {
   //Global variables
   uint public pubInt = 25;
   bool public truth = false;
   string public message = "Hello World";

   function dosomething() public {
       uint8 v = 30;
       address sender = msg.sender;
   }

}
0
pragma solidity ^0.8.7 ;

contract Variables {
   //Global variables
   uint public pubInt = 25;
   bool public truth = false;
   string public message = "Hello World"; // msg is a special global variable that needs to have a property or don't use it

   function dosomething() public{
       uint8 v = 30;
       address sender = msg.sender; // msg has a property called "sender"
   }
}

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.