#BlockChain-Etherum -DAPPS- Accessing Functions between contracts in Solidity.

I am using Remix-Solidity IDE for browser – https://ethereum.github.io/browser-solidity and have started learning solidity from udemy course- Ravinder Deol

In this post ,I will share how to access function between two contracts using solidity over etherum.

Step 1 : Creating a simple contract with Get/Set Function

Lets create a simple contract with a get and set function :

contract SampleContract1
{
uint myvar;
function SetVar(uint varvalue){
myvar = varvalue;
}
function GetVar() constant returns(uint)
{
return myvar;
}
}

Step 2 : Creating contract with constructor function that consumes other contracts

Lets create another contract where we got use function of contract1. In this contract we create constructor where we pass the adress of contract1.

contract SampleContract2
{
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1) // constructor
{
instanceofsc1=SampleContract1(addressofsc1);
}

}

 

Step 3 : Accessing Function from other contract 

After we pass the address of contract1,we would be able to access the functions directly in contract2.

contract SampleContract2
{
SampleContract1 instanceofsc1;
function SampleContract2(address addressofsc1)
{
instanceofsc1=SampleContract1(addressofsc1);
}

function GetvarofSc1() constant returns(uint)
{
return instanceofsc1.GetVar();
}
}

Output in Solidity Browser :

Capture

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s