• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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
0
Dynotoe
Asked:
Dynotoe
  • 5
  • 4
1 Solution
 
AGBrownCommented:
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..
}
0
 
AGBrownCommented:
I've just noticed that that probably won't work as you are trying to pass properties as ref parameters - and you can't do that, so instead, you need to modify your function to get rid of the ref keywords:

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

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

To understand whether or not you actually need to do use ref parameters, you need to understand a bit more about ref parameters
http://msdn2.microsoft.com/en-us/library/14akc2c7.aspx

ref parameters are mainly useful for value types, which would otherwise be passed as a copy of the value, so any changes to the value type would not be reflected outside the function. It is also useful for reference types, but for a different reason.

Does that help?

Andy
0
 
existenz2Commented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DynotoeAuthor Commented:
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.
0
 
AGBrownCommented:
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();
    }
}
0
 
DynotoeAuthor Commented:
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
0
 
DynotoeAuthor Commented:
Oh I forgot..

Here is a junk email address to reach me.

ishtomarfarza  "ate" comcast  "dott" net

Sean
0
 
AGBrownCommented:
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
0
 
AGBrownCommented:
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
0
 
DynotoeAuthor Commented:
I understand.  I just shot you an email.

Thx for your help on here.

Dynotoe
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now