Link to home
Start Free TrialLog in
Avatar of Dynotoe
Dynotoe

asked on

C# - After converting to a Project from VB.net to C#.net a functions isn't building.

Hi everyone,

I converted a project from VB.net to C#.

I have this function which built and works fine in VB.Net but won't build in C#.

Here is the C# call...

if (Login((this.txtUsername.Text),(this.txtPassword.Text),(this.txtURL.Text),(this.cmbConnection.Text), oTradeDesk1)  == false)
{
etc..
}

for the function definition...
private bool Login(ref string sUserName, ref string sPassword, ref string sHostsPath, ref string sTrading, ref object oTradeDesk )
{
etc...
}

the VB.net version which builds and works fine in VB.net is as follows...

If Login((Me.txtUsername.Text), (Me.txtPassword.Text), (Me.txtURL.Text),(Me.cmbConnection.Text), oTradeDesk1) = False Then Exit Sub

which calls this function....

    Private Function Login(ByRef sUserName As String, ByRef sPassword As String, ByRef sHostsPath As String, ByRef sTrading As String, ByRef oTradeDesk As Object) As Boolean


Anyway the C# comparible (I believe anyway) gets an error(s)...
error CS1503: Argument '1': cannot convert from 'string' to 'ref string'
error CS1503: Argument '2': cannot convert from 'string' to 'ref string'
error CS1503: Argument '3': cannot convert from 'string' to 'ref string'
error CS1503: Argument '4': cannot convert from 'string' to 'ref string'


I've tried everything  ;(

Anyway please allow me to express my gratitude in advance for your gracious help.

Hope everyone is well.

Best regards,
Dynotoe
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

To get it to work you can just do:

if (Login(ref this.txtUsername.Text, ref this.txtPassword.Text, ref this.txtURL.Text , ref this.cmbConnection.Text , ref oTradeDesk1)  == false)
{
etc..
}
ASKER CERTIFIED SOLUTION
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AGBrown is right, but also get rid of this:
if (Login((this.txtUsername.Text),(this.txtPassword.Text),(this.txtURL.Text),(this.cmbConnection.Text), oTradeDesk1)  == false)
{
etc..
}


change that to:
if (Login(this.txtUsername.Text,this.txtPassword.Text,this.txtURL.Text,this.cmbConnection.Text, oTradeDesk1)  == false)
{
etc..
}

Makes the code better readable and faster. If you keep the "()", every time the calls are done, it first evaluates all those parameters, while that has totally no usage. Getting rid of the "()" will ensure they won't be evaluated and just passed along.
Avatar of Dynotoe
Dynotoe

ASKER

Thanks.

A follow up question if I might.  Does your solution take into account that the parameters which our declared at the class level as plain objects might get "stamped/altered" in these functions?  I.e., a regular object goes in but comes out with a different definition etc.  Also would there be a problem with Instance situations?

Thanks again.

Dynotoe.
This is probably better illustrated with an example. Here's a console application:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace ConsoleApplicationCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Struct");
            S myS = new S();
            myS.Value = 1;
            Console.WriteLine(myS.Value);
            AlterStruct(myS);
            Console.WriteLine(myS.Value);
            AlterStruct(ref myS);
            Console.WriteLine(myS.Value);

            Console.WriteLine("Class");
            C myC = new C();
            myC.Value = 1;
            Console.WriteLine(myC.Value);
            AlterClass(myC);
            Console.WriteLine(myC.Value);

            Console.WriteLine(myC.MyGuid.ToString());
            ChangeClass(myC);
            Console.WriteLine(myC.MyGuid.ToString());
            ChangeClass(ref myC);
            Console.WriteLine(myC.MyGuid.ToString());

            Console.ReadLine();
        }

        public static void AlterStruct(S theStruct)
        {
            theStruct.Value += 1;
        }
        public static void AlterStruct(ref S theStruct)
        {
            theStruct.Value += 1;
        }
        public static void AlterClass(C theClass)
        {
            theClass.Value += 1;
        }
        public static void ChangeClass(C theClass)
        {
            theClass = new C();
        }
        public static void ChangeClass(ref C theClass)
        {
            theClass = new C();
        }
    }
    public struct S
    {
        public int Value;
    }
    public class C
    {
        public int Value;
        public readonly Guid MyGuid = Guid.NewGuid();
    }
}
Avatar of Dynotoe

ASKER

Hi Andy,

Thanks.  It's a bit over my head ( as I'm not much of a programmer) but I'll take a look at it.

Would there be anyway to pay you a few bucks to fix and get this thing to compile for me?  I can send it via paypal etc.  It is a very small app.

Please let me know.

Dynotoe
Avatar of Dynotoe

ASKER

Oh I forgot..

Here is a junk email address to reach me.

ishtomarfarza  "ate" comcast  "dott" net

Sean
W.r.t. the code above, there are two basic scenarios:
-passing a value type (like an int variable, or a struct) to a method without ref. This does not change the value of the value type internal fields/properties outside the method.
-passing a value type (like an int variable, or a struct) to a method with a ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type (like a dataset, or a class instance) to a method without ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type to a method without ref, and changing the instance that the function argument refers to. This change does not persist outside the method.
-passing a reference type to a method with ref, and changing the instance that the function argument refers to. This change does persist outside the method (hence you see the guid change).

I had to go check the FAQ before I replied to that last one. I'm happy to help, but the EE agreement doesn't allow:

"...soliciting individuals for employment...

The only "currency" you may offer for assistance is points. Offering money or employment is prohibited within any technical thread. It's possible that Experts Exchange may, at some point, offer some kind of "classified advertising" or "Jobs" topic area, but currently, it doesn't."

That having been said, you can get in touch with me through my profile. If there is anything that you want to know that I think should be directed back to EE I'll be up front about it. Mainly as getting opinions from several people is often better. However if you want a general discussion about what you are trying to do, I think that it is fine to take that off EE.W.r.t. the code above, there are two basic scenarios:
-passing a value type (like an int variable, or a struct) to a method without ref. This does not change the value of the value type internal fields/properties outside the method.
-passing a value type (like an int variable, or a struct) to a method with a ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type (like a dataset, or a class instance) to a method without ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type to a method without ref, and changing the instance that the function argument refers to. This change does not persist outside the method.
-passing a reference type to a method with ref, and changing the instance that the function argument refers to. This change does persist outside the method (hence you see the guid change).

You can extend that example code to see what happens when you pass properties of structs and classes (where you can't use ref).

I had to go check the FAQ before I replied to that last question. I'm happy to help with advice on EE, but what the EE agreement doesn't allow is:

"...soliciting individuals for employment...

The only "currency" you may offer for assistance is points. Offering money or employment is prohibited within any technical thread. It's possible that Experts Exchange may, at some point, offer some kind of "classified advertising" or "Jobs" topic area, but currently, it doesn't."

That having been said, you can get in touch with me, or get to my website through my profile. If there is anything that you want to know that I think should be asked as an EE question I'll be up front about it (mainly as getting opinions from several people is often better). However if you want a general discussion about what you are trying to do, I think that it is fine to take that off EE.

Andy
oops ... I think I had a bit of a cut and paste problem there! Here's what it said without having to decipher it...

W.r.t. the code above, there are two basic scenarios:
-passing a value type (like an int variable, or a struct) to a method without ref. This does not change the value of the value type internal fields/properties outside the method.
-passing a value type (like an int variable, or a struct) to a method with a ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type (like a dataset, or a class instance) to a method without ref. This does change the value of the value type fields/properties outside the method.
-passing a reference type to a method without ref, and changing the instance that the function argument refers to. This change does not persist outside the method.
-passing a reference type to a method with ref, and changing the instance that the function argument refers to. This change does persist outside the method (hence you see the guid change).

You can extend that example code to see what happens when you pass properties of structs and classes (where you can't use ref).

I had to go check the FAQ before I replied to that last question. I'm happy to help with advice on EE, but what the EE agreement doesn't allow is:

"...soliciting individuals for employment...

The only "currency" you may offer for assistance is points. Offering money or employment is prohibited within any technical thread. It's possible that Experts Exchange may, at some point, offer some kind of "classified advertising" or "Jobs" topic area, but currently, it doesn't."

That having been said, you can get in touch with me, or get to my website through my profile. If there is anything that you want to know that I think should be asked as an EE question I'll be up front about it (mainly as getting opinions from several people is often better). However if you want a general discussion about what you are trying to do, I think that it is fine to take that off EE.

Andy
Avatar of Dynotoe

ASKER

I understand.  I just shot you an email.

Thx for your help on here.

Dynotoe